Skip to content

gh-151673: Fix crash in warnings setup_context when filename alloc fails#154715

Closed
Vamsi-klu wants to merge 1 commit into
python:mainfrom
Vamsi-klu:fix-gh151673-warnings-filename-oom
Closed

gh-151673: Fix crash in warnings setup_context when filename alloc fails#154715
Vamsi-klu wants to merge 1 commit into
python:mainfrom
Vamsi-klu:fix-gh151673-warnings-filename-oom

Conversation

@Vamsi-klu

Copy link
Copy Markdown

Summary

Fix OOM-0001 (gh-151673 / umbrella gh-151763): crash in the warnings module when allocating the synthetic "<sys>" filename fails under memory pressure.


What the issue is

In setup_context (Python/_warnings.c), when no frame is available:

*filename = PyUnicode_FromString("<sys>");
*lineno = 0;
// NO NULL CHECK

If that allocation returns NULL, setup still proceeds / returns success-ish paths that later Py_DECREF(*filename) in do_warn or handle_error on a NULL/invalid pointer. The sibling "<string>" module path already checks for NULL; "<sys>" did not.


Why I solved it that way

  1. Fail at the source — check immediately after PyUnicode_FromString.
  2. return 0 rather than goto handle_error on this path, because at that moment nothing else has been allocated (*registry / *module not set yet), and handle_error historically did Py_DECREF(*filename) which is unsafe when *filename is NULL.
  3. Leave the "<string>" path alone — it already has the correct check.
  4. Minimal, mechanical — matches the shape of other Family-C OOM fixes under CPython crashes on memory-allocation failure (OOM): 35 findings (4 dups → 31 distinct) #151763.

How I did it

*filename = PyUnicode_FromString("<sys>");
*lineno = 0;
if (*filename == NULL) {
    return 0;  /* nothing else allocated yet on this path */
}

NEWS entry under Core and Builtins for gh-151673.


Impact

  • Stops a crash when issuing warnings under extreme memory pressure with no Python frame (e.g. very early / finalization-adjacent paths using sys dict as globals).
  • No behavioral change when allocations succeed.

Testing plan

  • Rebuild debug interpreter.
  • ./python -m test test_warnings — PASS.
  • Smoke: warnings.warn("x") — OK.
  • Optional nomemory-targeted test (not included; can add on request).
  • CI matrix.

Everything else


Requested reviewers (subject-matter experts)

Could the following SMEs take a look when convenient (I cannot formally request reviews from this fork account):

Thank you!

…loc fails

Check PyUnicode_FromString(\"<sys>\") for NULL before continuing
(OOM-0001 / pythongh-151763).
@python-cla-bot

python-cla-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 2d6f1ffb20

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Vamsi-klu
Vamsi-klu marked this pull request as draft July 26, 2026 08:01
@picnixz picnixz closed this Jul 26, 2026
@picnixz

picnixz commented Jul 26, 2026

Copy link
Copy Markdown
Member
  1. Do not just open PRs by agents. It is against our policy. Should you continue in doing so, we will restrict your access.
  2. There is already wn open PR. Do not create duplicates.

@Vamsi-klu

Copy link
Copy Markdown
Author

Thank you so much for taking the time to review this and for the clear feedback. I really appreciate it.

I’m sorry for opening this PR with an agent and for creating a duplicate when there was already an open PR. That was against CPython’s policy, and I take full responsibility. I understand why that matters for the project and for maintainers’ time.

I’ve tightened my process so this doesn’t happen again: I won’t open CPython PRs via agents, I’ll always check for existing PRs before filing anything new, and I’ve added stricter guidelines for my agent so it doesn’t invent or submit work like this on my behalf.

I won’t repeat this. Thanks again for the patience and for looking out for the project’s standards. I’m grateful you flagged it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants