https://github.com/python/cpython/commit/e2517533742e0f78484dde8b71ba13c62c90da32
commit: e2517533742e0f78484dde8b71ba13c62c90da32
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: cmaloney <[email protected]>
date: 2026-11-21T13:29:14-08:00
summary:

[3.15] gh-162364: Speed up test_signal 
StressTest.test_stress_delivery_simultaneous (GH-162366) (#162401)

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 <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>

files:
M Lib/test/test_signal.py

diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py
index d6cc22558ec4fa..9b4f1e1a25c7ca 100644
--- a/Lib/test/test_signal.py
+++ b/Lib/test/test_signal.py
@@ -1227,6 +1227,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 = []
@@ -1298,14 +1309,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
@@ -1330,6 +1338,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.
@@ -1337,10 +1347,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

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to