Skip to content

gh-153005: Use a monotonic clock for concurrent.interpreters Queue timeouts#154156

Open
fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-gh-153005-interpreters-queue-monotonic
Open

gh-153005: Use a monotonic clock for concurrent.interpreters Queue timeouts#154156
fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-gh-153005-interpreters-queue-monotonic

Conversation

@fedonman

Copy link
Copy Markdown
Contributor

concurrent.interpreters.Queue.get() and Queue.put() computed their timeout deadline from time.time(), the wall clock:

end = time.time() + timeout
...
if timeout is not None and time.time() >= end:
    raise

If the system clock is stepped (an NTP correction, a manual change) while a call is blocked, the deadline is off by the size of the adjustment, so the call can over- or under-wait. queue.Queue, which this queue is meant to be compatible with, uses time.monotonic() precisely to avoid that.

This computes the deadline and checks it against time.monotonic() instead, in both get() and put().

The issue also mentions that int(timeout) truncates a float timeout. That part was left out here on purpose: in the earlier PR #153006 a core reviewer asked to keep the clock change separate and to discuss the int() conversion (and the matching docs and other stdlib queues) first. This PR is only the monotonic-clock fix.

…eue timeouts

Queue.get() and Queue.put() computed their timeout deadline from
time.time(), the wall clock. If the system clock was stepped (NTP, a
manual change) while a call was blocked, the timeout could over- or
under-wait. queue.Queue uses time.monotonic() for the same reason.

Compute the deadline and check it against time.monotonic() instead.
@python-cla-bot

python-cla-bot Bot commented Jul 19, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant