]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43406: Fix test_signal.test_stress_modifying_handlers() (GH-24815)
authorVictor Stinner <vstinner@python.org>
Wed, 10 Mar 2021 14:26:45 +0000 (15:26 +0100)
committerGitHub <noreply@github.com>
Wed, 10 Mar 2021 14:26:45 +0000 (15:26 +0100)
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.

Lib/test/test_signal.py

index c9de4a4e015a39cdf4b319ec8a6bab1e459a3e44..f973d4fe08be319e36c874512cad16c8c3cae082 100644 (file)
@@ -1280,11 +1280,16 @@ class StressTest(unittest.TestCase):
 
         old_handler = signal.signal(signum, custom_handler)
         self.addCleanup(signal.signal, signum, old_handler)
+
         t = threading.Thread(target=set_interrupts)
-        t.start()
         try:
+            ignored = False
             with support.catch_unraisable_exception() as cm:
+                t.start()
                 cycle_handlers()
+                do_stop = True
+                t.join()
+
                 if cm.unraisable is not None:
                     # An unraisable exception may be printed out when
                     # a signal is ignored due to the aforementioned
@@ -1293,8 +1298,13 @@ class StressTest(unittest.TestCase):
                     self.assertIn(
                         f"Signal {signum} ignored due to race condition",
                         str(cm.unraisable.exc_value))
-            # Sanity check that some signals were received, but not all
-            self.assertGreater(num_received_signals, 0)
+                    ignored = True
+
+            # bpo-43406: Even if it is unlikely, it's technically possible that
+            # all signals were ignored because of race conditions.
+            if not ignored:
+                # Sanity check that some signals were received, but not all
+                self.assertGreater(num_received_signals, 0)
             self.assertLess(num_received_signals, num_sent_signals)
         finally:
             do_stop = True