Skip to content

Commit fbd71f6

Browse files
bpo-41247: asyncio.set_running_loop() cache running loop holder (GH-21401)
The running loop holder cache variable was always set to NULL when calling set_running_loop. Now set_running_loop saves the newly created running loop holder in the cache variable for faster access in get_running_loop. Automerge-Triggered-By: @1st1 (cherry picked from commit 529f426) Co-authored-by: Tony Solomonik <[email protected]>
1 parent a0a6f11 commit fbd71f6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Always cache the running loop holder when running
2+
``asyncio.set_running_loop``.

Modules/_asynciomodule.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,13 @@ get_running_loop(PyObject **loop)
291291
static int
292292
set_running_loop(PyObject *loop)
293293
{
294-
cached_running_holder = NULL;
295-
cached_running_holder_tsid = 0;
294+
PyObject *ts_dict = NULL;
295+
296+
PyThreadState *tstate = PyThreadState_Get();
297+
if (tstate != NULL) {
298+
ts_dict = _PyThreadState_GetDict(tstate); // borrowed
299+
}
296300

297-
PyObject *ts_dict = PyThreadState_GetDict(); // borrowed
298301
if (ts_dict == NULL) {
299302
PyErr_SetString(
300303
PyExc_RuntimeError, "thread-local storage is not available");
@@ -314,6 +317,9 @@ set_running_loop(PyObject *loop)
314317
}
315318
Py_DECREF(rl);
316319

320+
cached_running_holder = (PyObject *)rl;
321+
cached_running_holder_tsid = PyThreadState_GetID(tstate);
322+
317323
return 0;
318324
}
319325

0 commit comments

Comments
 (0)