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

@Ijtihed Ijtihed 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-bot Bot 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

"""
rc, out, err = assert_python_failure("-c", code)
self.assertIn(rc, (1, 120))
self.assertIn(b'MemoryError', err)

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.

Can we not make this a try/except?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

skip is now handled inside the test trough import_helper.import_module

Comment thread Lib/test/test_warnings/__init__.py Outdated
# Python built with Py_TRACE_REFS fail with a fatal error in
# _PyRefchain_Trace() on memory allocation error.
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
@unittest.skipIf(_testcapi is None, "requires _testcapi")

@StanFromIreland StanFromIreland Jun 20, 2026

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.

This can be simplified to running:

# Skip this test if the _testcapi module isn't available.
_testcapi = import_helper.import_module('_testcapi')

In the test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

@Ijtihed Ijtihed force-pushed the fix/gh-151673-warn-nomemory-crash branch from 6866bc9 to e01d44b Compare June 20, 2026 11:19
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.

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

2 participants