Skip to content

gh-151673: Fix crash in warnings.warn() under memory pressure - #151767

Open
Ijtihed wants to merge 1 commit into
python:mainfrom
Ijtihed:fix/gh-151673-warn-nomemory-crash
Open

gh-151673: Fix crash in warnings.warn() under memory pressure#151767
Ijtihed wants to merge 1 commit into
python:mainfrom
Ijtihed:fix/gh-151673-warn-nomemory-crash

Conversation

@Ijtihed

@IjtihedIjtihed commented Jun 20, 2026

Copy link
Copy Markdown

When warnings.warn() runs while every allocation is failing setup_context() took the f == NULL branch and called PyUnicode_FromString("<sys>") without checking the result. The resulting NULL filename was then passed to Py_DECREF either by do_warn() on the success path or by setup_context()'s own handle_error: label (which used Py_DECREF, not Py_XDECREF). This caused a segfault.

The fix initialize the output references up front then check the "<sys>" allocation and use Py_XDECREF in the error path.

Fixes#151673

@python-cla-bot

python-cla-botBot commented Jun 20, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@Ijtihed

Ijtihed commented Jun 20, 2026

Copy link
Copy Markdown
Author

the ci failure was preexisting on main. i can update branch when needed

Comment threadLib/test/test_warnings/__init__.py
Comment threadLib/test/test_warnings/__init__.py Outdated
@Ijtihed
Ijtihedforce-pushed the fix/gh-151673-warn-nomemory-crash branch from 6866bc9 to e01d44bCompareJune 20, 2026 11:19
@Ijtihed

Copy link
Copy Markdown
Author

r? @StanFromIreland

Sign up for freeto 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.

Segfault: Py_DECREF(NULL) in setup_context/do_warn (_warnings.c) when emitting a warning under MemoryError

2 participants

@Ijtihed@StanFromIreland