Repository navigation
gh-162280: Require explicit opt-in for tests that change the system clock - #162309
Open
gabrielrbl wants to merge 5 commits into
Open
gabrielrbl wants to merge 5 commits into
gabrielrbl wants to merge 5 commits into
Conversation
gabrielrbl
requested review from
StanFromIreland and
pganssle
as code owners
November 20, 2026 08:19
|
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 |
|
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 |
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.
gabrielrbl
force-pushed
the
gh-162280-settime
branch
from
November 20, 2026 10:42
41a5289 to
90b659f
Compare
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Require
-u settimefor thetest_timetests that write toCLOCK_REALTIME.test_monotonic_settimemoves the system clock back by one hour, which can make tests running in parallel fail, such as thetest_tarfilefailure in the issue.settimeis 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.thread_unsafe, so--parallel-threadsruns them once.test_monotonic_settimerestores the clock if it is interrupted right after moving it back. A write that fails is not undone:PermissionErrorskips the test, anotherOSErroris reported.test_clock_settimenow skips onPermissionErrorinstead of passing. The check that settingCLOCK_MONOTONICfails still runs by default.Opting in still affects the whole machine. If a
-jworker 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_regrtestandtest_supportpass. 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 oldtest_monotonic_settime, thetest_tarfilecase 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.pyconflict. 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.