]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.13] gh-119213: Be More Careful About _PyArg_Parser.kwtuple Across Interpreters...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 22 May 2024 18:09:48 +0000 (20:09 +0200)
committerGitHub <noreply@github.com>
Wed, 22 May 2024 18:09:48 +0000 (12:09 -0600)
commitbfd9c3ea5336f2898e3c58e0c3999bfc25d7da89
tree9ac0fd005fe5c7754cbb9250518afed10dbfa79b
parenta463cd8e45d6fc6ca16a59bcfeece99cf268e684
[3.13] gh-119213: Be More Careful About _PyArg_Parser.kwtuple Across Interpreters (gh-119331) (gh-119410)

_PyArg_Parser holds static global data generated for modules by Argument Clinic.  The _PyArg_Parser.kwtuple field is a tuple object, even though it's stored within a static global.  In some cases the tuple is statically allocated and thus it's okay that it gets shared by multiple interpreters.  However, in other cases the tuple is set lazily, allocated from the heap using the active interprepreter at the point the tuple is needed.

This is a problem once that interpreter is destroyed since _PyArg_Parser.kwtuple becomes at dangling pointer, leading to crashes.  It isn't a problem if the tuple is allocated under the main interpreter, since its lifetime is bound to the lifetime of the runtime.  The solution here is to temporarily switch to the main interpreter.  The alternative would be to always statically allocate the tuple.

This change also fixes a bug where only the most recent parser was added to the global linked list.

(cherry picked from commit 81865002aee8eaaeb3c7e402f86183afa6de77bf)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Include/internal/pycore_global_objects_fini_generated.h
Include/internal/pycore_global_strings.h
Include/internal/pycore_runtime_init_generated.h
Include/internal/pycore_unicodeobject_generated.h
Lib/test/test_capi/test_getargs.py
Misc/NEWS.d/next/Core and Builtins/2024-05-21-11-27-14.gh-issue-119213.nxjxrt.rst [new file with mode: 0644]
Modules/_testinternalcapi.c
Modules/clinic/_testinternalcapi.c.h
Python/getargs.c
Tools/clinic/libclinic/parse_args.py