mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
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.
This commit is contained in:
parent
a932d0b496
commit
2ab2afd387
1 changed files with 16 additions and 20 deletions
|
@ -21,25 +21,19 @@ class InterProcessSignalTests(unittest.TestCase):
|
||||||
self.got_signals['SIGUSR1'] += 1
|
self.got_signals['SIGUSR1'] += 1
|
||||||
raise SIGUSR1Exception
|
raise SIGUSR1Exception
|
||||||
|
|
||||||
def wait_signal(self, child, signame, exc_class=None):
|
def wait_signal(self, child, signame):
|
||||||
try:
|
if child is not None:
|
||||||
if child is not None:
|
# This wait should be interrupted by exc_class
|
||||||
# This wait should be interrupted by exc_class
|
# (if set)
|
||||||
# (if set)
|
child.wait()
|
||||||
child.wait()
|
|
||||||
|
|
||||||
timeout = 10.0
|
timeout = 10.0
|
||||||
deadline = time.monotonic() + timeout
|
deadline = time.monotonic() + timeout
|
||||||
|
|
||||||
while time.monotonic() < deadline:
|
while time.monotonic() < deadline:
|
||||||
if self.got_signals[signame]:
|
if self.got_signals[signame]:
|
||||||
return
|
|
||||||
signal.pause()
|
|
||||||
except BaseException as exc:
|
|
||||||
if exc_class is not None and isinstance(exc, exc_class):
|
|
||||||
# got the expected exception
|
|
||||||
return
|
return
|
||||||
raise
|
signal.pause()
|
||||||
|
|
||||||
self.fail('signal %s not received after %s seconds'
|
self.fail('signal %s not received after %s seconds'
|
||||||
% (signame, timeout))
|
% (signame, timeout))
|
||||||
|
@ -65,8 +59,9 @@ class InterProcessSignalTests(unittest.TestCase):
|
||||||
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 0,
|
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 0,
|
||||||
'SIGALRM': 0})
|
'SIGALRM': 0})
|
||||||
|
|
||||||
with self.subprocess_send_signal(pid, "SIGUSR1") as child:
|
with self.assertRaises(SIGUSR1Exception):
|
||||||
self.wait_signal(child, 'SIGUSR1', SIGUSR1Exception)
|
with self.subprocess_send_signal(pid, "SIGUSR1") as child:
|
||||||
|
self.wait_signal(child, 'SIGUSR1')
|
||||||
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1,
|
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1,
|
||||||
'SIGALRM': 0})
|
'SIGALRM': 0})
|
||||||
|
|
||||||
|
@ -75,8 +70,9 @@ class InterProcessSignalTests(unittest.TestCase):
|
||||||
child.wait()
|
child.wait()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
signal.alarm(1)
|
with self.assertRaises(KeyboardInterrupt):
|
||||||
self.wait_signal(None, 'SIGALRM', KeyboardInterrupt)
|
signal.alarm(1)
|
||||||
|
self.wait_signal(None, 'SIGALRM')
|
||||||
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1,
|
self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1,
|
||||||
'SIGALRM': 0})
|
'SIGALRM': 0})
|
||||||
finally:
|
finally:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue