From aa7b6bf11276f8f30ed33d102ef752dfb5780cd5 Mon Sep 17 00:00:00 2001 From: Gabriel Lobo Date: Fri, 20 Nov 2026 05:17:12 -0300 Subject: [PATCH 1/5] Require explicit opt-in for tests that change the system clock 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: gh-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(). --- Lib/test/libregrtest/cmdline.py | 3 +++ Lib/test/libregrtest/utils.py | 4 +++- Lib/test/support/__init__.py | 13 ++++++------- Lib/test/test_regrtest.py | 5 +++++ Lib/test/test_time.py | 17 ++++++++++++----- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py index 720b073e460127..f190146e5f6006 100644 --- a/Lib/test/libregrtest/cmdline.py +++ b/Lib/test/libregrtest/cmdline.py @@ -122,6 +122,9 @@ walltime - Long running but not CPU-bound tests. + settime - Allow tests to change the system clock. Must be enabled + explicitly, even when using `-u all`. + subprocess Run all tests for the subprocess module. urlfetch - It is okay to download files required on testing. diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 28e6d904fa28aa..c62ac9034000af 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -51,7 +51,9 @@ # - tzdata: while needed to validate fully test_datetime, it makes # test_datetime too slow (15-20 min on some buildbots) and so is disabled by # default (see bpo-30822). -RESOURCE_NAMES = ALL_RESOURCES + ('extralargefile', 'tzdata', 'xpickle', 'wantobjects') +# - settime: changing the system clock affects other processes. +RESOURCE_NAMES = ALL_RESOURCES + ('extralargefile', 'settime', 'tzdata', + 'xpickle', 'wantobjects') # Types for types hints diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 2cfe4b7f20e101..4e285dc5ee827b 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -324,16 +324,15 @@ def is_resource_enabled(resource): """Test whether a resource is enabled. Known resources are set by regrtest.py. If not running under regrtest.py, - all resources are assumed enabled unless use_resources has been set. + all resources except settime are assumed enabled unless use_resources + has been set. """ - return use_resources is None or resource in use_resources + if use_resources is None: + return resource != 'settime' + return resource in use_resources def get_resource_value(resource): - """Test whether a resource is enabled. - - Known resources are set by regrtest.py. If not running under regrtest.py, - all resources are assumed enabled unless use_resources has been set. - """ + """Return the configured resource value, or None if no value was set.""" if use_resources is None: return None return use_resources.get(resource) diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index cd2cd8a21e8ea7..e9429de56194fa 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -340,6 +340,11 @@ def test_use(self): self.checkError([opt, 'none=0'], 'invalid resource: none=0') self.checkError([opt, 'all,-gui=0'], 'invalid resource: -gui=0') + def test_settime_requires_opt_in(self): + self.assertNotIn('settime', self.parse_args([]).use_resources) + self.assertNotIn('settime', self.parse_args(['-u', 'all']).use_resources) + self.assertIn('settime', self.parse_args(['-u', 'settime']).use_resources) + def test_memlimit(self): for opt in '-M', '--memlimit': with self.subTest(opt=opt): diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 861076357e0019..291d00dd623434 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -143,6 +143,7 @@ def test_clock_getres(self): @unittest.skipUnless(hasattr(time, 'clock_settime'), 'need time.clock_settime()') + @support.requires_resource('settime') def test_clock_settime(self): t = time.clock_gettime(time.CLOCK_REALTIME) try: @@ -150,9 +151,12 @@ def test_clock_settime(self): except PermissionError: pass - if hasattr(time, 'CLOCK_MONOTONIC'): - self.assertRaises(OSError, - time.clock_settime, time.CLOCK_MONOTONIC, 0) + @unittest.skipUnless(hasattr(time, 'clock_settime') and + hasattr(time, 'CLOCK_MONOTONIC'), + 'need clock_settime() and CLOCK_MONOTONIC') + def test_clock_settime_monotonic(self): + with self.assertRaises(OSError): + time.clock_settime(time.CLOCK_MONOTONIC, 0) def test_large_timestamp_year(self): # 2147483648-06-29 00:00:00 UTC, away from local year boundaries. @@ -703,6 +707,7 @@ def test_thread_time(self): @unittest.skipUnless(hasattr(time, 'clock_settime'), 'need time.clock_settime') + @support.requires_resource('settime') def test_monotonic_settime(self): t1 = time.monotonic() realtime = time.clock_gettime(time.CLOCK_REALTIME) @@ -711,8 +716,10 @@ def test_monotonic_settime(self): time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) except PermissionError as err: self.skipTest(err) - t2 = time.monotonic() - time.clock_settime(time.CLOCK_REALTIME, realtime) + try: + t2 = time.monotonic() + finally: + time.clock_settime(time.CLOCK_REALTIME, realtime) # monotonic must not be affected by system clock updates self.assertGreaterEqual(t2, t1) From c34cc52c74c5ebf364886ae06aa19bbfb3f2fd47 Mon Sep 17 00:00:00 2001 From: Gabriel Lobo Date: Fri, 20 Nov 2026 05:37:34 -0300 Subject: [PATCH 2/5] Check the settime resource when the clock tests run 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. --- Lib/test/libregrtest/cmdline.py | 3 ++- Lib/test/test_regrtest.py | 19 +++++++++++++++++++ Lib/test/test_time.py | 10 ++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py index f190146e5f6006..0e016abc3cf45b 100644 --- a/Lib/test/libregrtest/cmdline.py +++ b/Lib/test/libregrtest/cmdline.py @@ -123,7 +123,8 @@ walltime - Long running but not CPU-bound tests. settime - Allow tests to change the system clock. Must be enabled - explicitly, even when using `-u all`. + explicitly, even when using `-u all`. Run these tests + in isolation as clock changes affect other processes. subprocess Run all tests for the subprocess module. diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index e9429de56194fa..6093ec9d53da8e 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -22,6 +22,7 @@ import tempfile import threading import textwrap +import time import types import unittest import unittest.mock @@ -907,6 +908,24 @@ def run_python(self, args, isolated=True, **kw): class CheckActualTests(BaseTestCase): + @unittest.skipUnless(hasattr(time, 'clock_settime'), 'requires clock_settime') + @support.thread_unsafe('changes the enabled test resources') + def test_system_clock_writes(self): + from test.libregrtest.single import _parallelize_tests + from test.test_time import TimeTestCase + + names = ('test_clock_settime', 'test_monotonic_settime') + for resources, expected in (({}, 0), ({'settime': None}, 3), ({}, 0)): + with (self.subTest(resources=resources), + unittest.mock.patch.object(support, 'use_resources', resources), + unittest.mock.patch.object(time, 'clock_settime') as setter): + suite = unittest.TestSuite(TimeTestCase(name) for name in names) + _parallelize_tests(suite, 2) + result = unittest.TestResult() + suite.run(result) + self.assertTrue(result.wasSuccessful(), result.errors + result.failures) + self.assertEqual(setter.call_count, expected) + def test_finds_expected_number_of_tests(self): """ Check that regrtest appears to find the expected set of tests. diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 291d00dd623434..1d5fa9e69c6c52 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -143,13 +143,14 @@ def test_clock_getres(self): @unittest.skipUnless(hasattr(time, 'clock_settime'), 'need time.clock_settime()') - @support.requires_resource('settime') + @support.thread_unsafe('sets the system clock') def test_clock_settime(self): + support.requires('settime') t = time.clock_gettime(time.CLOCK_REALTIME) try: time.clock_settime(time.CLOCK_REALTIME, t) - except PermissionError: - pass + except PermissionError as err: + self.skipTest(err) @unittest.skipUnless(hasattr(time, 'clock_settime') and hasattr(time, 'CLOCK_MONOTONIC'), @@ -707,8 +708,9 @@ def test_thread_time(self): @unittest.skipUnless(hasattr(time, 'clock_settime'), 'need time.clock_settime') - @support.requires_resource('settime') + @support.thread_unsafe('sets the system clock') def test_monotonic_settime(self): + support.requires('settime') t1 = time.monotonic() realtime = time.clock_gettime(time.CLOCK_REALTIME) # jump backward with an offset of 1 hour From f9348e704d86021ea27920b35c94cfa3889efdf2 Mon Sep 17 00:00:00 2001 From: Gabriel Lobo Date: Fri, 20 Nov 2026 06:00:19 -0300 Subject: [PATCH 3/5] Restore the clock when test_monotonic_settime is interrupted 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 gh-162280. --- Doc/library/test.rst | 2 +- Lib/test/test_regrtest.py | 21 ++++++++++++++++++- Lib/test/test_time.py | 16 +++++++++----- ...-11-20-08-50-55.gh-issue-162280.zWJny3.rst | 3 +++ 4 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2026-11-20-08-50-55.gh-issue-162280.zWJny3.rst diff --git a/Doc/library/test.rst b/Doc/library/test.rst index e6786a441c66f8..a2e5bdc64dfe61 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -490,7 +490,7 @@ The :mod:`!test.support` module defines the following functions: Return ``True`` if *resource* is enabled and available. The list of available resources is only set when :mod:`test.regrtest` is executing the - tests. + tests. Otherwise, all resources except ``settime`` are enabled. .. function:: get_resource_value(resource) diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index 6093ec9d53da8e..63bdebfda6cf48 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -915,7 +915,9 @@ def test_system_clock_writes(self): from test.test_time import TimeTestCase names = ('test_clock_settime', 'test_monotonic_settime') - for resources, expected in (({}, 0), ({'settime': None}, 3), ({}, 0)): + # None: the tests are run directly by unittest, outside regrtest. + cases = ((None, 0), ({}, 0), ({'settime': None}, 3), ({}, 0)) + for resources, expected in cases: with (self.subTest(resources=resources), unittest.mock.patch.object(support, 'use_resources', resources), unittest.mock.patch.object(time, 'clock_settime') as setter): @@ -926,6 +928,23 @@ def test_system_clock_writes(self): self.assertTrue(result.wasSuccessful(), result.errors + result.failures) self.assertEqual(setter.call_count, expected) + @unittest.skipUnless(hasattr(time, 'clock_settime'), 'requires clock_settime') + @support.thread_unsafe('changes the enabled test resources') + def test_system_clock_restored_after_interrupt(self): + from test.test_time import TimeTestCase + + # A signal can arrive after the clock changed, before the setter returns. + with (unittest.mock.patch.object(support, 'use_resources', {'settime': None}), + unittest.mock.patch.object(time, 'clock_gettime', return_value=7200), + unittest.mock.patch.object(time, 'clock_settime', + side_effect=[KeyboardInterrupt, None]) as setter): + with self.assertRaises(KeyboardInterrupt): + TimeTestCase('test_monotonic_settime').test_monotonic_settime() + self.assertEqual(setter.call_args_list, [ + unittest.mock.call(time.CLOCK_REALTIME, 3600), + unittest.mock.call(time.CLOCK_REALTIME, 7200), + ]) + def test_finds_expected_number_of_tests(self): """ Check that regrtest appears to find the expected set of tests. diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 1d5fa9e69c6c52..de500c31cd3f49 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -714,14 +714,20 @@ def test_monotonic_settime(self): t1 = time.monotonic() realtime = time.clock_gettime(time.CLOCK_REALTIME) # jump backward with an offset of 1 hour + restore_clock = True try: - time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) - except PermissionError as err: - self.skipTest(err) - try: + try: + time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) + except OSError as err: + # A failed system call did not change the clock. + restore_clock = False + if isinstance(err, PermissionError): + self.skipTest(err) + raise t2 = time.monotonic() finally: - time.clock_settime(time.CLOCK_REALTIME, realtime) + if restore_clock: + time.clock_settime(time.CLOCK_REALTIME, realtime) # monotonic must not be affected by system clock updates self.assertGreaterEqual(t2, t1) diff --git a/Misc/NEWS.d/next/Tests/2026-11-20-08-50-55.gh-issue-162280.zWJny3.rst b/Misc/NEWS.d/next/Tests/2026-11-20-08-50-55.gh-issue-162280.zWJny3.rst new file mode 100644 index 00000000000000..f1a92d264eb303 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-11-20-08-50-55.gh-issue-162280.zWJny3.rst @@ -0,0 +1,3 @@ +Tests in :mod:`!test.test_time` that change the system clock now require the +``settime`` resource, which is not enabled by ``-u all``. Moving the clock +back could make tests running in parallel fail. Patch by Gabriel Lobo. From 41d99a40b917c091796b27a474e1c80427be297d Mon Sep 17 00:00:00 2001 From: Gabriel Lobo Date: Fri, 20 Nov 2026 06:16:22 -0300 Subject: [PATCH 4/5] Check that test_monotonic_settime always restores the clock 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. --- Lib/test/test_regrtest.py | 49 +++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index 63bdebfda6cf48..d5205ed3dce418 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -930,20 +930,45 @@ def test_system_clock_writes(self): @unittest.skipUnless(hasattr(time, 'clock_settime'), 'requires clock_settime') @support.thread_unsafe('changes the enabled test resources') - def test_system_clock_restored_after_interrupt(self): + def test_system_clock_restored(self): from test.test_time import TimeTestCase - # A signal can arrive after the clock changed, before the setter returns. - with (unittest.mock.patch.object(support, 'use_resources', {'settime': None}), - unittest.mock.patch.object(time, 'clock_gettime', return_value=7200), - unittest.mock.patch.object(time, 'clock_settime', - side_effect=[KeyboardInterrupt, None]) as setter): - with self.assertRaises(KeyboardInterrupt): - TimeTestCase('test_monotonic_settime').test_monotonic_settime() - self.assertEqual(setter.call_args_list, [ - unittest.mock.call(time.CLOCK_REALTIME, 3600), - unittest.mock.call(time.CLOCK_REALTIME, 7200), - ]) + # Each step of clock_settime(): (exception raised, clock changed). + cases = ( + ('success', [(None, True), (None, True)], None), + # A signal can arrive after the clock changed, before the setter + # returns. + ('interrupted', [(KeyboardInterrupt, True), (None, True)], + KeyboardInterrupt), + ('denied', [(PermissionError, False)], unittest.SkipTest), + ('failed', [(OSError, False)], OSError), + ) + for name, steps, expected in cases: + clock = 7200 + writes = 0 + + def clock_settime(clock_id, value): + nonlocal clock, writes + self.assertLess(writes, len(steps), 'unexpected clock write') + exc, changed = steps[writes] + writes += 1 + if changed: + clock = value + if exc is not None: + raise exc + + with (self.subTest(name), + unittest.mock.patch.object(support, 'use_resources', {'settime': None}), + unittest.mock.patch.object(time, 'clock_gettime', lambda clock_id: clock), + unittest.mock.patch.object(time, 'clock_settime', clock_settime)): + test = TimeTestCase('test_monotonic_settime') + if expected is None: + test.test_monotonic_settime() + else: + with self.assertRaises(expected): + test.test_monotonic_settime() + self.assertEqual(writes, len(steps)) + self.assertEqual(clock, 7200) def test_finds_expected_number_of_tests(self): """ From 90b659f151c9ef7ba4eb0cf376f2147419c89629 Mon Sep 17 00:00:00 2001 From: Gabriel Lobo Date: Fri, 20 Nov 2026 06:56:20 -0300 Subject: [PATCH 5/5] Advise running the settime tests without -j 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. --- Lib/test/libregrtest/cmdline.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py index 0e016abc3cf45b..85a6e9169369c5 100644 --- a/Lib/test/libregrtest/cmdline.py +++ b/Lib/test/libregrtest/cmdline.py @@ -124,7 +124,8 @@ settime - Allow tests to change the system clock. Must be enabled explicitly, even when using `-u all`. Run these tests - in isolation as clock changes affect other processes. + alone, without -j: clock changes affect other processes + and a killed worker cannot restore the clock. subprocess Run all tests for the subprocess module.