Skip to content

gh-124160: Pass main_tstate to update_global_state_for_extension() #124164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash when importing modules containing state and single-phase
initialization in a subinterpreter.
9 changes: 9 additions & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ static void _testembed_Py_Initialize(void)
}


static int test_import_in_subinterpreters(void)
{
_testembed_Py_InitializeFromConfig();
PyThreadState_Swap(Py_NewInterpreter());
return PyRun_SimpleString("import readline"); // gh-124160
}
Comment on lines +98 to +103
Copy link
Member

Choose a reason for hiding this comment

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

This works, but only as long as the readline module fits the relevant criteria. That's probably fine.

Ideally we would add a new test module for this dedicated case, though we could probably reuse _testsinglephase_with_reinit (in Modules/_testsinglephase.c). If you think that would take much time, I'm fine with addressing that separately, so we can get this PR merged sooner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would prefer if someone else figured out how to set up a more robust test case for this issue in a separate PR.



/*****************************************************
* Test repeated initialisation and subinterpreters
*****************************************************/
Expand Down Expand Up @@ -2398,6 +2406,7 @@ static struct TestCase TestCases[] = {
{"test_repeated_init_exec", test_repeated_init_exec},
{"test_repeated_simple_init", test_repeated_simple_init},
{"test_forced_io_encoding", test_forced_io_encoding},
{"test_import_in_subinterpreters", test_import_in_subinterpreters},
{"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
{"test_repeated_init_and_inittab", test_repeated_init_and_inittab},
{"test_pre_initialization_api", test_pre_initialization_api},
Expand Down
2 changes: 1 addition & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
singlephase.m_init = p0;
}
cached = update_global_state_for_extension(
tstate, info->path, info->name, def, &singlephase);
main_tstate, info->path, info->name, def, &singlephase);
if (cached == NULL) {
assert(PyErr_Occurred());
goto main_finally;
Expand Down
Loading