gh-119213: Be More Careful About _PyArg_Parser.kwtuple Across Interpreters (gh-119331)

_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.
This commit is contained in:
Eric Snow 2024-05-22 11:57:52 -04:00 committed by GitHub
parent d472b4f9fa
commit 81865002ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 144 additions and 3 deletions

View file

@ -4,11 +4,17 @@ import string
import sys
from test import support
from test.support import import_helper
from test.support import script_helper
from test.support import warnings_helper
# Skip this test if the _testcapi module isn't available.
_testcapi = import_helper.import_module('_testcapi')
from _testcapi import getargs_keywords, getargs_keyword_only
try:
import _testinternalcapi
except ImportError:
_testinternalcapi = NULL
# > How about the following counterproposal. This also changes some of
# > the other format codes to be a little more regular.
# >
@ -1346,6 +1352,33 @@ class ParseTupleAndKeywords_Test(unittest.TestCase):
"argument 1 must be sequence of length 1, not 0"):
parse(((),), {}, '(' + f + ')', ['a'])
@unittest.skipIf(_testinternalcapi is None, 'needs _testinternalcapi')
def test_gh_119213(self):
rc, out, err = script_helper.assert_python_ok("-c", """if True:
from test import support
script = '''if True:
import _testinternalcapi
_testinternalcapi.gh_119213_getargs(spam='eggs')
'''
config = dict(
allow_fork=False,
allow_exec=False,
allow_threads=True,
allow_daemon_threads=False,
use_main_obmalloc=False,
gil=2,
check_multi_interp_extensions=True,
)
rc = support.run_in_subinterp_with_config(script, **config)
assert rc == 0
# The crash is different if the interpreter was not destroyed first.
#interpid = _testinternalcapi.create_interpreter()
#rc = _testinternalcapi.exec_interpreter(interpid, script)
#assert rc == 0
""")
self.assertEqual(rc, 0)
if __name__ == "__main__":
unittest.main()