-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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-119213: Be More Careful About _PyArg_Parser.kwtuple Across Interpreters #119331
Merged
ericsnowcurrently
merged 9 commits into
python:main
from
ericsnowcurrently:fix-getargs-parser
May 22, 2024
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f0f080b
Identify where the problem is.
ericsnowcurrently c288a71
Add a TODO.
ericsnowcurrently 35abdad
Fix a no-gil bug in _parser_init().
ericsnowcurrently e179765
Add a test for the crash.
ericsnowcurrently e6f78fb
Always make sure kwtuple is allocatoed against the main interpreter.
ericsnowcurrently dbf113b
Add NEWS.
ericsnowcurrently 9b73c3d
Drop a dead line.
ericsnowcurrently 6b9c436
Update global objects.
ericsnowcurrently 9157ad5
Merge branch 'main' into fix-getargs-parser
ericsnowcurrently File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core and Builtins/2024-05-21-11-27-14.gh-issue-119213.nxjxrt.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Non-builtin modules built with argument clinic were crashing if used in a | ||
subinterpreter before the main interpreter. The objects that were causing | ||
the problem by leaking between interpreters carelessly have been fixed. |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||
#include "pycore_dict.h" // _PyDict_HasOnlyStringKeys() | ||||
#include "pycore_modsupport.h" // export _PyArg_NoKeywords() | ||||
#include "pycore_pylifecycle.h" // _PyArg_Fini | ||||
#include "pycore_pystate.h" // _Py_IsMainInterpreter() | ||||
#include "pycore_tuple.h" // _PyTuple_ITEMS() | ||||
#include "pycore_pyerrors.h" // _Py_CalculateSuggestions() | ||||
|
||||
|
@@ -1947,7 +1948,23 @@ _parser_init(void *arg) | |||
int owned; | ||||
PyObject *kwtuple = parser->kwtuple; | ||||
if (kwtuple == NULL) { | ||||
/* We may temporarily switch to the main interpreter to avoid | ||||
* creating a tuple that could outlive its owning interpreter. */ | ||||
PyThreadState *save_tstate = NULL; | ||||
PyThreadState *temp_tstate = NULL; | ||||
if (!_Py_IsMainInterpreter(PyInterpreterState_Get())) { | ||||
temp_tstate = PyThreadState_New(_PyInterpreterState_Main()); | ||||
if (temp_tstate == NULL) { | ||||
return -1; | ||||
} | ||||
save_tstate = PyThreadState_Swap(temp_tstate); | ||||
} | ||||
kwtuple = new_kwtuple(keywords, len, pos); | ||||
if (temp_tstate != NULL) { | ||||
PyThreadState_Clear(temp_tstate); | ||||
(void)PyThreadState_Swap(save_tstate); | ||||
PyThreadState_Delete(temp_tstate); | ||||
} | ||||
if (kwtuple == NULL) { | ||||
return -1; | ||||
} | ||||
|
@@ -1969,8 +1986,8 @@ _parser_init(void *arg) | |||
parser->next = _Py_atomic_load_ptr(&_PyRuntime.getargs.static_parsers); | ||||
do { | ||||
// compare-exchange updates parser->next on failure | ||||
} while (_Py_atomic_compare_exchange_ptr(&_PyRuntime.getargs.static_parsers, | ||||
&parser->next, parser)); | ||||
} while (!_Py_atomic_compare_exchange_ptr(&_PyRuntime.getargs.static_parsers, | ||||
1st1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
&parser->next, parser)); | ||||
return 0; | ||||
} | ||||
|
||||
|
@@ -2883,6 +2900,7 @@ _PyArg_Fini(void) | |||
struct _PyArg_Parser *tmp, *s = _PyRuntime.getargs.static_parsers; | ||||
while (s) { | ||||
tmp = s->next; | ||||
//assert(tmp != s); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||
s->next = NULL; | ||||
parser_clear(s); | ||||
s = tmp; | ||||
|
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
Oops, something went wrong.
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.
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.
Is doing this before checking
if (kwtuple == NULL)
the right way?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.
Yeah, we want to clean up the temporary tstate regardless of the outcome of
new_kwtuple()
.