Title: gh-151763: Fix OOM cleanup of initial thread state by zainnadeem786 · Pull Request #152155 · python/cpython · GitHub
Open Graph Title: gh-151763: Fix OOM cleanup of initial thread state by zainnadeem786 · Pull Request #152155 · python/cpython
X Title: gh-151763: Fix OOM cleanup of initial thread state by zainnadeem786 · Pull Request #152155 · python/cpython
Description: 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: _interpreters.create(reqrefs=True) -> _interpreters_create_impl() -> _PyXI_NewInterpreter() -> Py_NewInterpreterFromConfig() -> new_interpreter() -> _PyThreadState_New() -> new_threadstate() -> alloc_threadstate() -> _Py_qsbr_reserve() / _Py_ReserveTLBCIndex() -> free_threadstate() For the initial thread state, alloc_threadstate() obtains memory embedded in PyInterpreterState rather than heap-allocated memory. However, if a later free-threaded allocation fails before init_threadstate() runs, cleanup reaches free_threadstate() while tstate->base.interp is still unset. As a result, free_threadstate() fails to recognize the preallocated initial thread state and may incorrectly call PyMem_RawFree() on embedded memory. This can result in: _PyMem_DebugRawFree: bad ID in debug builds access violation in release builds Fix Initialize the interpreter back-pointer earlier in alloc_threadstate(): tstate->base.interp = interp; 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: PCbuild\build.bat -p x64 -c Debug --disable-gil PCbuild\build.bat -p x64 -c Release --disable-gil PCbuild\build.bat -p x64 -c Debug Before the fix, the OOM-0020 reproducer crashed in the free-threaded build with: _PyMem_DebugRawFree: bad ID After the fix, the same allocation-failure path reaches MemoryError instead of crashing. Focused tests passed: PCbuild/amd64t/python_d.exe -m test test__interpreters PCbuild/amd64/python_d.exe -m test test__interpreters git diff --check also 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.
Open Graph Description: 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 selec...
X Description: 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 selec...
Opengraph URL: https://github.com/python/cpython/pull/152155
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:49cbe6bf-d8aa-76f0-ec50-54998612abad |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | CB80:2FC742:FD8DFB:1528E31:6A533476 |
| html-safe-nonce | b5e989fc2145c7ac60b4931072fc220dd94b26888abd46e4a92593ebec7aa508 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjgwOjJGQzc0MjpGRDhERkI6MTUyOEUzMTo2QTUzMzQ3NiIsInZpc2l0b3JfaWQiOiI0MDUzMDEzNDcxOTY0NjQwMzc0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6cb49cfa733bedecd99e5bc9df4d4b84afecdbeba23a5e739dafbe3b07e88f66 |
| hovercard-subject-tag | pull_request:3933152282 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/152155/files |
| twitter:image | https://avatars.githubusercontent.com/u/165761530?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/165761530?s=400&v=4 |
| og:image:alt | 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 selec... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width