-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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-115649: Copy the filename into main interpreter before intern in import.c #120315
Merged
Conversation
This file contains 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
aisk
changed the title
Copy the filename into main interpreter before intern in import.c
gh-115649: Copy the filename into main interpreter before intern in import.c
Jun 10, 2024
Can you add a test for it? |
Thanks for the review! I added the test which will crash the interpreter before this pull request. |
kumaraditya303
approved these changes
Jun 17, 2024
Thanks @aisk for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13. |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Jun 17, 2024
…n in import.c (pythonGH-120315) (cherry picked from commit 28140d1) Co-authored-by: AN Long <aisk@users.noreply.github.com> Co-authored-by: Kumar Aditya <kumaraditya@python.org>
GH-120652 is a backport of this pull request to the 3.13 branch. |
FWIW, the fix looks good. Thanks for taking care of this, @aisk. |
mrahtz
pushed a commit
to mrahtz/cpython
that referenced
this pull request
Jun 30, 2024
…n in import.c (python#120315) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
noahbkim
pushed a commit
to hudson-trading/cpython
that referenced
this pull request
Jul 11, 2024
…n in import.c (python#120315) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
estyxx
pushed a commit
to estyxx/cpython
that referenced
this pull request
Jul 17, 2024
…n in import.c (python#120315) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
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.
Test code:
Execute result:
In current main branch, when a single-phase extension (
_tkinter
is an example) got imported, it'sinit
function will be execute in the main interpreter, although the import statement is executed in the sub-interpreter.And there is a workaround code to intern the
filename
object to the main interpreter's interned string dict:cpython/Python/import.c
Lines 1970 to 1973 in c3b6dbf
I think the
filename
is created in the sub-interpreter in this test code, and it's allocated with the sub-interpreter's obmalloc state, which will be freed when the sub-interpreter is destroyed by statement_interpreters.destroy(iid)
in the test code.When the main interpreter shutdown, it will try to free the interned string dict, this will cause issues and crash the interpreter.
Using gdb to dump the call stack:
Please note that the
#12
frame#12 0x00005555556ac9ad in dictkeys_decref (interp=interp@entry=0x555555b17798 <_PyRuntime+101656>, dk=dk@entry=0x555555c51800, use_qsbr=use_qsbr@entry=false) at Objects/dictobject.c:492
, it's code is:cpython/Objects/dictobject.c
Line 492 in c3b6dbf
We can't inspect the value here because it's memory got destroyed already. But adding these codes to L492:
We can see that when the interpreter crashed, it's calling
_Py_Dealloc
onfilename
string object:Clone the
filename
and re-create it on the main interpreter can avoid the crash.import _tkinter
leads to shutdown crash #115649