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 initi 8000 al "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
Add tests to check different "bad" runtime fini situations.
  • Loading branch information
ericsnowcurrently committed Jun 20, 2024
commit 608139b0eb211d9590529c265e65b98c04cde32b
13 changes: 13 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ def test_replace_main_tstate(self):
)
self.assertEqual(out.strip(), 'spam!')

def test_fini_in_subthread(self):
self.run_embedded_interpreter('test_fini_in_subthread')

def test_fini_in_main_thread_with_other_tstate(self):
self.run_embedded_interpreter(
'test_fini_in_main_thread_with_other_tstate',
)

def test_fini_in_main_thread_with_subinterpreter(self):
self.run_embedded_interpreter(
'test_fini_in_main_thread_with_subinterpreter',
)

def test_forced_io_encoding(self):
# Checks forced configuration of embedded interpreter IO streams
env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
Expand Down
86 changes: 86 additions & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,87 @@ static int test_replace_main_tstate(void)
}


/****************************************************************************
* Test (mostly) unsupported Py_Finalize() scenarios
***************************************************************************/

struct fini_subthread_args {
PyThreadState *main_tstate;
PyInterpreterState *interp;
PyMutex done;
};

static void fini_with_new_tstate(void *arg)
{
struct fini_subthread_args *args = (struct fini_subthread_args *)arg;

assert(!_Py_IsMainThread());
assert(_PyThreadState_GET() == NULL);

PyThreadState *tstate = PyThreadState_New(args->interp);
assert(tstate != NULL);
assert(tstate != args->main_tstate);
(void)PyThreadState_Swap(tstate);

assert(PyThreadState_Get() != args->main_tstate);
Py_Finalize();

PyMutex_Unlock(&args->done);
}

static int test_fini_in_subthread(void)
{
_testembed_Py_InitializeFromConfig();
PyThreadState *main_tstate = PyThreadState_Get();

struct fini_subthread_args args = {
.main_tstate = main_tstate,
.interp = main_tstate->interp,
};
PyMutex_Lock(&args.done);
(void)PyThread_start_new_thread(fini_with_new_tstate, &args);

// Wait for fini to finish.
PyMutex_Lock(&args.done);
PyMutex_Unlock(&args.done);

return 0;
}

static int test_fini_in_main_thread_with_other_tstate(void)
{
_testembed_Py_InitializeFromConfig();
PyThreadState *main_tstate = PyThreadState_Get();

PyThreadState *tstate = PyThreadState_New(main_tstate->interp);
(void)PyThreadState_Swap(tstate);

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

return 0;
}

static int test_fini_in_main_thread_with_subinterpreter(void)
{
_testembed_Py_InitializeFromConfig();
PyThreadState *main_tstate = PyThreadState_Get();

PyThreadState *substate = Py_NewInterpreter();
assert(substate != main_tstate);
#ifndef NDEBUG
(void)main_tstate;
(void)substate;
#endif

// The subinterpreter's tstate is still current.
assert(PyThreadState_Get() == substate);
Py_Finalize();

return 0;
}


/*****************************************************
* Test forcing a particular IO encoding
*****************************************************/
Expand Down Expand Up @@ -2227,6 +2308,11 @@ static struct TestCase TestCases[] = {
{"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
{"test_repeated_init_and_inittab", test_repeated_init_and_inittab},
{"test_replace_main_tstate", test_replace_main_tstate},
{"test_fini_in_subthread", test_fini_in_subthread},
{"test_fini_in_main_thread_with_other_tstate",
test_fini_in_main_thread_with_other_tstate},
{"test_fini_in_main_thread_with_subinterpreter",
test_fini_in_main_thread_with_subinterpreter},
{"test_pre_initialization_api", test_pre_initialization_api},
{"test_pre_initialization_sys_options", test_pre_initialization_sys_options},
{"test_bpo20891", test_bpo20891},
Expand Down
0