gh-151763: Fix OOM cleanup of initial thread state#152165
Open
zainnadeem786 wants to merge 1 commit into
Open
Conversation
ZeroIntensity
requested changes
Jun 25, 2026
| } | ||
| reset_threadstate(tstate); | ||
| } | ||
| tstate->base.interp = interp; |
Member
There was a problem hiding this comment.
This should happen in new_threadstate, not in the allocator.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses OOM-0020 from gh-151763.
It fixes a free-threaded crash during interpreter creation when memory allocation fails after the preallocated initial thread state has been selected but before
init_threadstate()initializes its interpreter back-pointer.Issue
In the free-threaded build,
_interpreters.create(reqrefs=True)can fail during early thread-state allocation setup.The affected path is:
For the initial thread state,
alloc_threadstate()obtains memory embedded inPyInterpreterStaterather than heap-allocated memory.However, if a later free-threaded allocation fails before
init_threadstate()runs, cleanup reachesfree_threadstate()whiletstate->base.interpis still unset.As a result,
free_threadstate()fails to recognize the preallocated initial thread state and may incorrectly callPyMem_RawFree()on embedded memory.This can result in:
_PyMem_DebugRawFree: bad IDin debug buildsFix
Initialize the interpreter back-pointer earlier in
alloc_threadstate():This ensures that cleanup paths can correctly identify the preallocated initial thread state even if a later allocation fails before
init_threadstate()is reached.The successful path remains unchanged because
init_threadstate()already sets the same value later.Validation
Validated locally on Windows.
Builds completed successfully:
Before the fix, the OOM-0020 reproducer crashed in the free-threaded build with:
After the fix, the same allocation-failure path reaches
MemoryErrorinstead of crashing.Focused tests passed:
git diff --checkalso passes.Regression Test
No regression test is included.
The reproducer depends on
_testcapi.set_nomemory()in a free-threaded build, and the exact allocation index is build-sensitive. This follows the recent maintainer direction for these OOM-path fixes, where a focused code fix is preferable to fragile allocation-index regression tests.Compatibility
This is a localized initialization-order fix for an OOM cleanup path.
Normal successful interpreter creation keeps the same final state, because
init_threadstate()already assigns the same interpreter pointer later.Addresses OOM-0020 from gh-151763.