Skip to content

Commit 500a419

Browse files
Yhg1snjsmith
andauthored
[3.6] bpo-32591: fix abort in _PyErr_WarnUnawaitedCoroutine during shutdown (GH-5337) (#6536)
When an unawaited coroutine is collected very late in shutdown -- like, during the final GC at the end of PyImport_Cleanup -- then it was triggering an interpreter abort, because we'd try to look up the "warnings" module and not only was it missing (we were prepared for that), but the entire module system was missing (which we were not prepared for). I've tried to fix this at the source, by making the utility function get_warnings_attr robust against this in general. Note that it already has the convention that it can return NULL without setting an error, which is how it signals that the attribute it was asked to fetch is missing, and that all callers already check for NULL returns. There's a similar check for being late in shutdown at the top of warn_explicit, which might be unnecessary after this fix, but I'm not sure so I'm going to leave it.. (cherry picked from commit dba976b) Co-authored-by: Nathaniel J. Smith <[email protected]>
1 parent 55d9e86 commit 500a419

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Python/_warnings.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ get_warnings_attr(const char *attr, int try_import)
6363
}
6464
}
6565
else {
66+
/* if we're so late into Python finalization that the module dict is
67+
gone, then we can't even use PyImport_GetModule without triggering
68+
an interpreter abort.
69+
*/
70+
if (!PyThreadState_GET()->interp->modules) {
71+
return NULL;
72+
}
6673
all_modules = PyImport_GetModuleDict();
6774

6875
warnings_module = PyDict_GetItem(all_modules, warnings_str);

0 commit comments

Comments
 (0)