aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/pystate.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index e6770ef40df..89a652850e9 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -643,6 +643,8 @@ init_interpreter(PyInterpreterState *interp,
_Py_brc_init_state(interp);
#endif
llist_init(&interp->mem_free_queue.head);
+ llist_init(&interp->asyncio_tasks_head);
+ interp->asyncio_tasks_lock = (PyMutex){0};
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
interp->monitors.tools[i] = 0;
}
@@ -1512,7 +1514,7 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->delete_later = NULL;
llist_init(&_tstate->mem_free_queue);
-
+ llist_init(&_tstate->asyncio_tasks_head);
if (interp->stoptheworld.requested || _PyRuntime.stoptheworld.requested) {
// Start in the suspended state if there is an ongoing stop-the-world.
tstate->state = _Py_THREAD_SUSPENDED;
@@ -1692,6 +1694,14 @@ PyThreadState_Clear(PyThreadState *tstate)
Py_CLEAR(((_PyThreadStateImpl *)tstate)->asyncio_running_loop);
Py_CLEAR(((_PyThreadStateImpl *)tstate)->asyncio_running_task);
+
+ PyMutex_Lock(&tstate->interp->asyncio_tasks_lock);
+ // merge any lingering tasks from thread state to interpreter's
+ // tasks list
+ llist_concat(&tstate->interp->asyncio_tasks_head,
+ &((_PyThreadStateImpl *)tstate)->asyncio_tasks_head);
+ PyMutex_Unlock(&tstate->interp->asyncio_tasks_lock);
+
Py_CLEAR(tstate->dict);
Py_CLEAR(tstate->async_exc);