Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_free_threading/test_interpreters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import textwrap
import unittest

from test.support import import_helper, script_helper


# Make sure _testinternalcapi is available before running the test.
import_helper.import_module('_testinternalcapi')


class InterpreterTeardownTests(unittest.TestCase):
def test_destroy_subinterpreter_does_not_abort(self):
# gh-153176: On a free-threaded debug build, destroying a
# subinterpreter used to trip an overly-restrictive assertion in
# _PyMem_mi_page_reclaimed and abort the process. Run the
# reproduction in a subprocess so that a regression surfaces as a
# non-zero exit / SIGABRT instead of killing the test runner.
script = textwrap.dedent("""
import _testinternalcapi

interpid = _testinternalcapi.create_interpreter()
_testinternalcapi.destroy_interpreter(interpid, basic=True)
""")
script_helper.assert_python_ok('-c', script)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a crash on free-threaded debug builds when destroying a subinterpreter.
An overly-restrictive assertion in ``_PyMem_mi_page_reclaimed`` wrongly
assumed the reclaimed page's owning thread state always matched the current
thread state, which is not true during stop-the-world events.
4 changes: 3 additions & 1 deletion Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ _PyMem_mi_page_reclaimed(mi_page_t *page)
if (page->qsbr_goal != 0) {
if (mi_page_all_free(page)) {
assert(page->qsbr_node.next == NULL);
// We may be reclaiming a page belonging to a different thread
// during a stop-the-world event. Find the _PyThreadStateImpl for
// the page.
_PyThreadStateImpl *tstate = tstate_from_heap(mi_page_heap(page));
assert(tstate == (_PyThreadStateImpl *)_PyThreadState_GET());
page->retire_expire = 0;
llist_insert_tail(&tstate->mimalloc.page_list, &page->qsbr_node);
}
Expand Down
Loading