From cd48f4890edc0bc9a3bda3149c177193b92498ce Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Thu, 24 Nov 2022 22:41:38 +0530 Subject: [PATCH 1/2] Revert "GH-66285: skip asyncio fork tests for platforms without md5 hash (#99745)" This reverts commit 679d963fc896a3328a6440a1af1bc2a38bf6a5e9. --- Lib/test/test_asyncio/test_unix_events.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index e71e242a5f9f17..4e1dab2f86b4dd 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -18,7 +18,6 @@ from test.support import os_helper from test.support import socket_helper from test.support import wait_process -from test.support import hashlib_helper if sys.platform == 'win32': raise unittest.SkipTest('UNIX only') @@ -1894,7 +1893,6 @@ async def test_fork_not_share_event_loop(self): self.assertNotEqual(child_loop, id(loop)) wait_process(pid, exitcode=0) - @hashlib_helper.requires_hashdigest('md5') def test_fork_signal_handling(self): # Sending signal to the forked process should not affect the parent # process @@ -1932,7 +1930,6 @@ async def func(): self.assertFalse(parent_handled.is_set()) self.assertTrue(child_handled.is_set()) - @hashlib_helper.requires_hashdigest('md5') def test_fork_asyncio_run(self): ctx = multiprocessing.get_context('fork') manager = ctx.Manager() @@ -1949,7 +1946,6 @@ async def child_main(): self.assertEqual(result.value, 42) - @hashlib_helper.requires_hashdigest('md5') def test_fork_asyncio_subprocess(self): ctx = multiprocessing.get_context('fork') manager = ctx.Manager() From 0671a90ac5463b61e09c2cd123af9915c9e36fee Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Thu, 24 Nov 2022 22:41:52 +0530 Subject: [PATCH 2/2] Revert "GH-66285: fix forking in `asyncio` (#99539)" This reverts commit 0c1fbc17b48f56c6070d335ed291a24c91ed190a. --- Lib/asyncio/events.py | 9 -- Lib/test/test_asyncio/test_unix_events.py | 95 ------------------- ...2-11-17-10-56-47.gh-issue-66285.KvjlaB.rst | 1 - 3 files changed, 105 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2022-11-17-10-56-47.gh-issue-66285.KvjlaB.rst diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 39a01048b4c8dd..a327ba54a323a8 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -17,7 +17,6 @@ import subprocess import sys import threading -import signal from . import format_helpers @@ -666,14 +665,6 @@ class _Local(threading.local): def __init__(self): self._local = self._Local() - if hasattr(os, 'fork'): - def on_fork(): - # Reset the loop and wakeupfd in the forked child process. - self._local = self._Local() - signal.set_wakeup_fd(-1) - - os.register_at_fork(after_in_child=on_fork) - def get_event_loop(self): """Get the event loop for the current context. diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 4e1dab2f86b4dd..93e8611f184d25 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -11,13 +11,10 @@ import sys import threading import unittest -import time from unittest import mock import warnings -import multiprocessing from test.support import os_helper from test.support import socket_helper -from test.support import wait_process if sys.platform == 'win32': raise unittest.SkipTest('UNIX only') @@ -1870,97 +1867,5 @@ async def runner(): wsock.close() -@unittest.skipUnless(hasattr(os, 'fork'), 'requires os.fork()') -class TestFork(unittest.IsolatedAsyncioTestCase): - - async def test_fork_not_share_event_loop(self): - # The forked process should not share the event loop with the parent - loop = asyncio.get_running_loop() - r, w = os.pipe() - self.addCleanup(os.close, r) - self.addCleanup(os.close, w) - pid = os.fork() - if pid == 0: - # child - try: - loop = asyncio.get_event_loop_policy().get_event_loop() - os.write(w, str(id(loop)).encode()) - finally: - os._exit(0) - else: - # parent - child_loop = int(os.read(r, 100).decode()) - self.assertNotEqual(child_loop, id(loop)) - wait_process(pid, exitcode=0) - - def test_fork_signal_handling(self): - # Sending signal to the forked process should not affect the parent - # process - ctx = multiprocessing.get_context('fork') - manager = ctx.Manager() - self.addCleanup(manager.shutdown) - child_started = manager.Event() - child_handled = manager.Event() - parent_handled = manager.Event() - - def child_main(): - signal.signal(signal.SIGTERM, lambda *args: child_handled.set()) - child_started.set() - time.sleep(1) - - async def main(): - loop = asyncio.get_running_loop() - loop.add_signal_handler(signal.SIGTERM, lambda *args: parent_handled.set()) - - process = ctx.Process(target=child_main) - process.start() - child_started.wait() - os.kill(process.pid, signal.SIGTERM) - process.join() - - async def func(): - await asyncio.sleep(0.1) - return 42 - - # Test parent's loop is still functional - self.assertEqual(await asyncio.create_task(func()), 42) - - asyncio.run(main()) - - self.assertFalse(parent_handled.is_set()) - self.assertTrue(child_handled.is_set()) - - def test_fork_asyncio_run(self): - ctx = multiprocessing.get_context('fork') - manager = ctx.Manager() - self.addCleanup(manager.shutdown) - result = manager.Value('i', 0) - - async def child_main(): - await asyncio.sleep(0.1) - result.value = 42 - - process = ctx.Process(target=lambda: asyncio.run(child_main())) - process.start() - process.join() - - self.assertEqual(result.value, 42) - - def test_fork_asyncio_subprocess(self): - ctx = multiprocessing.get_context('fork') - manager = ctx.Manager() - self.addCleanup(manager.shutdown) - result = manager.Value('i', 1) - - async def child_main(): - proc = await asyncio.create_subprocess_exec(sys.executable, '-c', 'pass') - result.value = await proc.wait() - - process = ctx.Process(target=lambda: asyncio.run(child_main())) - process.start() - process.join() - - self.assertEqual(result.value, 0) - if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2022-11-17-10-56-47.gh-issue-66285.KvjlaB.rst b/Misc/NEWS.d/next/Library/2022-11-17-10-56-47.gh-issue-66285.KvjlaB.rst deleted file mode 100644 index ebd82173882726..00000000000000 --- a/Misc/NEWS.d/next/Library/2022-11-17-10-56-47.gh-issue-66285.KvjlaB.rst +++ /dev/null @@ -1 +0,0 @@ -Fix :mod:`asyncio` to not share event loop and signal wakeupfd in forked processes. Patch by Kumar Aditya.