8000 gh-120838: Add Tests For "Bad" Usage in Runtime Lifecycle Operations by ericsnowcurrently · Pull Request #120840 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-120838: Add Tests For "Bad" Usage in Runtime Lifecycle Operations #120840

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4e8a976
Add the option to not reuse the initial tstate.
ericsnowcurrently Jun 20, 2024
120c2a6
Add a test to verify the initial "main" tstate isn't special.
ericsnowcurrently Jun 20, 2024
608139b
Add tests to check different "bad" runtime fini situations.
ericsnowcurrently Jun 20, 2024
592b214
Fix the code in test_audit_subinterpreter.
ericsnowcurrently Jun 20, 2024
e6f31ff
Always try reusing the main tstate (and drop interp.reuse_init_tstate).
ericsnowcurrently Jun 21, 2024
05c0cf3
Skip test_fini_in_subthread on Windows.
ericsnowcurrently Jun 25, 2024
8b241d2
Expect a failure on Windows.
ericsnowcurrently Jun 25, 2024
b66c2b7
Fail if PyFinalize() fails.
ericsnowcurrently Jun 25, 2024
88b1679
Add _Py_Finalize() and struct pyfinalize_args.
ericsnowcurrently Jun 20, 2024
d9d0e10
Use _Py_Finalize() in the new tests.
ericsnowcurrently Jun 21, 2024
72e961f
Factor out resolve_final_tstate().
ericsnowcurrently Jun 25, 2024
e2392c2
Fix the Windows returncode.
ericsnowcurrently Jun 25, 2024
6b1eb57
Export _Py_Finalize().
ericsnowcurrently Jun 25, 2024
7b2afb6
Do not print the error messages with Py_Exit().
ericsnowcurrently Jun 25, 2024
155dddc
Rename the macro.
ericsnowcurrently Jun 25, 2024
f37bcc7
Fix the macro.
ericsnowcurrently Jun 25, 2024
0cffd32
Merge branch 'main' into tests-runtime-lifecycle-bad-usage
ericsnowcurrently Jun 26, 2024
361d477
Fix tests.
ericsnowcurrently Jun 26, 2024
d3beea4
Tweaks to resolve_final_tstate().
ericsnowcurrently Jun 27, 2024
d78f655
Fix the test failures.
ericsnowcurrently Jun 27, 2024
e5c5a5f
More tweaking resolve_final_tstate().
ericsnowcurrently Jun 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Always try reusing the main tstate (and drop interp.reuse_init_tstate).
  • Loading branch information
ericsnowcurrently committed Jun 21, 2024
commit e6f31ff2b5e133a4d854e1bc264092fe89ce15b3
4 changes: 0 additions & 4 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ struct _is {
PyThreadState *head;
/* The thread currently executing in the __main__ module, if any. */
PyThreadState *main;
#ifdef Py_DEBUG
/* For testing (_testembed, etc.). */
int reuse_init_tstate;
#endif
/* Used in Modules/_threadmodule.c. */
Py_ssize_t count;
/* Support for runtime thread stack size tuning.
Expand Down
22 changes: 11 additions & 11 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,17 @@ def test_repeated_init_and_inittab(self):
self.assertEqual(out, lines)

def test_replace_main_tstate(self):
with self.subTest(exec=False):
self.run_embedded_interpreter(
'test_replace_main_tstate',
)

with self.subTest(exec=True):
out, _ = self.run_embedded_interpreter(
'test_replace_main_tstate',
'print("spam!")'
)
self.assertEqual(out.strip(), 'spam!')
for reuse in (True, False):
with self.subTest(reuse=reuse, exec=False):
self.run_embedded_interpreter(
'test_replace_main_tstate',
)
with self.subTest(reuse=reuse, exec=True):
out, _ = self.run_embedded_interpreter(
'test_replace_main_tstate',
'print("spam!")'
)
self.assertEqual(out.strip(), 'spam!')

def test_fini_in_subthread(self):
self.run_embedded_interpreter('test_fini_in_subthread')
Expand Down
47 changes: 31 additions & 16 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,38 +216,58 @@ static int test_replace_main_tstate(void)
{
int err = 0;

int reuse = 0;
int hascode = 0;
for (int i = 2; i < main_argc; i++) {
if (strncmp(main_argv[i], "--", 2) == 0) {
if (strcmp(main_argv[i], "--reuse") == 0) {
reuse = 1;
}
else if (strncmp(main_argv[i], "--", 2) == 0) {
fprintf(stderr, "ERROR: unsupported arg %s\n", main_argv[i]);
err = 1;
}
else {
hascode = 1;
}
if (err) {
fprintf(stderr, "usage: %s test_replace_main_tstate [CODE ...]\n",
fprintf(stderr,
"usage: %s test_replace_main_tstate --reuse [CODE ...]\n",
PROGRAM);
return 1;
}
}

#ifdef Py_DEBUG
_testembed_Py_InitializeFromConfig();
PyThreadState *main_tstate = PyThreadState_Get();
PyInterpreterState *main_interp = main_tstate->interp;
assert(_Py_IsMainInterpreter(main_interp));

// If the initial tstate is reused then there are slightly
// different possible failure paths through the code than if we got
// a completely new one.

PyThreadState *tstate = NULL;
if (!reuse) {
// The initial thread state is still alive,
// so this will create a completely new one,
// with its own distinct pointer.
tstate = PyThreadState_New(main_interp);
assert(tstate != NULL);
assert(tstate != main_tstate);
}
PyThreadState_Clear(main_tstate);
PyThreadState_DeleteCurrent();
assert(PyInterpreterState_ThreadHead(main_interp) == NULL);

main_interp->threads.reuse_init_tstate = 0;
assert(reuse == (PyInterpreterState_ThreadHead(main_interp) == NULL));
if (reuse) {
// The initial thread state has already been "destroyed",
// so this will re-use the statically allocated tstate
// (along with reinitializing it).
tstate = PyThreadState_New(main_interp);
assert(tstate != NULL);
assert(tstate == main_tstate);
}
assert(_PyThreadState_GET() == NULL);

PyThreadState *tstate = PyThreadState_New(main_interp);
// The runtime automatically re-uses the initial main tstate.
// (It is statically allocated as part of the global runtime state.)
assert(_PyThreadState_GET() != main_tstate);
(void)PyThreadState_Swap(tstate);

if (hascode) {
Expand All @@ -260,15 +280,10 @@ static int test_replace_main_tstate(void)
}
}

assert(PyThreadState_Get() != main_tstate);
assert(PyThreadState_Get() == tstate);
Py_Finalize();

return err;
#else
// We want to always force a new "main" tstate, which requires Py_DEBUG.
fprintf(stderr, "ERROR: Py_DEBUG is required\n");
return 1;
#endif
}


Expand Down
13 changes: 2 additions & 11 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,6 @@ init_interpreter(PyInterpreterState *interp,
assert(next != NULL || (interp == runtime->interpreters.main));
interp->next = next;

#ifdef Py_DEBUG
interp->threads.reuse_init_tstate = 1;
#endif

PyStatus status = _PyObject_InitState(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down Expand Up @@ -1559,12 +1555,7 @@ new_threadstate(PyInterpreterState *interp, int whence)

// Allocate the thread state and add it to the interpreter.
PyThreadState *old_head = interp->threads.head;
if (old_head == NULL
#ifdef Py_DEBUG
&& interp->threads.reuse_init_tstate
#endif
)
{
if (old_head == NULL) {
// It's the interpreter's initial thread state.
used_newtstate = 0;
tstate = &interp->_initial_thread;
Expand All @@ -1573,7 +1564,7 @@ new_threadstate(PyInterpreterState *interp, int whence)
else {
// Every valid interpreter must have at least one thread.
assert(id > 1);
assert(old_head == NULL || old_head->prev == NULL);
assert(old_head->prev == NULL);
used_newtstate = 1;
tstate = new_tstate;
// Set to _PyThreadState_INIT.
Expand Down
Loading
0