]> granicus.if.org Git - python/commitdiff
Issue 19158: a rare race in BoundedSemaphore could allow .release() too often.
authorTim Peters <tim@python.org>
Wed, 9 Oct 2013 01:55:51 +0000 (20:55 -0500)
committerTim Peters <tim@python.org>
Wed, 9 Oct 2013 01:55:51 +0000 (20:55 -0500)
(grafted from e06edc0c7a4951327f0c95ebeebccba6879a6063)

Lib/test/test_threading.py
Lib/threading.py

index 5a765f32083702f468f1b7c7591d1dbc64cc4401..ac0b4bc701dc706da3fc126b2e1de597c7cb6936 100644 (file)
@@ -466,6 +466,23 @@ class ThreadTests(BaseTestCase):
         finally:
             sys.setcheckinterval(old_interval)
 
+    def test_BoundedSemaphore_limit(self):
+        # BoundedSemaphore should raise ValueError if released too often.
+        for limit in range(1, 10):
+            bs = threading.BoundedSemaphore(limit)
+            threads = [threading.Thread(target=bs.acquire)
+                       for _ in range(limit)]
+            for t in threads:
+                t.start()
+            for t in threads:
+                t.join()
+            threads = [threading.Thread(target=bs.release)
+                       for _ in range(limit)]
+            for t in threads:
+                t.start()
+            for t in threads:
+                t.join()
+            self.assertRaises(ValueError, bs.release)
 
 class ThreadJoinOnShutdown(BaseTestCase):
 
index 72c83197fd1cf4b901ba9163f18f910507a5a304..167f8c7fe3164b1d548db2be34458e73196aa1c7 100644 (file)
@@ -531,9 +531,11 @@ class _BoundedSemaphore(_Semaphore):
         raise a ValueError.
 
         """
-        if self._Semaphore__value >= self._initial_value:
-            raise ValueError("Semaphore released too many times")
-        return _Semaphore.release(self)
+        with self._cond:
+            if self._value >= self._initial_value:
+                raise ValueError("Semaphore released too many times")
+            self._value += 1
+            self._cond.notify()
 
 
 def Event(*args, **kwargs):