From 6d8e1e4d8a5f48b5499cf106cca38b8966c7d125 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Fri, 20 Nov 2026 23:53:30 -0800 Subject: [PATCH 1/2] gh-162364: Speed up test_signal StressTest.test_stress_delivery_simultaneous Reuse the short-sleep polling loop from test_stress_delivery_dependent. The 10 ms minimum sleep of sleeping_retry() dominated the test's runtime, especially under macOS background QoS where timer coalescing stretches each sleep. Co-Authored-By: Claude Opus 5.5 --- Lib/test/test_signal.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 2cad18a69ff7c02..0e6da09c21da240 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -1231,6 +1231,15 @@ def setsig(self, signum, handler): old_handler = signal.signal(signum, handler) self.addCleanup(signal.signal, signum, old_handler) + def wait_for_signals(self, sigs, expected_sigs, deadline): + # Wait for handlers to run and try to avoid timer coalescing on macOS. + # + # Signals normally arrive within microseconds, so poll with a short + # sleep rather than sleeping_retry() whose 10 ms minimum delay dominates + # the runtime. + while len(sigs) < expected_sigs and time.monotonic() < deadline: + time.sleep(1e-5) + def measure_itimer_resolution(self): N = 20 times = [] @@ -1302,14 +1311,11 @@ def second_handler(signum=None, frame=None): while expected_sigs < N: os.kill(os.getpid(), signal.SIGPROF) expected_sigs += 1 - # Wait for handlers to run to avoid signal coalescing - while len(sigs) < expected_sigs and time.monotonic() < deadline: - time.sleep(1e-5) + self.wait_for_signals(sigs, expected_sigs, deadline) os.kill(os.getpid(), signal.SIGUSR1) expected_sigs += 1 - while len(sigs) < expected_sigs and time.monotonic() < deadline: - time.sleep(1e-5) + self.wait_for_signals(sigs, expected_sigs, deadline) # All ITIMER_REAL signals should have been delivered to the # Python handler @@ -1334,6 +1340,8 @@ def handler(signum, frame): self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL expected_sigs = 0 + deadline = time.monotonic() + support.SHORT_TIMEOUT + while expected_sigs < N: # Hopefully the SIGALRM will be received somewhere during # initial processing of SIGUSR2. @@ -1341,10 +1349,7 @@ def handler(signum, frame): os.kill(os.getpid(), signal.SIGUSR2) expected_sigs += 2 - # Wait for handlers to run to avoid signal coalescing - for _ in support.sleeping_retry(support.SHORT_TIMEOUT): - if len(sigs) >= expected_sigs: - break + self.wait_for_signals(sigs, expected_sigs, deadline) # All ITIMER_REAL signals should have been delivered to the # Python handler From d8f01220b9734093f81d8cc40ab164fce5c4b884 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Sat, 21 Nov 2026 00:34:49 -0800 Subject: [PATCH 2/2] gh-162364: Clarify why test_signal waits for signal handlers The wait avoids coalescing of pending signals; timer coalescing on macOS is what made the old 10 ms sleep so slow. Co-Authored-By: Claude Opus 5.5 --- Lib/test/test_signal.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 0e6da09c21da240..a8f82faee775533 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -1232,11 +1232,13 @@ def setsig(self, signum, handler): self.addCleanup(signal.signal, signum, old_handler) def wait_for_signals(self, sigs, expected_sigs, deadline): - # Wait for handlers to run and try to avoid timer coalescing on macOS. + # Wait for the handlers to run, so that pending signals are not + # coalesced. # # Signals normally arrive within microseconds, so poll with a short - # sleep rather than sleeping_retry() whose 10 ms minimum delay dominates - # the runtime. + # sleep rather than sleeping_retry() whose 10 ms minimum delay + # dominates the runtime (and is stretched by timer coalescing on + # macOS). while len(sigs) < expected_sigs and time.monotonic() < deadline: time.sleep(1e-5)