Skip to content

Commit 351842b

Browse files
GH-66285: Revert "fix forking in asyncio" (#99756)
1 parent b5b3904 commit 351842b

File tree

3 files changed

+0
-109
lines changed

3 files changed

+0
-109
lines changed

Lib/asyncio/events.py

-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import subprocess
1818
import sys
1919
import threading
20-
import signal
2120

2221
from . import format_helpers
2322

@@ -666,14 +665,6 @@ class _Local(threading.local):
666665

667666
def __init__(self):
668667
self._local = self._Local()
669-
if hasattr(os, 'fork'):
670-
def on_fork():
671-
# Reset the loop and wakeupfd in the forked child process.
672-
self._local = self._Local()
673-
signal.set_wakeup_fd(-1)
674-
675-
os.register_at_fork(after_in_child=on_fork)
676-
677668

678669
def get_event_loop(self):
679670
"""Get the event loop for the current context.

Lib/test/test_asyncio/test_unix_events.py

-99
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@
1111
import sys
1212
import threading
1313
import unittest
14-
import time
1514
from unittest import mock
1615
import warnings
17-
import multiprocessing
1816
from test.support import os_helper
1917
from test.support import socket_helper
20-
from test.support import wait_process
21-
from test.support import hashlib_helper
2218

2319
if sys.platform == 'win32':
2420
raise unittest.SkipTest('UNIX only')
@@ -1871,100 +1867,5 @@ async def runner():
18711867
wsock.close()
18721868

18731869

1874-
@unittest.skipUnless(hasattr(os, 'fork'), 'requires os.fork()')
1875-
class TestFork(unittest.IsolatedAsyncioTestCase):
1876-
1877-
async def test_fork_not_share_event_loop(self):
1878-
# The forked process should not share the event loop with the parent
1879-
loop = asyncio.get_running_loop()
1880-
r, w = os.pipe()
1881-
self.addCleanup(os.close, r)
1882-
self.addCleanup(os.close, w)
1883-
pid = os.fork()
1884-
if pid == 0:
1885-
# child
1886-
try:
1887-
loop = asyncio.get_event_loop_policy().get_event_loop()
1888-
os.write(w, str(id(loop)).encode())
1889-
finally:
1890-
os._exit(0)
1891-
else:
1892-
# parent
1893-
child_loop = int(os.read(r, 100).decode())
1894-
self.assertNotEqual(child_loop, id(loop))
1895-
wait_process(pid, exitcode=0)
1896-
1897-
@hashlib_helper.requires_hashdigest('md5')
1898-
def test_fork_signal_handling(self):
1899-
# Sending signal to the forked process should not affect the parent
1900-
# process
1901-
ctx = multiprocessing.get_context('fork')
1902-
manager = ctx.Manager()
1903-
self.addCleanup(manager.shutdown)
1904-
child_started = manager.Event()
1905-
child_handled = manager.Event()
1906-
parent_handled = manager.Event()
1907-
1908-
def child_main():
1909-
signal.signal(signal.SIGTERM, lambda *args: child_handled.set())
1910-
child_started.set()
1911-
time.sleep(1)
1912-
1913-
async def main():
1914-
loop = asyncio.get_running_loop()
1915-
loop.add_signal_handler(signal.SIGTERM, lambda *args: parent_handled.set())
1916-
1917-
process = ctx.Process(target=child_main)
1918-
process.start()
1919-
child_started.wait()
1920-
os.kill(process.pid, signal.SIGTERM)
1921-
process.join()
1922-
1923-
async def func():
1924-
await asyncio.sleep(0.1)
1925-
return 42
1926-
1927-
# Test parent's loop is still functional
1928-
self.assertEqual(await asyncio.create_task(func()), 42)
1929-
1930-
asyncio.run(main())
1931-
1932-
self.assertFalse(parent_handled.is_set())
1933-
self.assertTrue(child_handled.is_set())
1934-
1935-
@hashlib_helper.requires_hashdigest('md5')
1936-
def test_fork_asyncio_run(self):
1937-
ctx = multiprocessing.get_context('fork')
1938-
manager = ctx.Manager()
1939-
self.addCleanup(manager.shutdown)
1940-
result = manager.Value('i', 0)
1941-
1942-
async def child_main():
1943-
await asyncio.sleep(0.1)
1944-
result.value = 42
1945-
1946-
process = ctx.Process(target=lambda: asyncio.run(child_main()))
1947-
process.start()
1948-
process.join()
1949-
1950-
self.assertEqual(result.value, 42)
1951-
1952-
@hashlib_helper.requires_hashdigest('md5')
1953-
def test_fork_asyncio_subprocess(self):
1954-
ctx = multiprocessing.get_context('fork')
1955-
manager = ctx.Manager()
1956-
self.addCleanup(manager.shutdown)
1957-
result = manager.Value('i', 1)
1958-
1959-
async def child_main():
1960-
proc = await asyncio.create_subprocess_exec(sys.executable, '-c', 'pass')
1961-
result.value = await proc.wait()
1962-
1963-
process = ctx.Process(target=lambda: asyncio.run(child_main()))
1964-
process.start()
1965-
process.join()
1966-
1967-
self.assertEqual(result.value, 0)
1968-
19691870
if __name__ == '__main__':
19701871
unittest.main()

Misc/NEWS.d/next/Library/2022-11-17-10-56-47.gh-issue-66285.KvjlaB.rst

-1
This file was deleted.

0 commit comments

Comments
 (0)