Commit 641d6217 authored by Tim Peters's avatar Tim Peters

Issue 19158: a rare race in BoundedSemaphore could allow .release() too often.

(grafted from e06edc0c7a4951327f0c95ebeebccba6879a6063)
parent 090588ec
......@@ -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):
......
......@@ -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):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment