Skip to content
Open
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
8 changes: 4 additions & 4 deletions Lib/test/support/interpreters/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ def put(self, obj, timeout=None, *,
timeout = int(timeout)
if timeout < 0:
raise ValueError(f'timeout value must be non-negative')
end = time.time() + timeout
end = time.monotonic() + timeout
if fmt is _PICKLED:
obj = pickle.dumps(obj)
while True:
try:
_queues.put(self._id, obj, fmt, unboundop)
except QueueFull as exc:
if timeout is not None and time.time() >= end:
if timeout is not None and time.monotonic() >= end:
raise # re-raise
time.sleep(_delay)
else:
Expand Down Expand Up @@ -271,12 +271,12 @@ def get(self, timeout=None, *,
timeout = int(timeout)
if timeout < 0:
raise ValueError(f'timeout value must be non-negative')
end = time.time() + timeout
end = time.monotonic() + timeout
while True:
try:
obj, fmt, unboundop = _queues.get(self._id)
except QueueEmpty as exc:
if timeout is not None and time.time() >= end:
if timeout is not None and time.monotonic() >= end:
raise # re-raise
time.sleep(_delay)
else:
Expand Down
16 changes: 15 additions & 1 deletion Lib/test/test_interpreters/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import pickle
import threading
from textwrap import dedent
import unittest
import time
import unittest
from unittest import mock

from test.support import import_helper, Py_DEBUG
# Raise SkipTest if subinterpreters not supported.
Expand Down Expand Up @@ -383,6 +384,19 @@ def test_get_timeout(self):
with self.assertRaises(queues.QueueEmpty):
queue.get(timeout=0.1)

def test_timeout_uses_monotonic_clock(self):
# gh-153005: the deadline must be computed from the monotonic clock,
# since the wall clock can be adjusted while the call is blocked.
queue = queues.create(1)
with mock.patch.object(queues, 'time', wraps=time) as fake_time:
with self.assertRaises(queues.QueueEmpty):
queue.get(timeout=0)
queue.put(None)
with self.assertRaises(queues.QueueFull):
queue.put(None, timeout=0)
fake_time.monotonic.assert_called()
fake_time.time.assert_not_called()

def test_get_nowait(self):
queue = queues.create()
with self.assertRaises(queues.QueueEmpty):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:meth:`!concurrent.interpreters.Queue.get` and
:meth:`!concurrent.interpreters.Queue.put` now compute their ``timeout``
deadline from :func:`time.monotonic` instead of the wall clock, so adjusting
the system clock during the call no longer makes them over- or under-wait.
Loading