Crash report
Bug: If a finalizer declares or resolves a lazy import during shutdown, CPython can segfault.
This is a corner case where both of the following must exist:
- a lazy object whose
__import__ still works at shutdown;
- a holder that outlives the lazy-import state, such as a
ContextVar, the codec registry, fork callbacks, audit hooks or warnings filters.
Why: Shutdown frees lazy_modules and lazy_pending_submodules and sets them to NULL, but finalizers can still run after they have been freed and we do not have the necessary NULL checks in place to handle reachable functions accessing either nulled object.
Minimal repro:
import contextvars, json, types
def safe_import(name, *args, allowed={"json": json}):
return allowed[name]
ns = types.ModuleType("ns")
ns.json = __lazy_import__("json", {"__builtins__": {"__import__": safe_import}})
class Handle:
def __del__(self, ns=ns):
ns.json
contextvars.ContextVar("v").set(Handle())
$ ./python repro.py
Segmentation fault (core dumped)
#0 PySet_Discard (set=0x0, ...) at Objects/setobject.c
#1 _PyImport_DiscardLazyModule (...) at Python/import.c
#2 lazy_import_resolve_impl (...) at Objects/lazyimportobject.c
#3 _PyLazyImport_Reify (...)
#4 _Py_module_getattro_impl (...)
NOTE: PR is in progress
CPython versions tested on:
3.15
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
No response
Linked PRs
Crash report
Bug: If a finalizer declares or resolves a lazy import during shutdown, CPython can segfault.
This is a corner case where both of the following must exist:
__import__still works at shutdown;ContextVar, the codec registry, fork callbacks, audit hooks or warnings filters.Why: Shutdown frees
lazy_modulesandlazy_pending_submodulesand sets them to NULL, but finalizers can still run after they have been freed and we do not have the necessary NULL checks in place to handle reachable functions accessing either nulled object.Minimal repro:
NOTE: PR is in progress
CPython versions tested on:
3.15
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
No response
Linked PRs