Skip to content

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

Draft
abdulm5 wants to merge 1 commit into
python:mainfrom
abdulm5:agent/gh-155504-threadstate-interp
Draft

gh-155504: Set the interpreter before fallible thread state initialization#155590
abdulm5 wants to merge 1 commit into
python:mainfrom
abdulm5:agent/gh-155504-threadstate-interp

Conversation

@abdulm5

@abdulm5 abdulm5 commented Aug 11, 2026

Copy link
Copy Markdown

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

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

1 participant