Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/lock_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ def f():
for r, dt in results2:
self.assertTrue(r)

def test_nan_timeout(self):
evt = self.eventtype()
self.assertRaises(ValueError, evt.wait, float('nan'))

def test_set_and_clear(self):
# gh-57711: check that wait() returns true even when the event is
# cleared before the waiting thread is woken up.
Expand Down Expand Up @@ -760,6 +764,14 @@ def f():
# In practice, this implementation has no spurious wakeups.
self.assertFalse(result)

def test_nan_timeout(self):
cond = self.condtype()
nan = float('nan')
with cond:
self.assertRaises(ValueError, cond.wait, nan)
self.assertRaises(ValueError, cond.wait_for, lambda: False, nan)
self.assertEqual(len(cond._waiters), 0)

def test_waitfor(self):
cond = self.condtype()
state = 0
Expand Down
7 changes: 4 additions & 3 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,11 @@ def wait(self, timeout=None):
waiter.acquire()
gotit = True
else:
if timeout > 0:
gotit = waiter.acquire(True, timeout)
else:
if timeout <= 0:
gotit = waiter.acquire(False)
else:
# A NaN timeout is rejected by acquire().
gotit = waiter.acquire(True, timeout)
return gotit
finally:
self._acquire_restore(saved_state)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Raise :exc:`ValueError` for a NaN *timeout* in
:meth:`threading.Condition.wait` and the blocking calls built on it, such as
:meth:`threading.Event.wait`, :meth:`threading.Semaphore.acquire` and
:meth:`queue.Queue.get`, instead of returning at once or looping without ever
timing out.
Loading