Skip to content

Commit 05b139b

Browse files
[3.12] gh-109582: test_fork_signal_handling should wait for event (GH-109605) (#109695)
gh-109582: test_fork_signal_handling should wait for event (GH-109605) Sometimes the child_handled event was missing because either the child quits before it gets a chance to handle the signal, or the parent asserts before the event notification is delivered via IPC. Synchronize explicitly to avoid this. (cherry picked from commit 608c1f3) Co-authored-by: Davide Rizzo <[email protected]>
1 parent 5baa8af commit 05b139b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Lib/test/test_asyncio/test_unix_events.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import stat
1212
import sys
1313
import threading
14+
import time
1415
import unittest
1516
from unittest import mock
1617
import warnings
18+
from test import support
1719
from test.support import os_helper
1820
from test.support import socket_helper
1921
from test.support import wait_process
@@ -1911,8 +1913,14 @@ def test_fork_signal_handling(self):
19111913
parent_handled = manager.Event()
19121914

19131915
def child_main():
1914-
signal.signal(signal.SIGTERM, lambda *args: child_handled.set())
1916+
def on_sigterm(*args):
1917+
child_handled.set()
1918+
sys.exit()
1919+
1920+
signal.signal(signal.SIGTERM, on_sigterm)
19151921
child_started.set()
1922+
while True:
1923+
time.sleep(1)
19161924

19171925
async def main():
19181926
loop = asyncio.get_running_loop()
@@ -1922,7 +1930,7 @@ async def main():
19221930
process.start()
19231931
child_started.wait()
19241932
os.kill(process.pid, signal.SIGTERM)
1925-
process.join()
1933+
process.join(timeout=support.SHORT_TIMEOUT)
19261934

19271935
async def func():
19281936
await asyncio.sleep(0.1)
@@ -1933,6 +1941,7 @@ async def func():
19331941

19341942
asyncio.run(main())
19351943

1944+
child_handled.wait(timeout=support.SHORT_TIMEOUT)
19361945
self.assertFalse(parent_handled.is_set())
19371946
self.assertTrue(child_handled.is_set())
19381947

0 commit comments

Comments
 (0)