Uh oh!
There was an error while loading. Please reload this page.
gh-151763: Fix OOM cleanup of initial thread state - #152165
gh-151763: Fix OOM cleanup of initial thread state#152165zainnadeem786 wants to merge 3 commits into
Conversation
Uh oh!
There was an error while loading. Please reload this page.
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 |
ZeroIntensity
left a comment
There was a problem hiding this comment.
Thanks. This is technically user-facing, so please add a news entry.
Uh oh!
There was an error while loading. Please reload this page.
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.