Skip to content

Commit 38443c9

Browse files
cmaloneyclaude
authored andcommitted
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 3fd6401) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 932c1b7 commit 38443c9

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

‎Lib/test/test_signal.py‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,17 @@ def setsig(self, signum, handler):
12261226
old_handler = signal.signal(signum, handler)
12271227
self.addCleanup(signal.signal, signum, old_handler)
12281228

1229+
def wait_for_signals(self, sigs, expected_sigs, deadline):
1230+
# Wait for the handlers to run, so that pending signals are not
1231+
# coalesced.
1232+
#
1233+
# Signals normally arrive within microseconds, so poll with a short
1234+
# sleep rather than sleeping_retry() whose 10 ms minimum delay
1235+
# dominates the runtime (and is stretched by timer coalescing on
1236+
# macOS).
1237+
while len(sigs) < expected_sigs and time.monotonic() < deadline:
1238+
time.sleep(1e-5)
1239+
12291240
def measure_itimer_resolution(self):
12301241
N = 20
12311242
times = []
@@ -1297,14 +1308,11 @@ def second_handler(signum=None, frame=None):
12971308
while expected_sigs < N:
12981309
os.kill(os.getpid(), signal.SIGPROF)
12991310
expected_sigs += 1
1300-
# Wait for handlers to run to avoid signal coalescing
1301-
while len(sigs) < expected_sigs and time.monotonic() < deadline:
1302-
time.sleep(1e-5)
1311+
self.wait_for_signals(sigs, expected_sigs, deadline)
13031312

13041313
os.kill(os.getpid(), signal.SIGUSR1)
13051314
expected_sigs += 1
1306-
while len(sigs) < expected_sigs and time.monotonic() < deadline:
1307-
time.sleep(1e-5)
1315+
self.wait_for_signals(sigs, expected_sigs, deadline)
13081316

13091317
# All ITIMER_REAL signals should have been delivered to the
13101318
# Python handler
@@ -1329,17 +1337,16 @@ def handler(signum, frame):
13291337
self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL
13301338

13311339
expected_sigs = 0
1340+
deadline = time.monotonic() + support.SHORT_TIMEOUT
1341+
13321342
while expected_sigs < N:
13331343
# Hopefully the SIGALRM will be received somewhere during
13341344
# initial processing of SIGUSR2.
13351345
signal.setitimer(signal.ITIMER_REAL, 1e-6 + random.random() * 1e-5)
13361346
os.kill(os.getpid(), signal.SIGUSR2)
13371347

13381348
expected_sigs += 2
1339-
# Wait for handlers to run to avoid signal coalescing
1340-
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
1341-
if len(sigs) >= expected_sigs:
1342-
break
1349+
self.wait_for_signals(sigs, expected_sigs, deadline)
13431350

13441351
# All ITIMER_REAL signals should have been delivered to the
13451352
# Python handler

0 commit comments

Comments
 (0)