Skip to content

Commit 8e20d45

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 a782785 commit 8e20d45

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
@@ -1227,6 +1227,17 @@ def setsig(self, signum, handler):
12271227
old_handler = signal.signal(signum, handler)
12281228
self.addCleanup(signal.signal, signum, old_handler)
12291229

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

13051314
os.kill(os.getpid(), signal.SIGUSR1)
13061315
expected_sigs += 1
1307-
while len(sigs) < expected_sigs and time.monotonic() < deadline:
1308-
time.sleep(1e-5)
1316+
self.wait_for_signals(sigs, expected_sigs, deadline)
13091317

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

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

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

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

0 commit comments

Comments
 (0)