From 6f3db4b017f293933fed822578bd9e8f12217a04 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 7 Feb 2025 12:11:45 +0000 Subject: [PATCH 1/2] optimistic --- Modules/_asynciomodule.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 656c03a98d73b2..6d09fafe4ae99a 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -413,12 +413,20 @@ future_ensure_alive(FutureObj *fut) } \ } while(0); +static void unregister_task(asyncio_state *state, TaskObj *task); static int future_schedule_callbacks(asyncio_state *state, FutureObj *fut) { _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut); + assert(fut->fut_state != STATE_PENDING); + + if (Task_Check(state, fut)) { + TaskObj *task = (TaskObj *)fut; + unregister_task(state, task); + } + if (fut->fut_callback0 != NULL) { /* There's a 1st callback */ @@ -4019,6 +4027,7 @@ add_tasks_llist(struct llist_node *head, PyListObject *tasks) struct llist_node *node; llist_for_each_safe(node, head) { TaskObj *task = llist_data(node, TaskObj, task_node); + assert(task->task_state == STATE_PENDING); // The linked list holds borrowed references to task // as such it is possible that the task is concurrently // deallocated while added to this list. From ba20f49d44d1b58fcd8f5372fde0587df74dc8a7 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 11 Feb 2025 10:19:30 +0000 Subject: [PATCH 2/2] add comment --- Modules/_asynciomodule.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 7691cb8d95cd6d..ff24fd76a61733 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -423,6 +423,8 @@ future_schedule_callbacks(asyncio_state *state, FutureObj *fut) assert(fut->fut_state != STATE_PENDING); if (Task_Check(state, fut)) { + // remove task from linked-list of tasks + // as it is finished now TaskObj *task = (TaskObj *)fut; unregister_task(state, task); }