From 38443c98cfd3741d31c7e5d67862b30e3143e487 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Sat, 21 Nov 2026 12:39:11 -0800 Subject: [PATCH] gh-162364: Speed up test_signal StressTest.test_stress_delivery_simultaneous (GH-162366) 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. (cherry picked from commit 3fd6401aff53275055378614e33d2f0b950500c6) Co-authored-by: Cody Maloney Co-authored-by: Claude Opus 5.5 --- Lib/test/test_signal.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 6d62d6119255a8..e9fcfb2149a77b 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -1226,6 +1226,17 @@ 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 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 (and is stretched by timer coalescing on + # macOS). + while len(sigs) < expected_sigs and time.monotonic() < deadline: + time.sleep(1e-5) + def measure_itimer_resolution(self): N = 20 times = [] @@ -1297,14 +1308,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 @@ -1329,6 +1337,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. @@ -1336,10 +1346,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