Skip to content

gh-155504: Set the interpreter before fallible thread state initialization - #155590

Merged
kumaraditya303 merged 3 commits into
python:mainfrom
abdulm5:agent/gh-155504-threadstate-interp
Aug 16, 2026
Merged

gh-155504: Set the interpreter before fallible thread state initialization#155590
kumaraditya303 merged 3 commits into
python:mainfrom
abdulm5:agent/gh-155504-threadstate-interp

Conversation

@abdulm5

@abdulm5 abdulm5 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

alloc_threadstate() returned a reset thread state with base.interp still
set to NULL. If one of the later PyStats, TLBC, or QSBR initialization steps
failed, new_threadstate() passed that partially initialized state to
free_threadstate(). The cleanup path needs the interpreter both to identify
the embedded initial thread state and to release interpreter-owned resources.

Set base.interp in alloc_threadstate() before any later initialization can
fail, so every error path can safely free or recycle the thread state.

The change also adds a Core and Builtins NEWS entry.

Fixes #155504.

Tests

  • Py_DEBUG build:
    ./python.exe -m test -j4 --timeout=180 test_capi test_interpreters test_threading test_embed
    (2,001 tests passed)
  • Py_DEBUG free-threaded build with PyStats:
    ./python.exe -m test -j4 --timeout=180 test_pystats test_capi test_interpreters test_threading test_embed
    (1,836 tests passed; test_interpreters was skipped because the GIL was disabled)
  • Temporary failure injection verified cleanup for both the embedded/preallocated
    and heap-allocated thread state paths; the injection was removed afterward.
  • git diff --check

@python-cla-bot

python-cla-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@kumaraditya303

Copy link
Copy Markdown
Contributor

You need to sign CLA

@kumaraditya303
kumaraditya303 merged commit 94a75f7 into python:main Aug 16, 2026
54 checks passed
@kumaraditya303 kumaraditya303 added needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 16, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @abdulm5 for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app

Copy link
Copy Markdown

Thanks @abdulm5 for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Aug 16, 2026

Copy link
Copy Markdown

GH-155884 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 16, 2026
@bedevere-app

bedevere-app Bot commented Aug 16, 2026

Copy link
Copy Markdown

GH-155885 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 16, 2026
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.

Logic bug in new_threadstate() error path, interp is NULL

2 participants