Skip to content

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

Open
zangjiucheng wants to merge 2 commits 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 2 commits 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!

Comment thread Objects/obmalloc.c Outdated
…ent thread state

_testinternalcapi.destroy_interpreter(basic=True) called PyThreadState_Clear()
and PyThreadState_Delete() on t1 while t2 was the current thread state. On a
free-threaded debug build this reclaimed mimalloc pages into a heap not owned
by the current thread, tripping the assertion in _PyMem_mi_page_reclaimed().

Leave t1 for Py_EndInterpreter() to clean up. Add a regression test.

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Xiaowei Lu <weixlu420302@gmail.com>
@kumaraditya303

Copy link
Copy Markdown
Contributor

The PR title and description needs to be updated.

@@ -0,0 +1,3 @@
Fix ``_testinternalcapi.destroy_interpreter()`` calling

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

News entry is not needed because it is test code.

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)

4 participants