Skip to content

gh-153176: Fix crash in _PyMem_mi_page_reclaimed on free-threaded debug builds#153307

Open
zangjiucheng wants to merge 1 commit into
python:mainfrom
zangjiucheng:gh-153176-fix
Open

gh-153176: Fix crash in _PyMem_mi_page_reclaimed on free-threaded debug builds#153307
zangjiucheng wants to merge 1 commit into
python:mainfrom
zangjiucheng:gh-153176-fix

Conversation

@zangjiucheng

Copy link
Copy Markdown
Contributor

Summary

Fixes #153176.

On free-threaded debug builds, destroying a subinterpreter could abort the
process with:

Assertion `tstate == (_PyThreadStateImpl *)_PyThreadState_GET()' failed.

_PyMem_mi_page_reclaimed() asserted that the page being reclaimed always
belongs to the current thread state. That assumption doesn't hold during
stop-the-world events (e.g. subinterpreter teardown), where a page can be
reclaimed on behalf of a different thread state than the one currently
running. The function already looks up the correct owning thread state via
tstate_from_heap(mi_page_heap(page)) — it just also asserted, incorrectly,
that this always equals the current thread state. This mirrors the reasoning
in _PyMem_mi_page_maybe_free(), which does not make the same assumption.

This removes the overly-restrictive assertion and adds a regression test
that reproduces the crash via _testinternalcapi.create_interpreter() /
destroy_interpreter(), run in a subprocess so a regression fails the test
instead of aborting the whole test runner.

Test plan

  • ./python -m test test_free_threading.test_interpreters -v
  • Reproduced the crash on main prior to the fix; confirmed it no
    longer reproduces with this change on a free-threaded debug build.

@weixlu

weixlu commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

I also think we can just relax this assertion. Thanks for your fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assertion triggered in MiMalloc (free-threaded build)

2 participants