gh-80406: Finalise subinterpreters in Py_FinalizeEx() by LewisGaul · Pull Request #17575 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
23af5f5
Add test suggested by ncoghlan
LewisGaul Nov 21, 2019
433663c
Finalise sub-interpreters in Py_FinalizeEx()
LewisGaul Dec 11, 2019
48e1cfc
Improve test name
LewisGaul Dec 13, 2019
0400634
Switch back to main threadstate in test_audit_subinterpreter before c…
LewisGaul Dec 13, 2019
b79649c
📜🤖 Added by blurb_it.
blurb-it[bot] Dec 14, 2019
8b1e7d9
Markups including: switch from 'finalizing' flag to 'allow_new', add …
LewisGaul Jan 21, 2020
fd6073a
Merge branch 'finalise-subinterps' of github.com:LewisGaul/cpython in…
LewisGaul Jan 21, 2020
4bbd58f
Merge branch 'master' into finalise-subinterps
LewisGaul Oct 20, 2020
1095e66
Use '_' for unused variable in test_embed.py
LewisGaul Oct 20, 2020
675285d
Fix struct position of 'allow_new' flag
LewisGaul Oct 22, 2020
8e21788
Add handling for unsupported case of calling Py_Finalize() from a sub…
LewisGaul Oct 22, 2020
606c068
Emit resource warning when calling Py_Finalize() with unfinalized sub…
LewisGaul Oct 22, 2020
e0789b0
Update Py_FinalizeEx() docs
LewisGaul Oct 22, 2020
dda99ce
Update test for resource warning when implicitly finalizing subinterp…
LewisGaul Oct 23, 2020
847e8d2
Tidy up test_finalize_subinterps() testcase
LewisGaul Oct 23, 2020
a2fb0fc
Add testcase for calling Py_Finalize() from a subinterpreter
LewisGaul Oct 23, 2020
d234528
Tweak subinterpreters still running ResourceWarning handling
LewisGaul Nov 23, 2020
46a8619
Make calling PyFinalizeEx() from a subinterpreter a Py_FatalError
LewisGaul Nov 23, 2020
c89c0e5
Acquire interpreters mutex before setting allow_new=0 in PyFinalizeEx()
LewisGaul Nov 23, 2020
c285f52
Merge remote-tracking branch 'upstream/master' into finalise-subinterps
LewisGaul Nov 23, 2020
95cbfd4
Add back in the 'interp' variable to PyFinalizeEx() to fix the build
LewisGaul Nov 23, 2020
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
6 changes: 4 additions & 2 deletions Doc/c-api/init.rst
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ typedef struct pyruntimestate {
If that becomes a problem later then we can adjust, e.g. by
using a Python int. */
int64_t next_id;
int allow_new;
Comment thread
LewisGaul marked this conversation as resolved.
} interpreters;
// XXX Remove this field once we have a tp_* slot.
struct _xidregistry {
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ def test_subinterps_distinct_state(self):
self.assertNotEqual(sub.tstate, main.tstate)
self.assertNotEqual(sub.modules, main.modules)

def test_finalize_subinterps(self):
"""
bpo-36225: Subinterpreters should implicitly be torn down by
Py_Finalize().
"""
_, err = self.run_embedded_interpreter("test_finalize_subinterps")
if support.verbose > 1:
print()
print(err)
self.assertIn("ResourceWarning: extra 2 interpreters", err)

def test_finalize_from_subinterp(self):
"""
bpo-38865: Py_Finalize() should not be called from a subinterpreter.
"""
_, err = self.run_embedded_interpreter("test_finalize_from_subinterp",
returncode=-6)
self.assertIn(
"Fatal Python error: Py_FinalizeEx: must be called from the main interpreter",
err)

def test_forced_io_encoding(self):
# Checks forced configuration of embedded interpreter IO streams
env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`Py_FinalizeEx()` now implicitly cleans up subinterpreters, as the C API documentation suggests.
62 changes: 61 additions & 1 deletion Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/*********************************************************
* Embedded interpreter tests that need a custom exe
*
* Executed via 'EmbeddingTests' in Lib/test/test_capi.py
* Executed via 'EmbeddingTests' in Lib/test/test_embed.py
*********************************************************/

/* Use path starting with "./" avoids a search along the PATH */
Expand Down Expand Up @@ -83,6 +83,60 @@ static int test_repeated_init_and_subinterpreters(void)
return 0;
}

/* bpo-36225: Implicitly tear down subinterpreters with Py_Finalize() */
static int test_finalize_subinterps(void)
{
PyThreadState *mainstate;
PyThreadState *interp_tstate;
PyGILState_STATE gilstate;
int i;

_testembed_Py_Initialize();
mainstate = PyThreadState_Get();

PyEval_ReleaseThread(mainstate);

gilstate = PyGILState_Ensure();
print_subinterp();
PyThreadState_Swap(NULL);

// Create 3 subinterpreters and destroy the last one.
for (i=0; i<3; i++) {
interp_tstate = Py_NewInterpreter();
print_subinterp();
}
PyThreadState_Swap(interp_tstate);
Py_EndInterpreter(interp_tstate);

// Switch back to the main interpreter and finalize the runtime.
PyThreadState_Swap(mainstate);
print_subinterp();
PyGILState_Release(gilstate);

PyEval_RestoreThread(mainstate);
Py_Finalize();

return 0;
}

/* bpo-38865: Py_Finalize() should not be called from a subinterpreter */
static int test_finalize_from_subinterp(void)
{
PyThreadState *subinterp_tstate;
int rc;

_testembed_Py_Initialize();
PyGILState_Ensure();
PyThreadState_Swap(NULL);

subinterp_tstate = Py_NewInterpreter();
PyThreadState_Swap(subinterp_tstate);

rc = Py_FinalizeEx();

return rc;
}

/*****************************************************
* Test forcing a particular IO encoding
*****************************************************/
Expand Down Expand Up @@ -1195,10 +1249,14 @@ static int test_audit_subinterpreter(void)
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
_testembed_Py_Initialize();

PyThreadState *mainstate = PyThreadState_Get();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to double-check with @zooba on his intention here. It's pretty important to make sure that the auditing functionality works as expected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @zooba, as a consequence of my changes here, test_audit_subinterpreter() in _testembed.c started failing.

The change I'm making is to make Py_Finalize() implicitly clean up subinterpreters.

In the test, multiple subinterpreters are created, and then Py_Finalize() is called from the last-created subinterpreter. It seems there's currently an issue with calling Py_Finalize() from a subinterpreter (see bpo-37776), which caused this test to fail when getting Py_Finalize() to clean up subinterpreters.

The test passes if Py_Finalize() is instead called from the main interpreter tstate - which is the change I've made to the test. Just wanting to check whether that's taking anything away from what's intentionally being checked by this test?

@LewisGaul LewisGaul Oct 20, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #19063 from @vstinner which was not merged, but also proposed to change the logic of this testcase. It seems like this testcase is doing something that is not currently working, and according to bpo-38865#msg357331 may not be supported in general?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also confirmed that this test still fails on my branch without this change.


Py_NewInterpreter();
Py_NewInterpreter();
Py_NewInterpreter();

// Currently unable to call Py_Finalize from subinterpreter thread, see bpo-37776.
PyThreadState_Swap(mainstate);
Py_Finalize();

switch (_audit_subinterpreter_interpreter_count) {
Expand Down Expand Up @@ -1707,6 +1765,8 @@ struct TestCase
static struct TestCase TestCases[] = {
{"test_forced_io_encoding", test_forced_io_encoding},
{"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
{"test_finalize_subinterps", test_finalize_subinterps},
Comment thread
LewisGaul marked this conversation as resolved.
{"test_finalize_from_subinterp", test_finalize_from_subinterp},
{"test_pre_initialization_api", test_pre_initialization_api},
{"test_pre_initialization_sys_options", test_pre_initialization_sys_options},
{"test_bpo20891", test_bpo20891},
Expand Down
51 changes: 46 additions & 5 deletions Python/pylifecycle.c