@@ -2258,7 +2258,14 @@ swap_current_task(PyObject *loop, PyObject *task)
22582258{
22592259 _PyThreadStateImpl * ts = (_PyThreadStateImpl * )_PyThreadState_GET ();
22602260 PyObject * prev_task = ts -> asyncio_running_task ;
2261- ts -> asyncio_running_task = Py_NewRef (task );
2261+ if (task != Py_None ) {
2262+ ts -> asyncio_running_task = Py_NewRef (task );
2263+ } else {
2264+ Py_CLEAR (ts -> asyncio_running_task );
2265+ }
2266+ if (prev_task == NULL ) {
2267+ Py_RETURN_NONE ;
2268+ }
22622269 return prev_task ;
22632270}
22642271
@@ -3846,8 +3853,6 @@ static PyObject *
38463853_asyncio_current_task_impl (PyObject * module , PyObject * loop )
38473854/*[clinic end generated code: output=fe15ac331a7f981a input=58910f61a5627112]*/
38483855{
3849- PyObject * ret ;
3850-
38513856 if (loop == Py_None ) {
38523857 loop = _asyncio_get_running_loop_impl (module );
38533858 if (loop == NULL ) {
@@ -3860,19 +3865,24 @@ _asyncio_current_task_impl(PyObject *module, PyObject *loop)
38603865 _PyThreadStateImpl * ts = (_PyThreadStateImpl * )_PyThreadState_GET ();
38613866
38623867 if (ts -> asyncio_running_loop == loop ) {
3863- ret = Py_XNewRef (ts -> asyncio_running_task );
3868+ if (ts -> asyncio_running_task != NULL ) {
3869+ Py_DECREF (loop );
3870+ return Py_NewRef (ts -> asyncio_running_task );
3871+ }
38643872 Py_DECREF (loop );
3865- return ret ;
3873+ Py_RETURN_NONE ;
38663874 }
38673875
3868- ret = Py_None ;
3876+ PyObject * ret = Py_None ;
38693877
38703878 PyInterpreterState * interp = ts -> base .interp ;
38713879 _PyEval_StopTheWorld (interp );
38723880 _Py_FOR_EACH_TSTATE_BEGIN (interp , p ) {
38733881 ts = (_PyThreadStateImpl * )p ;
38743882 if (ts -> asyncio_running_loop == loop ) {
3875- ret = Py_XNewRef (ts -> asyncio_running_task );
3883+ if (ts -> asyncio_running_task != NULL ) {
3884+ ret = Py_NewRef (ts -> asyncio_running_task );
3885+ }
38763886 goto exit ;
38773887 }
38783888 }
0 commit comments