Issue #7205: Fix a possible deadlock when using a BZ2File object from several threads at once.
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
index 215a04e..f68586d 100644
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -7,6 +7,7 @@
 import os
 import subprocess
 import sys
+import threading
 
 bz2 = import_module('bz2')
 from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
@@ -306,6 +307,23 @@
         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 = "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):