Skip to content

Commit 2ab2afd

Browse files
authored
bpo-35426: Eliminate race condition in test_interprocess_signal (GH-11087)
The test only except SIGUSR1Exception inside wait_signal(), but the signal can be sent during subprocess_send_signal() call.
1 parent a932d0b commit 2ab2afd

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

Lib/test/signalinterproctester.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,19 @@ def sigusr1_handler(self, signum, frame):
2121
self.got_signals['SIGUSR1'] += 1
2222
raise SIGUSR1Exception
2323

24-
def wait_signal(self, child, signame, exc_class=None):
25-
try:
26-
if child is not None:
27-
# This wait should be interrupted by exc_class
28-
# (if set)
29-
child.wait()
30-
31-
timeout = 10.0
32-
deadline = time.monotonic() + timeout
33-
34-
while time.monotonic() < deadline:
35-
if self.got_signals[signame]:
36-
return
37-
signal.pause()
38-
except BaseException as exc:
39-
if exc_class is not None and isinstance(exc, exc_class):
40-
# got the expected exception
24+
def wait_signal(self, child, signame):
25+
if child is not None:
26+
# This wait should be interrupted by exc_class
27+
# (if set)
28+
child.wait()
29+
30+
timeout = 10.0
31+
deadline = time.monotonic() + timeout
32+
33+
while time.monotonic() < deadline:
34+
if self.got_signals[signame]:
4135
return
42-
raise
36+
signal.pause()
4337

4438
self.fail('signal %s not received after %s seconds'
4539
% (signame, timeout))
@@ -65,8 +59,9 @@ def test_interprocess_signal(self):
6559
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 0,
6660
'SIGALRM': 0})
6761

68-
with self.subprocess_send_signal(pid, "SIGUSR1") as child:
69-
self.wait_signal(child, 'SIGUSR1', SIGUSR1Exception)
62+
with self.assertRaises(SIGUSR1Exception):
63+
with self.subprocess_send_signal(pid, "SIGUSR1") as child:
64+
self.wait_signal(child, 'SIGUSR1')
7065
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1,
7166
'SIGALRM': 0})
7267

@@ -75,8 +70,9 @@ def test_interprocess_signal(self):
7570
child.wait()
7671

7772
try:
78-
signal.alarm(1)
79-
self.wait_signal(None, 'SIGALRM', KeyboardInterrupt)
73+
with self.assertRaises(KeyboardInterrupt):
74+
signal.alarm(1)
75+
self.wait_signal(None, 'SIGALRM')
8076
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1,
8177
'SIGALRM': 0})
8278
finally:

0 commit comments

Comments
 (0)