Repository navigation
Extensions that raise exceptions in their PyInit* cause a crash in subinterpreters #144601
Copy link
Copy link
Closed
Closed
Copy link
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-subinterpreterstype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
Description
Activity
- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes
on Feb 8, 2026 - added a commit that references this issue
on Feb 9, 2026 Would this be fixed (for single-phase extension) by #142979?
No, this is a separate bug. I noticed this while writing a test case for #142979.
Reacted by jan-hasse- added a commit that references this issue
on Feb 16, 2026 - added 5 commits that reference this issue
on Feb 16, 2026
Metadata
Metadata
Assignees
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-subinterpreterstype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
Projects
- StatusShow more project fieldsDone
Crash report
What happened?
I found this while investigating #138045.
If a
PyInit_function raises an exception when imported from a subinterpreter, the process will crash. For example, if you modifyxxsubtyppe.cto raise an exception in itsPyInitand then import it from a subinterpreter, you'll see a crash:This isn't usually a problem for multi-phase extensions, because
PyModuleDef_Initdoesn't fail with an exception (except sometimes on the free-threaded build, but that's rare), but this does happen for single-phase extensions. Single-phase extension modules do not support loading in subinterpreters, but attempting to load them from a subinterpreter should raise an exception rather than crash the interpreter.I believe the problem lies here:
cpython/Python/import.c
Lines 2178 to 2196 in d736349
The exception object raised by the
PyInit_function is always owned by the main interpreter, but the exception is raised from the subinterpreter, which breaks things. There's not a clean way to raise the exception in the calling interpreter, so I think the best solution is to print the exception as unraisable in the main interpreter, and then raise some sort of blanket exception in the calling interpreter.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
No response
Linked PRs
PyInitfunction across multiple interpreters #144602PyInitfunction across multiple interpreters (GH-144602) #144633_testmultiphaseinstead of_testsinglephaseintest_importlib#144769_testmultiphaseinstead of_testsinglephaseintest_importlib(GH-144769) #144877PyInitfunction across multiple interpreters (GH-144602) #144880