Skip to content

gh-162280: Require explicit opt-in for tests that change the system clock - #162309

Open
gabrielrbl wants to merge 5 commits into
python:mainfrom
gabrielrbl:gh-162280-settime
Open

gabrielrbl wants to merge 5 commits into
python:mainfrom
gabrielrbl:gh-162280-settime

Conversation

@gabrielrbl

@gabrielrbl gabrielrbl commented Nov 20, 2026 •

Copy link
Copy Markdown

Require -u settime for the test_time tests that write to CLOCK_REALTIME. test_monotonic_settime moves the system clock back by one hour, which can make tests running in parallel fail, such as the test_tarfile failure in the issue.

  • settime is not included in -u all. When the tests do not run under regrtest (python -m unittest, or running the file directly), test.support.is_resource_enabled() reports it as disabled.
  • The resource is checked when the test runs, not at import time. Both tests are marked thread_unsafe, so --parallel-threads runs them once.
  • test_monotonic_settime restores the clock if it is interrupted right after moving it back. A write that fails is not undone: PermissionError skips the test, another OSError is reported.
  • test_clock_settime now skips on PermissionError instead of passing. The check that setting CLOCK_MONOTONIC fails still runs by default.

Opting in still affects the whole machine. If a -j worker is killed while the clock is moved back (Ctrl+C, --timeout, --fail-fast), nothing can restore it, so the help of the resource now says to run these tests alone, without -j.

Tested on Linux x86-64 with debug builds, with and without the GIL: test_time, test_regrtest and test_support pass. I also ran them as root in a VM and counted clock writes with ftrace. With -u all, the old code wrote the clock 3 times per run and this change writes it 0 times. Next to a loop of the old test_monotonic_settime, the test_tarfile case from the issue failed 39 times out of 40, with the same one-hour difference. With this change it passed 41 times out of 41.

This should be backported to 3.14 and 3.15. Only the imports of test_regrtest.py conflict. With that resolved, I checked that both branches build and pass these tests.

I used an AI coding assistant to help prepare the patch and the checks, and reviewed the changes myself.

@python-cla-bot

python-cla-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Nov 20, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Nov 20, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

test_monotonic_settime moves the system clock back by one hour for a
moment. When tests run in parallel, other tests can observe the jump:
pythongh-162280 reports test_tarfile failing because extracted files got
timestamps an hour in the past.

Add a settime resource for the two test_time tests that write
CLOCK_REALTIME. It is not part of -u all, and it is disabled when the
tests are not run by regrtest. Restore the clock in a finally block
after the monotonic read. The check that setting CLOCK_MONOTONIC fails
moves to its own test and still runs by default.

Also fix the docstring of get_resource_value(), which repeated the one
of is_resource_enabled().
The requires_resource() decorator is evaluated when test_time is
imported. A process that imported the module once and ran it again
with other resources kept the first decision. Call support.requires()
inside both tests instead.

Mark both tests thread_unsafe so that --parallel-threads runs them
once, report a PermissionError in test_clock_settime as a skip, and
add a regrtest test that counts the clock writes for each set of
resources. Say in the help that these tests should run in isolation.
The try/finally block started after the write that moves the clock
back. A KeyboardInterrupt raised right after that write, for example
by Ctrl+C, left the system clock one hour behind. Move the write
inside the block. A write that fails with OSError did not change the
clock and is not undone.

Also test that the settime resource stays disabled when the tests are
run directly with unittest, document this in is_resource_enabled() and
add a NEWS entry for pythongh-162280.
Replace the interrupt regression test with a stateful fake clock and
check the outcome on every exit path: success, interrupted after the
write, permission denied and a failed write. The previous mocks
returned a constant time, so a restore that read the clock again
instead of using the saved value went unnoticed.
On Ctrl+C, --timeout or --fail-fast, the main process kills workers
with SIGKILL. If that happens while test_monotonic_settime has moved
the clock back, nothing can restore it. Say so in the help of the
settime resource.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36039346 | 📁 Comparing 90b659f against main (c0f3fcf)

  🔍 Preview build  

2 files changed
± library/test.html
± whatsnew/changelog.html

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