[Python-checkins] cpython (merge 3.3 -> default): Issue 19158: a rare race in BoundedSemaphore could allow .release() too often.

tim.peters python-checkins at python.org
Wed Oct 9 04:30:16 CEST 2013


http://hg.python.org/cpython/rev/7c56bf5afee6
changeset:   86166:7c56bf5afee6
parent:      86164:1cbd3d9f7d61
parent:      86165:e06edc0c7a49
user:        Tim Peters <tim at python.org>
date:        Tue Oct 08 21:12:58 2013 -0500
summary:
  Issue 19158:  a rare race in BoundedSemaphore could allow .release() too often.

files:
  Lib/test/test_threading.py |  18 ++++++++++++++++++
  Lib/threading.py           |   8 +++++---
  2 files changed, 23 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -599,6 +599,24 @@
             time.sleep(0.01)
         self.assertIn(LOOKING_FOR, repr(t)) # we waited at least 5 seconds
 
+    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):
 
     def _run_and_join(self, script):
diff --git a/Lib/threading.py b/Lib/threading.py
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -289,9 +289,11 @@
         self._initial_value = value
 
     def release(self):
-        if self._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()
 
 
 class Event:

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list