Skip to content

Commit 531f2eb

Browse files
bpo-43406: Fix test_signal.test_stress_modifying_handlers() (GH-24815) (GH-24817)
Fix a race condition of test_stress_modifying_handlers() of test_signal: only raise signals while we are in the catch_unraisable_exception() context manager. Moreover, don't check if we received at least one signal if at least one signal got ignored. (cherry picked from commit 1fa17e8) Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent f814675 commit 531f2eb

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Lib/test/test_signal.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,11 +1272,16 @@ def cycle_handlers():
12721272

12731273
old_handler = signal.signal(signum, custom_handler)
12741274
self.addCleanup(signal.signal, signum, old_handler)
1275+
12751276
t = threading.Thread(target=set_interrupts)
1276-
t.start()
12771277
try:
1278+
ignored = False
12781279
with support.catch_unraisable_exception() as cm:
1280+
t.start()
12791281
cycle_handlers()
1282+
do_stop = True
1283+
t.join()
1284+
12801285
if cm.unraisable is not None:
12811286
# An unraisable exception may be printed out when
12821287
# a signal is ignored due to the aforementioned
@@ -1285,8 +1290,13 @@ def cycle_handlers():
12851290
self.assertIn(
12861291
f"Signal {signum} ignored due to race condition",
12871292
str(cm.unraisable.exc_value))
1288-
# Sanity check that some signals were received, but not all
1289-
self.assertGreater(num_received_signals, 0)
1293+
ignored = True
1294+
1295+
# bpo-43406: Even if it is unlikely, it's technically possible that
1296+
# all signals were ignored because of race conditions.
1297+
if not ignored:
1298+
# Sanity check that some signals were received, but not all
1299+
self.assertGreater(num_received_signals, 0)
12901300
self.assertLess(num_received_signals, num_sent_signals)
12911301
finally:
12921302
do_stop = True

0 commit comments

Comments
 (0)