Skip to content
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

Merged
merged 1 commit into from
Sep 19, 2024

Conversation

luk1337
Copy link
Contributor

@luk1337 luk1337 commented Sep 17, 2024

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.

Copy link

cpython-cla-bot bot commented Sep 17, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Sep 17, 2024

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 skip news label instead.

@ZeroIntensity
Copy link
Member

I'll review this when I reproduce the issue later today. In the meantime, this needs a NEWS entry.

@luk1337
Copy link
Contributor Author

luk1337 commented Sep 18, 2024

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?

@luk1337
Copy link
Contributor Author

luk1337 commented Sep 18, 2024

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 Allow edits by maintainers enabled so you can even push to my branch.

@ZeroIntensity
Copy link
Member

Sure, I'll do that alongside the review.

Copy link
Member

@ericsnowcurrently ericsnowcurrently left a 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.

@bedevere-app
Copy link

bedevere-app bot commented Sep 18, 2024

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@luk1337
Copy link
Contributor Author

luk1337 commented Sep 18, 2024

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.

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?

@ZeroIntensity
Copy link
Member

Looks good, but we should be using NULL checks in tests.

@luk1337
Copy link
Contributor Author

luk1337 commented Sep 18, 2024

Looks good, but we should be using NULL checks in tests.

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.

@ZeroIntensity
Copy link
Member

Yeah, functions like PyRun_SimpleString can fail. In that case, we don't want to segfault the tests, we should just return -1 and let the error propagate.

@luk1337
Copy link
Contributor Author

luk1337 commented Sep 18, 2024

Yeah, functions like PyRun_SimpleString can fail. In that case, we don't want to segfault the tests, we should just return -1 and let the error propagate.

ah indeed, I forgot about the return anyway.

@bedevere-app
Copy link

bedevere-app bot commented Sep 18, 2024

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 skip news label instead.

@luk1337
Copy link
Contributor Author

luk1337 commented Sep 18, 2024

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Sep 18, 2024

Thanks for making the requested changes!

@ericsnowcurrently: please review the changes made to this pull request.

@luk1337
Copy link
Contributor Author

luk1337 commented Sep 18, 2024

Added short news entry

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.
Copy link
Member

@ericsnowcurrently ericsnowcurrently left a 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

Comment on lines +98 to +103
static int test_import_in_subinterpreters(void)
{
_testembed_Py_InitializeFromConfig();
PyThreadState_Swap(Py_NewInterpreter());
return PyRun_SimpleString("import readline"); // gh-124160
}
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.

@ZeroIntensity
Copy link
Member

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.

Copy link
Member

@ZeroIntensity ZeroIntensity left a 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!

@kumaraditya303 kumaraditya303 merged commit 7331d0f into python:main Sep 19, 2024
39 of 40 checks passed
@miss-islington-app
Copy link

Thanks @luk1337 for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 19, 2024
…n() (pythonGH-124164)

(cherry picked from commit 7331d0f)

Co-authored-by: luk1337 <priv.luk@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Sep 19, 2024

GH-124250 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Sep 19, 2024
@luk1337 luk1337 deleted the luk/import branch September 19, 2024 16:06
luk1337 added a commit to luk1337/cpython that referenced this pull request Sep 19, 2024
savannahostrowski pushed a commit to savannahostrowski/cpython that referenced this pull request Sep 22, 2024
Yhg1s pushed a commit that referenced this pull request Sep 23, 2024
…on() (GH-124164) (#124250)

gh-124160: Pass main_tstate to update_global_state_for_extension() (GH-124164)
(cherry picked from commit 7331d0f)

Co-authored-by: luk1337 <priv.luk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants