]> granicus.if.org Git - python/commitdiff
Merged revisions 75818 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 27 Oct 2009 17:46:09 +0000 (17:46 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 27 Oct 2009 17:46:09 +0000 (17:46 +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 c4d9b6968a3426c21e06cccb311dc88f2e054bbf..e1e5a28fb7196468796c2b6b635d45855f4c7c43 100644 (file)
@@ -7,6 +7,7 @@ from cStringIO import StringIO
 import os
 import subprocess
 import sys
+import threading
 
 import bz2
 from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
@@ -284,6 +285,23 @@ class BZ2FileTest(BaseTest):
         bz2f.close()
         self.assertEqual(xlines, ['Test'])
 
+    def testThreading(self):
+        # Using a BZ2File from several threads doesn't deadlock (issue #7205).
+        data = "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 99eaa6d787ef9586b108be56e313ff2669f3b8f8..6f61c49d10a42d8ba281f3527dbc39164d395d53 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7205: Fix a possible deadlock when using a BZ2File object from
+  several threads at once.
+
 - Issue #7048: Force Decimal.logb to round its result when that result
   is too large to fit in the current precision.
 
index b5542bf968d3430a241707586f92898f7f2de10a..a53d1a50f8dc26a7419efdf61f4ed7d6074dbf7b 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)