@@ -2224,17 +2224,16 @@ enter_task(PyObject *loop, PyObject *task)
22242224 _PyThreadStateImpl * ts = (_PyThreadStateImpl * )_PyThreadState_GET ();
22252225
22262226 if (ts -> asyncio_running_loop != loop ) {
2227- PyErr_Format (
2228- PyExc_RuntimeError , "loop mismatch" );
2229- return -1 ;
2227+ PyErr_Format (PyExc_RuntimeError , "loop mismatch" );
2228+ return -1 ;
22302229 }
22312230
22322231 if (ts -> asyncio_running_task != NULL ) {
22332232 PyErr_Format (
22342233 PyExc_RuntimeError ,
22352234 "Cannot enter into task %R while another " \
22362235 "task %R is being executed." ,
2237- task , ts -> asyncio_running_task ? ts -> asyncio_running_task : Py_None , NULL );
2236+ task , ts -> asyncio_running_task , NULL );
22382237 return -1 ;
22392238 }
22402239
@@ -2248,9 +2247,8 @@ leave_task(PyObject *loop, PyObject *task)
22482247 _PyThreadStateImpl * ts = (_PyThreadStateImpl * )_PyThreadState_GET ();
22492248
22502249 if (ts -> asyncio_running_loop != loop ) {
2251- PyErr_Format (
2252- PyExc_RuntimeError , "loop mismatch" );
2253- return -1 ;
2250+ PyErr_Format (PyExc_RuntimeError , "loop mismatch" );
2251+ return -1 ;
22542252 }
22552253
22562254 if (ts -> asyncio_running_task != task ) {
@@ -2271,11 +2269,11 @@ swap_current_task(PyObject *loop, PyObject *task)
22712269 _PyThreadStateImpl * ts = (_PyThreadStateImpl * )_PyThreadState_GET ();
22722270
22732271 if (ts -> asyncio_running_loop != loop ) {
2274- PyErr_Format (
2275- PyExc_RuntimeError , "loop mismatch" );
2272+ PyErr_Format (PyExc_RuntimeError , "loop mismatch" );
22762273 return NULL ;
22772274 }
22782275
2276+ /* transfer ownership to avoid redundant ref counting */
22792277 PyObject * prev_task = ts -> asyncio_running_task ;
22802278 if (task != Py_None ) {
22812279 ts -> asyncio_running_task = Py_NewRef (task );
@@ -3882,7 +3880,8 @@ _asyncio_current_task_impl(PyObject *module, PyObject *loop)
38823880 }
38833881
38843882 _PyThreadStateImpl * ts = (_PyThreadStateImpl * )_PyThreadState_GET ();
3885-
3883+ // Fast path for the current running loop of current thread
3884+ // no locking or stop the world pause is required
38863885 if (ts -> asyncio_running_loop == loop ) {
38873886 if (ts -> asyncio_running_task != NULL ) {
38883887 Py_DECREF (loop );
@@ -3893,7 +3892,8 @@ _asyncio_current_task_impl(PyObject *module, PyObject *loop)
38933892 }
38943893
38953894 PyObject * ret = Py_None ;
3896-
3895+ // Stop the world and traverse the per-thread current tasks
3896+ // and return the task if the loop matches
38973897 PyInterpreterState * interp = ts -> base .interp ;
38983898 _PyEval_StopTheWorld (interp );
38993899 _Py_FOR_EACH_TSTATE_BEGIN (interp , p ) {
0 commit comments