Skip to content

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

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
fedonman:fix-gh-153005-interpreters-queue-monotonic
Aug 17, 2026
Merged

gh-153005: Use a monotonic clock for concurrent.interpreters Queue timeouts#154156
serhiy-storchaka merged 3 commits 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

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 16, 2026

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. But could the test be simplified?

@fedonman

Copy link
Copy Markdown
Contributor Author

Thanks, done.

@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) August 17, 2026 18:50
@serhiy-storchaka
serhiy-storchaka merged commit b94b9c8 into python:main Aug 17, 2026
51 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @fedonman for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @fedonman and @serhiy-storchaka, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker b94b9c8886a987a324a677b5fda5bef27f15cb14 3.14

@miss-islington-app

Copy link
Copy Markdown

Sorry, @fedonman and @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker b94b9c8886a987a324a677b5fda5bef27f15cb14 3.13

@bedevere-app

bedevere-app Bot commented Aug 17, 2026

Copy link
Copy Markdown

GH-155967 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 17, 2026
brittanyrey pushed a commit to brittanyrey/cpython that referenced this pull request Aug 18, 2026
…eue timeouts (pythonGH-154156)

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.
@bedevere-app

bedevere-app Bot commented Aug 18, 2026

Copy link
Copy Markdown

GH-156018 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 18, 2026
@bedevere-app

bedevere-app Bot commented Aug 18, 2026

Copy link
Copy Markdown

GH-156019 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants