[Python-checkins] cpython (3.2): Issue #13502: threading: Fix a race condition in Event.wait() that made it
charles-francois.natali
python-checkins at python.org
Sat Jan 7 18:27:06 CET 2012
http://hg.python.org/cpython/rev/eb39d862a250
changeset: 74292:eb39d862a250
branch: 3.2
parent: 74289:1d8286235da5
user: Charles-François Natali <neologix at free.fr>
date: Sat Jan 07 18:24:56 2012 +0100
summary:
Issue #13502: threading: Fix a race condition in Event.wait() that made it
return False when the event was set and cleared right after.
files:
Doc/library/threading.rst | 6 ++++--
Lib/test/lock_tests.py | 16 ++++++++++++++++
Lib/threading.py | 7 ++++---
Misc/NEWS | 3 +++
4 files changed, 27 insertions(+), 5 deletions(-)
diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst
--- a/Doc/library/threading.rst
+++ b/Doc/library/threading.rst
@@ -782,8 +782,10 @@
floating point number specifying a timeout for the operation in seconds
(or fractions thereof).
- This method returns the internal flag on exit, so it will always return
- ``True`` except if a timeout is given and the operation times out.
+ This method returns true if and only if the internal flag has been set to
+ 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
Previously, the method always returned ``None``.
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py
--- a/Lib/test/lock_tests.py
+++ b/Lib/test/lock_tests.py
@@ -351,6 +351,22 @@
for r, dt in results2:
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):
"""
diff --git a/Lib/threading.py b/Lib/threading.py
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -418,9 +418,10 @@
def wait(self, timeout=None):
self._cond.acquire()
try:
- if not self._flag:
- self._cond.wait(timeout)
- return self._flag
+ signaled = self._flag
+ if not signaled:
+ signaled = self._cond.wait(timeout)
+ return signaled
finally:
self._cond.release()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -97,6 +97,9 @@
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 #13696: Fix the 302 Relative URL Redirection problem.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list