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/concurrent/interpreters/_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ def put(self, obj, block=True, 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:
_queues.put(self._id, obj, unboundop)
except QueueFull:
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 @@ -255,12 +255,12 @@ def get(self, block=True, 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, unboundop = _queues.get(self._id)
except QueueEmpty:
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
23 changes: 23 additions & 0 deletions Lib/test/test_interpreters/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import threading
from textwrap import dedent
import unittest
from unittest import mock

from test.support import import_helper, Py_DEBUG
# Raise SkipTest if subinterpreters not supported.
Expand Down Expand Up @@ -354,6 +355,28 @@ def test_get_timeout(self):
with self.assertRaises(queues.QueueEmpty):
queue.get(HUGE_TIMEOUT, 0.1)

def test_timeout_uses_monotonic_clock(self):
# gh-153005: the timeout deadline must be based on time.monotonic(),
# not the wall clock, so adjusting the system clock during the call
# cannot make get()/put() over- or under-wait.
class FakeClock:
def __init__(self):
self.now = 1000.0
def monotonic(self):
return self.now
def sleep(self, delay):
self.now += delay
def time(self):
raise AssertionError('the wall clock must not be used')

with mock.patch.object(queues, 'time', FakeClock()):
queue = queues.create(1)
with self.assertRaises(queues.QueueEmpty):
queue.get(timeout=1, _delay=0.4)
queue.put(None)
with self.assertRaises(queues.QueueFull):
queue.put(None, timeout=1, _delay=0.4)

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