Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/asyncio/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
7 changes: 7 additions & 0 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_asyncio/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix stale awaited-by edges left by :func:`asyncio.as_completed` after its
consumer exits early.
69 changes: 58 additions & 11 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}


Expand Down
Loading