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.
changed the title [-]Segfault when a finalizer declares or resolves lazy import during shutdown[/-][+]Segfault when a finalizer declares or resolves a lazy import during shutdown[/+]on Oct 5, 2026
Activity
gh-158820: Add null checks to avoid segfaults with lazy imports at sh…
[3.15] gh-158820: Add null checks to avoid segfaults with lazy import…
Make lazy-import shutdown bookkeeping a no-op once state is gone