diff options
-rw-r--r-- | Lib/test/test_bz2.py | 18 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/bz2module.c | 7 |
3 files changed, 27 insertions, 1 deletions
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index 6715a02..9a9afa6 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -7,6 +7,7 @@ from io import BytesIO import os import subprocess import sys +import threading # Skip tests if the bz2 module doesn't exist. bz2 = support.import_module('bz2') @@ -282,6 +283,23 @@ class BZ2FileTest(BaseTest): else: self.fail("1/0 didn't raise an exception") + def testThreading(self): + # Using a BZ2File from several threads doesn't deadlock (issue #7205). + data = b"1" * 2**20 + nthreads = 10 + f = bz2.BZ2File(self.filename, 'wb') + try: + def comp(): + for i in range(5): + f.write(data) + threads = [threading.Thread(target=comp) for i in range(nthreads)] + for t in threads: + t.start() + for t in threads: + t.join() + finally: + f.close() + class BZ2CompressorTest(BaseTest): def testCompress(self): @@ -34,6 +34,9 @@ Core and Builtins Library ------- +- Issue #7205: Fix a possible deadlock when using a BZ2File object from + several threads at once. + - Issue #7071: byte-compilation in Distutils is now done with respect to sys.dont_write_bytecode. diff --git a/Modules/bz2module.c b/Modules/bz2module.c index 5f1d01b..550f1cf 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -78,7 +78,12 @@ typedef fpos_t Py_off_t; #ifdef WITH_THREAD -#define ACQUIRE_LOCK(obj) PyThread_acquire_lock(obj->lock, 1) +#define ACQUIRE_LOCK(obj) do { \ + if (!PyThread_acquire_lock(obj->lock, 0)) { \ + Py_BEGIN_ALLOW_THREADS \ + PyThread_acquire_lock(obj->lock, 1); \ + Py_END_ALLOW_THREADS \ + } } while(0) #define RELEASE_LOCK(obj) PyThread_release_lock(obj->lock) #else #define ACQUIRE_LOCK(obj) |