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

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

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 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

_______________________________________________
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