Skip to content

Commit c7a9d06

Browse files
gh-128002: optimistically remove tasks from linked list when finished (#129995)
1 parent 625470a commit c7a9d06

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Modules/_asynciomodule.c

+11
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,22 @@ future_ensure_alive(FutureObj *fut)
413413
} \
414414
} while(0);
415415

416+
static void unregister_task(asyncio_state *state, TaskObj *task);
416417

417418
static int
418419
future_schedule_callbacks(asyncio_state *state, FutureObj *fut)
419420
{
420421
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(fut);
421422

423+
assert(fut->fut_state != STATE_PENDING);
424+
425+
if (Task_Check(state, fut)) {
426+
// remove task from linked-list of tasks
427+
// as it is finished now
428+
TaskObj *task = (TaskObj *)fut;
429+
unregister_task(state, task);
430+
}
431+
422432
if (fut->fut_callback0 != NULL) {
423433
/* There's a 1st callback */
424434

@@ -4030,6 +4040,7 @@ add_tasks_llist(struct llist_node *head, PyListObject *tasks)
40304040
struct llist_node *node;
40314041
llist_for_each_safe(node, head) {
40324042
TaskObj *task = llist_data(node, TaskObj, task_node);
4043+
assert(task->task_state == STATE_PENDING);
40334044
// The linked list holds borrowed references to task
40344045
// as such it is possible that the task is concurrently
40354046
// deallocated while added to this list.

0 commit comments

Comments
 (0)