From: Tim Peters Date: Wed, 9 Oct 2013 01:55:51 +0000 (-0500) Subject: Issue 19158: a rare race in BoundedSemaphore could allow .release() too often. X-Git-Tag: v3.4.0a4~204^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7634e1cf9028ed5b21f0a97db45c91d8e8a159a6;p=thirdparty%2FPython%2Fcpython.git Issue 19158: a rare race in BoundedSemaphore could allow .release() too often. --- diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index fef3314c16fe..0ebeb39cbdc6 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -468,6 +468,24 @@ class ThreadTests(BaseTestCase): self.assertEqual(0, status) + 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): # Between fork() and exec(), only async-safe functions are allowed (issues diff --git a/Lib/threading.py b/Lib/threading.py index c98a0062314a..3d4952b58630 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -283,9 +283,11 @@ class BoundedSemaphore(Semaphore): 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: