Skip to content

Segfault when a finalizer declares or resolves a lazy import during shutdown #158820

Description

@brittanyrey

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions