Skip to content

Possible NULL-related SystemError when interpreter is not fully initialized #161863

Closed

Description

@sobolevn

Code:

cpython/Python/import.c

Lines 3921 to 3922 in 9f38636

mod = PyObject_CallMethodObjArgs(IMPORTLIB(interp), find_and_load,
abs_name, IMPORT_FUNC(interp), NULL);

Here IMPORTLIB(interp) can be NULL under rare conditions:

cpython/Python/import.c

Lines 95 to 96 in 9f38636

#define IMPORTLIB(interp) \
(interp)->imports.importlib

For example, when interpreter is not yet fully initialized.

Output:

» ./Programs/_freeze_module crash crash.py crash.h
Exception ignored in the internal traceback machinery:
SystemError: null argument to internal routine
# ...

Refs #161860 where I first found this case.
Refs #142662 where the initial reproducer was found.

I guess we should raise a different SystemError here with the text that imports are not yet fully initialized.

My proposal is to make the error message better here: it should state the interpreter is not fully initialized.

And ideally we can fix the freeze command as well later.

Linked PRs

Activity

  1. vstinner commented on Nov 12, 2026

    @vstinner
    Member

    Refs #142662 where the initial reproducer was found.

    So I suppose that crash.py is:

    echo "# -*- coding: UTF -*-" > crash.py
    ./Programs/_freeze_module crash crash.py crash.h
    

    For this specific bug, you can handle the error from the start, in _PyErr_Display()

    diff --git a/Python/pythonrun.c b/Python/pythonrun.c
    index 5e026a93d5c..57db31ce591 100644
    --- a/Python/pythonrun.c
    +++ b/Python/pythonrun.c
    @@ -1157,6 +1157,11 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
             }
         }
     
    +    PyInterpreterState *interp = _PyInterpreterState_GET();
    +    if (interp->imports.importlib == NULL) {
    +        goto fallback;
    +    }
    +
         // Try first with the stdlib traceback module
         PyObject *print_exception_fn = PyImport_ImportModuleAttrString(
             "traceback",

    With a comment explaining that the error occurred before importlib has been initialized.

    gdb traceback:

    Details
    (gdb) where
    #0  import_find_and_load_with_name (tstate=tstate@entry=0x9a55e8 <_PyRuntime+266472>, 
        abs_name=abs_name@entry=0x7ffff7c4ce50, find_and_load=0x979f90 <_PyRuntime+88720>, 
        not_found=not_found@entry=0x0) at Python/import.c:3921
    #1  0x00000000008426d2 in import_find_and_load (tstate=tstate@entry=0x9a55e8 <_PyRuntime+266472>, 
        abs_name=abs_name@entry=0x7ffff7c4ce50) at Python/import.c:3955
    #2  0x0000000000846996 in PyImport_ImportModuleLevelObject (name=name@entry=0x7ffff7c4ce50, 
        globals=globals@entry=0x7ffff7c50ad0, locals=locals@entry=0x7ffff7c50ad0, 
        fromlist=fromlist@entry=0x7ffff7c4cd20, level=level@entry=0) at Python/import.c:4048
    #3  0x00000000007d492b in builtin___import___impl (module=module@entry=0x7ffff7c305f0, 
        name=name@entry=0x7ffff7c4ce50, globals=globals@entry=0x7ffff7c50ad0, locals=locals@entry=0x7ffff7c50ad0, 
        fromlist=fromlist@entry=0x7ffff7c4cd20, level=level@entry=0) at Python/bltinmodule.c:287
    #4  0x00000000007d4a6b in builtin___import__ (module=0x7ffff7c305f0, args=<optimized out>, 
        args@entry=0x7fffffffd6e0, nargs=nargs@entry=5, kwnames=kwnames@entry=0x0)
        at Python/clinic/bltinmodule.c.h:112
    #5  0x0000000000718065 in cfunction_vectorcall_FASTCALL_KEYWORDS (func=0x7ffff7c3ee70, args=0x7fffffffd6e0, 
        nargsf=<optimized out>, kwnames=0x0) at Objects/methodobject.c:464
    #6  0x00000000006be06e in _PyObject_VectorcallTstate (tstate=tstate@entry=0x9a55e8 <_PyRuntime+266472>, 
        callable=callable@entry=0x7ffff7c3ee70, args=args@entry=0x7fffffffd6e0, nargsf=5, 
        kwnames=kwnames@entry=0x0) at ./Include/internal/pycore_call.h:143
    #7  0x00000000006be244 in _PyObject_CallFunctionVa (tstate=0x9a55e8 <_PyRuntime+266472>, 
        callable=callable@entry=0x7ffff7c3ee70, format=format@entry=0x41ba56 "OOOOi", va=va@entry=0x7fffffffd748)
        at Objects/call.c:552
    #8  0x00000000006be369 in PyObject_CallFunction (callable=callable@entry=0x7ffff7c3ee70, 
        format=format@entry=0x41ba56 "OOOOi") at Objects/call.c:574
    #9  0x00000000008471be in PyImport_Import (module_name=module_name@entry=0x7ffff7c4ce50)
        at Python/import.c:4667
    #10 0x0000000000847679 in PyImport_ImportModuleAttr (modname=modname@entry=0x7ffff7c4ce50, 
        attrname=attrname@entry=0x7ffff7c50dc0) at Python/import.c:4882
    #11 0x00000000008476e6 in PyImport_ImportModuleAttrString (modname=modname@entry=0x41a131 "traceback", 
        attrname=attrname@entry=0x41a004 "_print_exception_bltin") at Python/import.c:4903
    #12 0x000000000087755b in _PyErr_Display (file=0x7ffff7c00190, unused=unused@entry=0x0, 
        value=value@entry=0x7ffff7c2d240, tb=tb@entry=0x936720 <_Py_NoneStruct>) at Python/pythonrun.c:1161
    #13 0x0000000000877751 in PyErr_Display (unused=unused@entry=0x0, value=0x7ffff7c2d240, 
        tb=0x936720 <_Py_NoneStruct>) at Python/pythonrun.c:1229
    #14 0x000000000088a771 in sys_excepthook_impl (module=module@entry=0x7ffff7c300e0, exctype=<optimized out>, 
        value=<optimized out>, traceback=<optimized out>) at ./Python/sysmodule.c:839
    #15 0x000000000088a7b9 in sys_excepthook (module=0x7ffff7c300e0, args=args@entry=0x7fffffffda00, 
        nargs=nargs@entry=3) at ./Python/clinic/sysmodule.c.h:149
    #16 0x00000000007181f1 in cfunction_vectorcall_FASTCALL (func=0x7ffff7c3a030, args=0x7fffffffda00, 
        nargsf=<optimized out>, kwnames=<optimized out>) at Objects/methodobject.c:448
    #17 0x00000000006be06e in _PyObject_VectorcallTstate (tstate=0x9a55e8 <_PyRuntime+266472>, 
        callable=0x7ffff7c3a030, args=args@entry=0x7fffffffda00, nargsf=nargsf@entry=3, kwnames=kwnames@entry=0x0)
        at ./Include/internal/pycore_call.h:143
    #18 0x00000000006be136 in PyObject_Vectorcall (callable=<optimized out>, args=args@entry=0x7fffffffda00, 
        nargsf=nargsf@entry=3, kwnames=kwnames@entry=0x0) at Objects/call.c:327
    #19 0x000000000087790d in _PyErr_PrintEx (tstate=0x9a55e8 <_PyRuntime+266472>, 
        set_sys_last_vars=set_sys_last_vars@entry=1) at Python/pythonrun.c:749
    #20 0x0000000000877b14 in PyErr_PrintEx (set_sys_last_vars=set_sys_last_vars@entry=1) at Python/pythonrun.c:788
    #21 0x0000000000877b24 in PyErr_Print () at Python/pythonrun.c:794
    #22 0x000000000062012c in main (argc=<optimized out>, argv=<optimized out>) at Programs/_freeze_module.c:260
    
  2. added 3 commits that reference this issue on Nov 14, 2026
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-importlibtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions