https://github.com/python/cpython/commit/872e4c74367b415f91cd16a239530095593fba3b
commit: 872e4c74367b415f91cd16a239530095593fba3b
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-10-29T18:00:49+05:30
summary:

[3.14] gh-157856: Fix `asyncio.as_completed()` outside a running event loop 
(GH-157857) (#160231)

gh-157856: Fix `asyncio.as_completed()` outside a running event loop (GH-157857)
(cherry picked from commit b12fcffdb153d992e665510e23bfaa14519de56c)

Co-authored-by: Aslan Osmanov <[email protected]>
Co-authored-by: Andrew Svetlov <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-09-20-12-00-00.gh-issue-157856.wq4xuF.rst
M Lib/asyncio/tasks.py
M Lib/test/test_asyncio/test_tasks.py

diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index c5a07d8375100f..9889a990daa155 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -567,7 +567,13 @@ def __init__(self, aws, timeout):
         self._timeout_handle = None
 
         loop = events.get_event_loop()
-        self._cur_task = current_task()
+        # The iterator may be created outside of a running event loop and
+        # then driven with loop.run_until_complete(), in which case there
+        # is no current task to record as the waiter.
+        if events._get_running_loop() is loop:
+            self._cur_task = current_task(loop)
+        else:
+            self._cur_task = None
         todo = {ensure_future(aw, loop=loop) for aw in set(aws)}
         for f in todo:
             f.add_done_callback(self._handle_completion)
diff --git a/Lib/test/test_asyncio/test_tasks.py 
b/Lib/test/test_asyncio/test_tasks.py
index 6594c0ace89bd5..6c4c6e5086ac05 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -1843,6 +1843,22 @@ async def coro():
             futs = asyncio.as_completed([a])
             list(futs)
 
+    def test_as_completed_outside_running_loop(self):
+        # gh-157856: as_completed() must not require a running event loop
+        # when the iterator is created, only when it is driven.
+        loop = self.new_test_loop()
+        self.addCleanup(asyncio.set_event_loop, None)
+        asyncio.set_event_loop(loop)
+
+        async def coro(v):
+            await asyncio.sleep(0)
+            return v
+
+        tasks = [loop.create_task(coro(v)) for v in (1, 2)]
+        futs = asyncio.as_completed(tasks)
+        results = [loop.run_until_complete(f) for f in futs]
+        self.assertEqual(sorted(results), [1, 2])
+
     def test_as_completed_coroutine_use_running_loop(self):
         loop = self.new_test_loop()
 
diff --git 
a/Misc/NEWS.d/next/Library/2026-09-20-12-00-00.gh-issue-157856.wq4xuF.rst 
b/Misc/NEWS.d/next/Library/2026-09-20-12-00-00.gh-issue-157856.wq4xuF.rst
new file mode 100644
index 00000000000000..9f221fa657d429
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-09-20-12-00-00.gh-issue-157856.wq4xuF.rst
@@ -0,0 +1,3 @@
+Fix :func:`asyncio.as_completed` raising :exc:`RuntimeError` ("no running
+event loop") when the iterator is created outside of a running event loop and
+driven with :meth:`loop.run_until_complete() 
<asyncio.loop.run_until_complete>`.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to