From 15bc6d2e13adc4c9be983f919b55c584a9a52c22 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Tue, 29 May 2018 12:07:00 -0400 Subject: [PATCH 1/2] bpo-22087: Fix Policy.get_event_loop() to detect fork and return a new loop. Original patch by Dan O'Reilly. --- Lib/asyncio/events.py | 7 +++++++ Lib/test/test_asyncio/test_unix_events.py | 21 +++++++++++++++++++ .../2018-05-29-12-06-54.bpo-22087.uv7_Y6.rst | 3 +++ 3 files changed, 31 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-05-29-12-06-54.bpo-22087.uv7_Y6.rst diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 40946bbf65299d..68dc25e1e8bedf 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -625,16 +625,23 @@ class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy): class _Local(threading.local): _loop = None + _pid = None _set_called = False def __init__(self): self._local = self._Local() + self._local._pid = os.getpid() def get_event_loop(self): """Get the event loop. This may be None or an instance of EventLoop. """ + if self._local._pid != os.getpid(): + # If we detect we're in a child process forked by multiprocessing, + # we reset self._local so that we'll get a new event loop. + self._local = self._Local() + if (self._local._loop is None and not self._local._set_called and isinstance(threading.current_thread(), threading._MainThread)): diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index a01efedf66d909..3cc31d70a9d023 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -13,6 +13,7 @@ import tempfile import threading import unittest +import multiprocessing from unittest import mock from test import support @@ -1804,6 +1805,26 @@ def create_watcher(self): return asyncio.FastChildWatcher() +class ForkedProcessTests(unittest.TestCase): + def setUp(self): + self.parent_loop = asyncio.SelectorEventLoop() + asyncio.set_event_loop(self.parent_loop) + self.ctx = multiprocessing.get_context("fork") + + def _check_loops_not_equal(self, old_loop): + loop = asyncio.get_event_loop() + sys.exit(loop is old_loop) + + def test_new_loop_in_child(self): + p = self.ctx.Process(target=self._check_loops_not_equal, + args=(self.parent_loop,)) + p.start() + p.join() + self.assertEqual(p.exitcode, 0, + "Child process inherited parent's event loop") + self.parent_loop.close() + + class PolicyTests(unittest.TestCase): def create_policy(self): diff --git a/Misc/NEWS.d/next/Library/2018-05-29-12-06-54.bpo-22087.uv7_Y6.rst b/Misc/NEWS.d/next/Library/2018-05-29-12-06-54.bpo-22087.uv7_Y6.rst new file mode 100644 index 00000000000000..92127cd57a91d7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-05-29-12-06-54.bpo-22087.uv7_Y6.rst @@ -0,0 +1,3 @@ +Fix Policy.get_event_loop() to detect fork and return a new loop. + +Original patch by Dan O'Reilly. From 0bd8ecbd8cc3ab15b8a54af47c32fc9da7113b06 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Tue, 29 May 2018 14:29:19 -0400 Subject: [PATCH 2/2] More tests --- Lib/test/test_asyncio/test_unix_events.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 3cc31d70a9d023..1bc2d8658d8a92 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -1811,8 +1811,21 @@ def setUp(self): asyncio.set_event_loop(self.parent_loop) self.ctx = multiprocessing.get_context("fork") + def tearDown(self): + self.parent_loop.close() + def _check_loops_not_equal(self, old_loop): loop = asyncio.get_event_loop() + if loop is old_loop: + raise RuntimeError("Child process inherited parent's event loop") + + try: + val = loop.run_until_complete(asyncio.sleep(0.05, result=42)) + if val != 42: + raise RuntimeError("new event loop does not work") + finally: + loop.close() + sys.exit(loop is old_loop) def test_new_loop_in_child(self): @@ -1820,9 +1833,7 @@ def test_new_loop_in_child(self): args=(self.parent_loop,)) p.start() p.join() - self.assertEqual(p.exitcode, 0, - "Child process inherited parent's event loop") - self.parent_loop.close() + self.assertEqual(p.exitcode, 0) class PolicyTests(unittest.TestCase):