bpo-46070: Fix asyncio initialisation guard (GH-30423)

If init flag is set, exit successfully immediately.
If not, only set the flag after successful initialization.
(cherry picked from commit b127e70a8a)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
This commit is contained in:
Miss Islington (bot) 2022-01-07 06:35:15 -08:00 committed by GitHub
parent db60ed1170
commit 9d18045804
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 6 deletions

View file

@ -0,0 +1,2 @@
Fix possible segfault when importing the :mod:`asyncio` module from
different sub-interpreters in parallel. Patch by Erlend E. Aasland.

View file

@ -3309,17 +3309,14 @@ static int
module_init(void)
{
PyObject *module = NULL;
if (module_initialized) {
return 0;
}
asyncio_mod = PyImport_ImportModule("asyncio");
if (asyncio_mod == NULL) {
goto fail;
}
if (module_initialized != 0) {
return 0;
}
else {
module_initialized = 1;
}
current_tasks = PyDict_New();
if (current_tasks == NULL) {
@ -3380,6 +3377,7 @@ module_init(void)
goto fail;
}
module_initialized = 1;
Py_DECREF(module);
return 0;