gh-153176: Fix destroy_interpreter() test helper clearing a non-current thread state#153307
Open
zangjiucheng wants to merge 3 commits into
Open
gh-153176: Fix destroy_interpreter() test helper clearing a non-current thread state#153307zangjiucheng wants to merge 3 commits into
zangjiucheng wants to merge 3 commits into
Conversation
Contributor
|
I also think we can just relax this assertion. Thanks for your fix! |
…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>
Contributor
|
The PR title and description needs to be updated. |
| @@ -0,0 +1,3 @@ | |||
| Fix ``_testinternalcapi.destroy_interpreter()`` calling | |||
Contributor
There was a problem hiding this comment.
News entry is not needed because it is test code.
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
Fixes #153176.
On free-threaded debug builds,
_testinternalcapi.destroy_interpreter(id, basic=True)aborted the process with:
The root cause is in the test helper, not in obmalloc.
destroy_interpreter()calls
PyThreadState_Clear(t1)/PyThreadState_Delete(t1)whilet2is thecurrent thread state. Clearing a non-current thread state runs
_PyThreadState_ClearMimallocHeaps(t1), and on a debug buildmi_heap_collect_ex()treatsMI_ABANDONas>= MI_FORCE, so it calls_mi_abandoned_reclaim_all()and reclaims abandoned pages back intot1'sheaps. That reaches
_PyMem_mi_page_reclaimed(), where the reclaimed pagebelongs to
t1but the current thread state ist2, so the assertion fires.The assertion is correct — reclaim should only happen into the current thread's
own heap — so this keeps it and fixes the helper instead:
t1is left forPy_EndInterpreter()to clean up, matching the rule thatPyThreadState_Clear()must be called on the current thread. Thanks @kumaraditya303 for pinpointing the
root cause.
Test plan
./python -m test test_free_threading.test_interpreters -vdebug build before the fix; confirmed it no longer reproduces after.