diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index 76675988c2bfe28..1a0a7639c2a0506 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -127,7 +127,10 @@ def _log_traceback(self, val): def _asyncio_awaited_by(self): if self.__asyncio_awaited_by is None: return None - return frozenset(self.__asyncio_awaited_by) + return frozenset( + waiter for waiter in self.__asyncio_awaited_by + if not waiter.done() + ) def get_loop(self): """Return the event loop the Future is bound to.""" diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 86b64880b37c9bd..8c80c06f964ac07 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -592,6 +592,8 @@ def __init__(self, aws, timeout): for f in todo: f.add_done_callback(self._handle_completion) futures.future_add_to_awaited_by(f, self._cur_task) + if todo and self._cur_task is not None: + self._cur_task.add_done_callback(self._handle_task_completion) if todo and timeout is not None: self._timeout_handle = ( loop.call_later(timeout, self._handle_timeout) @@ -635,6 +637,11 @@ def _handle_completion(self, f): if not self._todo and self._timeout_handle is not None: self._timeout_handle.cancel() + def _handle_task_completion(self, task): + for f in self._todo: + futures.future_discard_from_awaited_by(f, task) + self._cur_task = None + async def _wait_for_one(self, resolve=False): # Wait for the next future to be done and return it unless resolve is # set, in which case return either the result of the future or raise diff --git a/Lib/test/test_asyncio/test_graph.py b/Lib/test/test_asyncio/test_graph.py index 1f1c76e4aef69f8..d580af30b0cfacd 100644 --- a/Lib/test/test_asyncio/test_graph.py +++ b/Lib/test/test_asyncio/test_graph.py @@ -678,6 +678,30 @@ async def main(t): ] ]) + async def test_stack_as_completed_early_exit(self): + # gh-161766: a finished consumer must not remain in the await graph. + async def slow(): + await asyncio.sleep(0.01) + + async def fails(): + await asyncio.sleep(0) + raise OSError("boom") + + async def fetch_first(tasks): + for f in asyncio.as_completed(tasks): + try: + return await f + except OSError: + break + + good = asyncio.create_task(slow()) + bad = asyncio.create_task(fails()) + waiter = asyncio.create_task(fetch_first([good, bad])) + await waiter + self.assertFalse(good._asyncio_awaited_by) + self.assertNotIn("fetch_first", asyncio.format_call_graph(good)) + await good + async def test_stack_as_completed_timeout(self): # gh-156523: the awaiting task must be dropped when as_completed() times out stack_for_inner = None diff --git a/Misc/NEWS.d/next/Library/2026-11-15-18-55-00.gh-issue-161766.Kh7xQM.rst b/Misc/NEWS.d/next/Library/2026-11-15-18-55-00.gh-issue-161766.Kh7xQM.rst new file mode 100644 index 000000000000000..577eb2ca75a7833 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-15-18-55-00.gh-issue-161766.Kh7xQM.rst @@ -0,0 +1,2 @@ +Fix stale awaited-by edges left by :func:`asyncio.as_completed` after its +consumer exits early. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index d7415aef91532a7..35b1597194b3706 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1319,6 +1319,31 @@ _asyncio_Future_get_loop_impl(FutureObj *self, PyTypeObject *cls) return Py_NewRef(self->fut_loop); } +static int +future_is_done(PyObject *future) +{ + PyObject *done = PyObject_CallMethodNoArgs(future, &_Py_ID(done)); + if (done == NULL) { + return -1; + } + int is_done = PyObject_IsTrue(done); + Py_DECREF(done); + return is_done; +} + +static int +future_awaited_by_add_if_pending(PyObject *set, PyObject *waiter) +{ + int is_done = future_is_done(waiter); + if (is_done < 0) { + return -1; + } + if (is_done) { + return 0; + } + return PySet_Add(set, waiter); +} + /*[clinic input] @critical_section @getter @@ -1333,21 +1358,43 @@ _asyncio_Future__asyncio_awaited_by_get_impl(FutureObj *self) if (self->fut_awaited_by == NULL) { Py_RETURN_NONE; } - if (self->fut_awaited_by_is_set) { - /* Already a set, just wrap it into a frozen set and return. */ - assert(PySet_CheckExact(self->fut_awaited_by)); - return PyFrozenSet_New(self->fut_awaited_by); - } - - PyObject *set = PyFrozenSet_New(NULL); + PyObject *set = PySet_New(NULL); if (set == NULL) { return NULL; } - if (PySet_Add(set, self->fut_awaited_by)) { - Py_DECREF(set); - return NULL; + if (self->fut_awaited_by_is_set) { + assert(PySet_CheckExact(self->fut_awaited_by)); + PyObject *iterator = PyObject_GetIter(self->fut_awaited_by); + if (iterator == NULL) { + Py_DECREF(set); + return NULL; + } + PyObject *waiter; + while ((waiter = PyIter_Next(iterator)) != NULL) { + int res = future_awaited_by_add_if_pending(set, waiter); + Py_DECREF(waiter); + if (res < 0) { + Py_DECREF(iterator); + Py_DECREF(set); + return NULL; + } + } + Py_DECREF(iterator); + if (PyErr_Occurred()) { + Py_DECREF(set); + return NULL; + } } - return set; + else { + PyObject *waiter = self->fut_awaited_by; + if (future_awaited_by_add_if_pending(set, waiter) < 0) { + Py_DECREF(set); + return NULL; + } + } + PyObject *result = PyFrozenSet_New(set); + Py_DECREF(set); + return result; }