Issue #13502: threading: Fix a race condition in Event.wait() that made it

return False when the event was set and cleared right after.
parent bf894e62
...@@ -782,8 +782,10 @@ An event object manages an internal flag that can be set to true with the ...@@ -782,8 +782,10 @@ An event object manages an internal flag that can be set to true with the
floating point number specifying a timeout for the operation in seconds floating point number specifying a timeout for the operation in seconds
(or fractions thereof). (or fractions thereof).
This method returns the internal flag on exit, so it will always return This method returns true if and only if the internal flag has been set to
``True`` except if a timeout is given and the operation times out. true, either before the wait call or after the wait starts, so it will
always return ``True`` except if a timeout is given and the operation
times out.
.. versionchanged:: 3.1 .. versionchanged:: 3.1
Previously, the method always returned ``None``. Previously, the method always returned ``None``.
......
...@@ -351,6 +351,22 @@ class EventTests(BaseTestCase): ...@@ -351,6 +351,22 @@ class EventTests(BaseTestCase):
for r, dt in results2: for r, dt in results2:
self.assertTrue(r) self.assertTrue(r)
def test_set_and_clear(self):
# Issue #13502: check that wait() returns true even when the event is
# cleared before the waiting thread is woken up.
evt = self.eventtype()
results = []
N = 5
def f():
results.append(evt.wait(1))
b = Bunch(f, N)
b.wait_for_started()
time.sleep(0.5)
evt.set()
evt.clear()
b.wait_for_finished()
self.assertEqual(results, [True] * N)
class ConditionTests(BaseTestCase): class ConditionTests(BaseTestCase):
""" """
......
...@@ -418,9 +418,10 @@ class _Event(_Verbose): ...@@ -418,9 +418,10 @@ class _Event(_Verbose):
def wait(self, timeout=None): def wait(self, timeout=None):
self._cond.acquire() self._cond.acquire()
try: try:
if not self._flag: signaled = self._flag
self._cond.wait(timeout) if not signaled:
return self._flag signaled = self._cond.wait(timeout)
return signaled
finally: finally:
self._cond.release() self._cond.release()
......
...@@ -97,6 +97,9 @@ Core and Builtins ...@@ -97,6 +97,9 @@ Core and Builtins
Library Library
------- -------
- Issue #13502: threading: Fix a race condition in Event.wait() that made it
return False when the event was set and cleared right after.
- Issue #12926: Fix a bug in tarfile's link extraction. - Issue #12926: Fix a bug in tarfile's link extraction.
- Issue #13696: Fix the 302 Relative URL Redirection problem. - Issue #13696: Fix the 302 Relative URL Redirection problem.
......
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