-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
gh-124160: Pass main_tstate to update_global_state_for_extension() #124164
Conversation
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
I'll review this when I reproduce the issue later today. In the meantime, this needs a NEWS entry. |
Would you be so nice to write one for me? |
I left |
Sure, I'll do that alongside the review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change is correct. In this entire section, the main tstate is always active and should always be targeted.
Before merging this, let's add a regression test that would have caught the reported failure.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index 342cc91cc58..5506ba21948 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -95,6 +95,14 @@ static void _testembed_Py_Initialize(void)
}
+static int test_import_in_subinterpreters(void)
+{
+ _testembed_Py_InitializeFromConfig();
+ PyThreadState_Swap(Py_NewInterpreter());
+ PyRun_SimpleString("import readline"); // gh-124160
+}
+
+
/*****************************************************
* Test repeated initialisation and subinterpreters
*****************************************************/
@@ -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}, does this look ok? |
Looks good, but we should be using |
There are no null checks for Py_NewInterpreter(), PyThreadState_Swap(), PyRun_SimpleString() in the whole file and this test is just crashing w/o this PR. |
Yeah, functions like |
ah indeed, I forgot about the return anyway. |
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
I have made the requested changes; please review again |
Thanks for making the requested changes! @ericsnowcurrently: please review the changes made to this pull request. |
Added short news entry |
Misc/NEWS.d/next/C_API/2024-09-18-18-30-16.gh-issue-124160.wFuEVx.rst
Outdated
Show resolved
Hide resolved
Otherwise it'll always return NULL if tstate != main_tstate due to _Py_IsMainInterpreter() check inside it. Also, add a regression test that makes sure that `readline` is importable in case that triggered the crash before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, with one caveat about the test using the readline module
static int test_import_in_subinterpreters(void) | ||
{ | ||
_testembed_Py_InitializeFromConfig(); | ||
PyThreadState_Swap(Py_NewInterpreter()); | ||
return PyRun_SimpleString("import readline"); // gh-124160 | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I've added the 3.13 backport label, since this is a regression from 3.12. Unfortunately, I don't think this can go in until 3.13.1, as the branch is locked right now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've gone through and confirmed that this fixes importing for all stateful single-phase modules. Thank you for the fix, @luk1337!
Thanks @luk1337 for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13. |
…n() (pythonGH-124164) (cherry picked from commit 7331d0f) Co-authored-by: luk1337 <priv.luk@gmail.com>
GH-124250 is a backport of this pull request to the 3.13 branch. |
Otherwise it'll always return NULL if tstate != main_tstate due to _Py_IsMainInterpreter() check inside it.
Also, add a regression test that makes sure that
readline
is importable in case that triggered the crash before.