Skip to content

Commit 3fd6401

Browse files
cmaloneyclaude
andauthored
gh-162364: Speed up test_signal StressTest.test_stress_delivery_simultaneous (#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. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 83d2ed4 commit 3fd6401

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
@@ -1231,6 +1231,17 @@ def setsig(self, signum, handler):
12311231
old_handler = signal.signal(signum, handler)
12321232
self.addCleanup(signal.signal, signum, old_handler)
12331233

1234+
def wait_for_signals(self, sigs, expected_sigs, deadline):
1235+
# Wait for the handlers to run, so that pending signals are not
1236+
# coalesced.
1237+
#
1238+
# Signals normally arrive within microseconds, so poll with a short
1239+
# sleep rather than sleeping_retry() whose 10 ms minimum delay
1240+
# dominates the runtime (and is stretched by timer coalescing on
1241+
# macOS).
1242+
while len(sigs) < expected_sigs and time.monotonic() < deadline:
1243+
time.sleep(1e-5)
1244+
12341245
def measure_itimer_resolution(self):
12351246
N = 20
12361247
times = []
@@ -1302,14 +1313,11 @@ def second_handler(signum=None, frame=None):
13021313
while expected_sigs < N:
13031314
os.kill(os.getpid(), signal.SIGPROF)
13041315
expected_sigs += 1
1305-
# Wait for handlers to run to avoid signal coalescing
1306-
while len(sigs) < expected_sigs and time.monotonic() < deadline:
1307-
time.sleep(1e-5)
1316+
self.wait_for_signals(sigs, expected_sigs, deadline)
13081317

13091318
os.kill(os.getpid(), signal.SIGUSR1)
13101319
expected_sigs += 1
1311-
while len(sigs) < expected_sigs and time.monotonic() < deadline:
1312-
time.sleep(1e-5)
1320+
self.wait_for_signals(sigs, expected_sigs, deadline)
13131321

13141322
# All ITIMER_REAL signals should have been delivered to the
13151323
# Python handler
@@ -1334,17 +1342,16 @@ def handler(signum, frame):
13341342
self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL
13351343

13361344
expected_sigs = 0
1345+
deadline = time.monotonic() + support.SHORT_TIMEOUT
1346+
13371347
while expected_sigs < N:
13381348
# Hopefully the SIGALRM will be received somewhere during
13391349
# initial processing of SIGUSR2.
13401350
signal.setitimer(signal.ITIMER_REAL, 1e-6 + random.random() * 1e-5)
13411351
os.kill(os.getpid(), signal.SIGUSR2)
13421352

13431353
expected_sigs += 2
1344-
# Wait for handlers to run to avoid signal coalescing
1345-
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
1346-
if len(sigs) >= expected_sigs:
1347-
break
1354+
self.wait_for_signals(sigs, expected_sigs, deadline)
13481355

13491356
# All ITIMER_REAL signals should have been delivered to the
13501357
# Python handler

0 commit comments

Comments
 (0)