Mailing List Archive

GH-95899: fix asyncio.Runner to call set_event_loop only once (GH-95900) (#96003)
https://github.com/python/cpython/commit/8bd7a0b5810529cd3ed378de42a74e9301856f12
commit: 8bd7a0b5810529cd3ed378de42a74e9301856f12
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
committer: gvanrossum <gvanrossum@gmail.com>
date: 2022-08-15T15:01:23-07:00
summary:

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

(cherry picked from commit 914f6367a0d015986dafa7a9d542e24192753b6b)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2022-08-11-18-52-17.gh-issue-95899._Bi4uG.rst
M Lib/asyncio/runners.py
M Lib/test/test_asyncio/test_runners.py

diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py
index a19a7f99af06..b3e0c44b7fb5 100644
--- a/Lib/asyncio/runners.py
+++ b/Lib/asyncio/runners.py
@@ -115,8 +115,6 @@ def run(self, coro, *, context=None):

self._interrupt_count = 0
try:
- if self._set_event_loop:
- events.set_event_loop(self._loop)
return self._loop.run_until_complete(task)
except exceptions.CancelledError:
if self._interrupt_count > 0:
@@ -137,7 +135,11 @@ def _lazy_init(self):
return
if self._loop_factory is None:
self._loop = events.new_event_loop()
- self._set_event_loop = True
+ 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
else:
self._loop = self._loop_factory()
if self._debug is not None:
diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py
index 5e1db2357e75..4e3acb97c85d 100644
--- a/Lib/test/test_asyncio/test_runners.py
+++ b/Lib/test/test_asyncio/test_runners.py
@@ -456,6 +456,20 @@ async def 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__':
unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2022-08-11-18-52-17.gh-issue-95899._Bi4uG.rst b/Misc/NEWS.d/next/Library/2022-08-11-18-52-17.gh-issue-95899._Bi4uG.rst
new file mode 100644
index 000000000000..d2386cf3ae22
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-08-11-18-52-17.gh-issue-95899._Bi4uG.rst
@@ -0,0 +1 @@
+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.

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins