]> granicus.if.org Git - python/commitdiff
Merged revisions 75818 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 27 Oct 2009 17:47:14 +0000 (17:47 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 27 Oct 2009 17:47:14 +0000 (17:47 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r75818 | antoine.pitrou | 2009-10-27 18:41:58 +0100 (mar., 27 oct. 2009) | 3 lines

  Issue #7205: Fix a possible deadlock when using a BZ2File object from several threads at once.
........

Lib/test/test_bz2.py
Misc/NEWS
Modules/bz2module.c

index 6715a023a839ae554df6088b64e298c8446785d8..9a9afa6e6b551c75464af21f1163a96273b22a9c 100644 (file)
@@ -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):
index bb5e4cc7e82562d5555a750eca044566d06deddd..9707276ecb19deb9bde00ab297363deba1f7b163 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -117,6 +117,9 @@ C-API
 Library
 -------
 
+- Issue #7205: Fix a possible deadlock when using a BZ2File object from
+  several threads at once.
+
 - Issue #7077: logging: SysLogHandler now treats Unicode as per RFC 5424.
 
 - Issue #7099: Decimal.is_normal now returns True for numbers with exponent
index 5f1d01b7751a5bbccf6a4f94bf06645c3e426b5e..550f1cf9999102252e3c88e83ff2cfe6a98607a1 100644 (file)
@@ -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)