Skip to content
Snippets Groups Projects
Unverified Commit 914f6367 authored by Kumar Aditya's avatar Kumar Aditya Committed by GitHub
Browse files

GH-95899: fix asyncio.Runner to call set_event_loop only once (#95900)

parent 2fa03b1b
Branches
Tags
No related merge requests found
...@@ -114,8 +114,6 @@ def run(self, coro, *, context=None): ...@@ -114,8 +114,6 @@ def run(self, coro, *, context=None):
self._interrupt_count = 0 self._interrupt_count = 0
try: try:
if self._set_event_loop:
events.set_event_loop(self._loop)
return self._loop.run_until_complete(task) return self._loop.run_until_complete(task)
except exceptions.CancelledError: except exceptions.CancelledError:
if self._interrupt_count > 0: if self._interrupt_count > 0:
...@@ -136,6 +134,10 @@ def _lazy_init(self): ...@@ -136,6 +134,10 @@ def _lazy_init(self):
return return
if self._loop_factory is None: if self._loop_factory is None:
self._loop = events.new_event_loop() self._loop = events.new_event_loop()
if not self._set_event_loop:
# Call set_event_loop only once to avoid calling
# attach_loop multiple times on child watchers
events.set_event_loop(self._loop)
self._set_event_loop = True self._set_event_loop = True
else: else:
self._loop = self._loop_factory() self._loop = self._loop_factory()
......
...@@ -455,6 +455,20 @@ async def coro(): ...@@ -455,6 +455,20 @@ async def coro():
): ):
runner.run(coro()) runner.run(coro())
def test_set_event_loop_called_once(self):
# See https://github.com/python/cpython/issues/95736
async def coro():
pass
policy = asyncio.get_event_loop_policy()
policy.set_event_loop = mock.Mock()
runner = asyncio.Runner()
runner.run(coro())
runner.run(coro())
self.assertEqual(1, policy.set_event_loop.call_count)
runner.close()
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Fix :class:`asyncio.Runner` to call :func:`asyncio.set_event_loop` only once to avoid calling :meth:`~asyncio.AbstractChildWatcher.attach_loop` multiple times on child watchers. Patch by Kumar Aditya.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment