-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-80406: Finalise subinterpreters in Py_FinalizeEx() #17575
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
23af5f5
433663c
48e1cfc
0400634
b79649c
8b1e7d9
fd6073a
4bbd58f
1095e66
675285d
8e21788
606c068
e0789b0
dda99ce
847e8d2
a2fb0fc
d234528
46a8619
c89c0e5
c285f52
95cbfd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 */ | ||
|
|
@@ -83,6 +83,39 @@ 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; | ||
| PyGILState_STATE gilstate; | ||
| int i, j; | ||
|
|
||
| for (i=0; i<15; i++) { | ||
| printf("--- Pass %d ---\n", i); | ||
| _testembed_Py_Initialize(); | ||
| mainstate = PyThreadState_Get(); | ||
|
|
||
| PyEval_ReleaseThread(mainstate); | ||
|
|
||
| gilstate = PyGILState_Ensure(); | ||
| print_subinterp(); | ||
| PyThreadState_Swap(NULL); | ||
|
|
||
| for (j=0; j<2; j++) { | ||
| Py_NewInterpreter(); | ||
| print_subinterp(); | ||
| } | ||
|
|
||
| PyThreadState_Swap(mainstate); | ||
| print_subinterp(); | ||
| PyGILState_Release(gilstate); | ||
|
|
||
| PyEval_RestoreThread(mainstate); | ||
| Py_Finalize(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This certainly helps verify that finalization still works. There should probably also be something verifying that the subinterpreters were properly cleaned up at the beginning of finalization. (...perhaps with some artifact generated when each sub-interp is finalized.) Also, what about the case where:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps registering an "atexit" handler in each subinterpreter that prints something, and then confirming in the Python test case code that all the subinterpreter exit messages appear before the main interpreter's exit message?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This all sounds worth checking in a test, but I'm unclear how to implement it. Any advice would be appreciated, specifically:
With a better understanding of the above I may be able to have a go at covering the above points in the tests, although it'll likely take quite a lot of thought given I'm pretty new to the C API! Any further guidance very welcome, and I can hopefully get this finished off without such a delay this time.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Put as much logic as you can in the Python code. _testembed.c should mostly be only what can't be done from Python (with exceptions where practicality dictates more).
You can call Python code from C if needed. Import the atexit module, get the appropriate function, and call it, all using the C-API. We do the same thing in various places, like Python/import.c. For me (not a C expert) searching the code base has always been the easiest way to see how to do something. 😄 (That assumes there isn't a C-API for atexit handlers.)
I'll need more context.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a good reason to pair up on a video call. Then we could walk through this stuff a bit more efficiently. What do you think?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least some of the embedding tests already use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like there's a lot of considerations and things to check here, also fleshed out by Victor's message at bpo-36225#msg371571. Does everything here need addressing in this one PR, or can some of these points be split into separate issues to follow this fix? This feels like rather a lot to tackle all in one go - which of the cases you listed would you suggest starting with @ericsnowcurrently (perhaps the simplest to test!)? |
||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| /***************************************************** | ||
| * Test forcing a particular IO encoding | ||
| *****************************************************/ | ||
|
|
@@ -1195,10 +1228,14 @@ static int test_audit_subinterpreter(void) | |
| PySys_AddAuditHook(_audit_subinterpreter_hook, NULL); | ||
| _testembed_Py_Initialize(); | ||
|
|
||
| PyThreadState *mainstate = PyThreadState_Get(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @zooba, as a consequence of my changes here, The change I'm making is to make In the test, multiple subinterpreters are created, and then The test passes if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
|
@@ -1658,6 +1695,7 @@ 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}, | ||
|
LewisGaul marked this conversation as resolved.
|
||
| {"test_pre_initialization_api", test_pre_initialization_api}, | ||
| {"test_pre_initialization_sys_options", test_pre_initialization_sys_options}, | ||
| {"test_bpo20891", test_bpo20891}, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,7 @@ _PyRuntime_Initialize(void) | |
| return _PyStatus_OK(); | ||
| } | ||
| runtime_initialized = 1; | ||
| _PyRuntime.interpreters.allow_new = 0; | ||
|
|
||
| return _PyRuntimeState_Init(&_PyRuntime); | ||
| } | ||
|
|
@@ -986,6 +987,7 @@ init_interp_main(PyThreadState *tstate) | |
| */ | ||
| if (is_main_interp) { | ||
| interp->runtime->initialized = 1; | ||
| interp->runtime->interpreters.allow_new = 1; | ||
| } | ||
| return _PyStatus_OK(); | ||
| } | ||
|
|
@@ -1060,6 +1062,7 @@ init_interp_main(PyThreadState *tstate) | |
| } | ||
|
|
||
| interp->runtime->initialized = 1; | ||
| interp->runtime->interpreters.allow_new = 1; | ||
| } | ||
|
|
||
| if (config->site_import) { | ||
|
|
@@ -1348,6 +1351,20 @@ Py_FinalizeEx(void) | |
| PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); | ||
| PyInterpreterState *interp = tstate->interp; | ||
|
|
||
| // Finalize sub-interpreters. | ||
| runtime->interpreters.allow_new = 0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be safer to acquire _PyRuntime.interpreters.mutex beforing setting this variable. It may be better to move this code into pystate.c, since this file control the list of interpreters.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I presume you're suggesting refactoring the 'finalize subinterpreters' logic? It would seem to me a function that does 'finalizing' belongs better in pylifecycle than pystate? I believe this refactoring was referred to by Eric above, where he says this can be addressed separately. I've added in an acquisition of the lock here for now. |
||
| PyInterpreterState *curr_interp = PyInterpreterState_Head(); | ||
| PyInterpreterState *next_interp; | ||
| while (curr_interp != NULL) { | ||
| next_interp = PyInterpreterState_Next(curr_interp); | ||
| if (curr_interp != PyInterpreterState_Main()) { | ||
| PyThreadState_Swap(curr_interp->tstate_head); | ||
| Py_EndInterpreter(curr_interp->tstate_head); | ||
| } | ||
| curr_interp = next_interp; | ||
| } | ||
| PyThreadState_Swap(tstate); | ||
|
LewisGaul marked this conversation as resolved.
|
||
|
|
||
| // Wrap up existing "threading"-module-created, non-daemon threads. | ||
| wait_for_thread_shutdown(tstate); | ||
|
|
||
|
|
@@ -1534,8 +1551,10 @@ new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter) | |
| } | ||
| _PyRuntimeState *runtime = &_PyRuntime; | ||
|
|
||
| if (!runtime->initialized) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd not remove this condition
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for any confusion, @soltysh. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ericsnowcurrently so runtime is always a single object (like an uber-object) and then you can create multiple interpreters. Right, but that still requires the runtime to be initialized. Even though the situation should not happen, because I'd assume the first invocation of python would initialize the runtime, it should not hurt having this here. Unless my thinking is wrong here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose your suggestion @soltysh would be to have two separate checks on It's a while ago now, but I think my reasoning here was that this 'allow_new' flag encapsulates all information about whether a new interpreter can be created, so there should be no need to check things like whether the runtime is initialised (since 'allow_new' is only set to true when the runtime is initialised). Does that sounds reasonable? I could be persuaded to change this if there are better suggestions :) |
||
| return _PyStatus_ERR("Py_Initialize must be called first"); | ||
| if (!runtime->interpreters.allow_new) { | ||
| return _PyStatus_ERR( | ||
| "New interpreters cannot currently be created - Py_Initialize must " | ||
| "be called first, and Py_Finalize must not have been called"); | ||
| } | ||
|
|
||
| /* Issue #10915, #15751: The GIL API doesn't work with multiple | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.