Skip to content

gh-151112: Fix crash in compiler_mod() when entering the current compilation unit fails#151234

Open
StanFromIreland wants to merge 1 commit into
python:mainfrom
StanFromIreland:compiler_mod
Open

gh-151112: Fix crash in compiler_mod() when entering the current compilation unit fails#151234
StanFromIreland wants to merge 1 commit into
python:mainfrom
StanFromIreland:compiler_mod

Conversation

@StanFromIreland

@StanFromIreland StanFromIreland commented Jun 10, 2026

Copy link
Copy Markdown
Member

_PyCodegen_EnterAnonymousScope can fail to enter the module's compilation unit (e.g. when OOM, like the in repro), so c->u is left NULL. compiler_mod's cleanup then calls _PyCompile_ExitScope, which derefs that NULL unit in compiler_unit_free and segfaults.

The reproducers start/stop for nomemory will probably have to be adjusted, but for me I can reproduce with:

import _testcapi

compile("pass", "<warmup>", "single")
_testcapi.set_nomemory(31, 32)
try:
    compile("pass", "<trigger>", "single")
except MemoryError:
    pass
first = bytes(128)
second = bytes(128)

Skipping news, the other PR has one which I think can cover this too, these are both quite rare.

Comment thread Python/compile.c
co = _PyCompile_OptimizeAndAssemble(c, addNone);
finally:
_PyCompile_ExitScope(c);
if (c->u != NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure c->u was NULL when we entered compiler_mod? Can we assert that at the top of the function?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants