[Python-checkins] bpo-46070: Fix asyncio initialisation guard (GH-30423)

miss-islington webhook-mailer at python.org
Fri Jan 7 09:35:20 EST 2022


https://github.com/python/cpython/commit/9d18045804f6db8224be14f7a618b77977f90144
commit: 9d18045804f6db8224be14f7a618b77977f90144
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-01-07T06:35:15-08:00
summary:

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 b127e70a8a682fe869c22ce04c379bd85a00db67)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland at innova.no>

files:
A Misc/NEWS.d/next/Library/2022-01-07-13-51-22.bpo-46070.-axLUW.rst
M Modules/_asynciomodule.c

diff --git a/Misc/NEWS.d/next/Library/2022-01-07-13-51-22.bpo-46070.-axLUW.rst b/Misc/NEWS.d/next/Library/2022-01-07-13-51-22.bpo-46070.-axLUW.rst
new file mode 100644
index 0000000000000..0fedc9dfb8fb1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-07-13-51-22.bpo-46070.-axLUW.rst
@@ -0,0 +1,2 @@
+Fix possible segfault when importing the :mod:`asyncio` module from
+different sub-interpreters in parallel. Patch by Erlend E. Aasland.
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index 56079b0277d1a..befec9a8342ef 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -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;
 



More information about the Python-checkins mailing list