@@ -3747,9 +3747,16 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
37473747        return  NULL ;
37483748    }
37493749    int  err  =  0 ;
3750-     struct  llist_node  * node ;
37513750    PyInterpreterState  * interp  =  PyInterpreterState_Get ();
3751+     // Stop the world and traverse the per-thread linked list 
3752+     // of asyncio tasks of all threads and add them to the list. 
3753+     // Stop the world pause is required so that no thread 
3754+     // modifies it's linked list while being iterated here 
3755+     // concurrently. 
3756+     // This design allows for lock free register/unregister of tasks 
3757+     // of loops running concurrently in different threads. 
37523758    _PyEval_StopTheWorld (interp );
3759+     struct  llist_node  * node ;
37533760    _Py_FOR_EACH_TSTATE_BEGIN (interp , p ) {
37543761        _PyThreadStateImpl  * tstate  =  (_PyThreadStateImpl  * )p ;
37553762        struct  llist_node  * head  =  & tstate -> asyncio_tasks_head ;
@@ -3764,8 +3771,10 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
37643771            // otherwise it gets added to the list. 
37653772            if  (_Py_TryIncref ((PyObject  * )task )) {
37663773                if  (_PyList_AppendTakeRef ((PyListObject  * )tasks , (PyObject  * )task ) <  0 ) {
3767-                     Py_DECREF (tasks );
3768-                     Py_DECREF (loop );
3774+                     // do not call any escaping function such as Py_DECREF 
3775+                     // while holding the runtime lock, instead set err=1 and 
3776+                     // call them after releasing the runtime lock 
3777+                     // and starting the world to avoid any deadlocks. 
37693778                    err  =  1 ;
37703779                    break ;
37713780                }
@@ -3775,6 +3784,8 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
37753784    _Py_FOR_EACH_TSTATE_END (interp );
37763785    _PyEval_StartTheWorld (interp );
37773786    if  (err ) {
3787+         Py_DECREF (tasks );
3788+         Py_DECREF (loop );
37783789        return  NULL ;
37793790    }
37803791    PyObject  * scheduled_iter  =  PyObject_GetIter (state -> non_asyncio_tasks );
0 commit comments