Skip to content

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

LewisGaul
Copy link
Contributor

@LewisGaul LewisGaul commented Dec 11, 2019

  • Added testcases in Programs/_testembed.c - previously failing, now passing
  • Loop over subinterpreters in Py_FinalizeEx() and call Py_EndInterpreter()
  • New flag _PyRuntime.interpreters.allow_new to prevent new subinterpreters from being created during finalisation
  • Error if calling Py_FinalizeEx() from a subinterpreter (to be changed at a later stage)

https://bugs.python.org/issue36225

Also addresses bpo-38865 and bpo-37776.

@LewisGaul
Copy link
Contributor Author

I need to look into test_audit_subinterpreter() testcase, will do so at some point soon.

@LewisGaul
Copy link
Contributor Author

As noted here, it seems test_audit_subinterpreter() was failing because it was trying to call Py_Finalize() from a subinterpreter. Hopefully now fixed by switching back to the main threadstate in that testcase.

@ericsnowcurrently ericsnowcurrently self-requested a review December 14, 2019 00:08
Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! The approach makes sense, including the flag (and where you put it). My comments are basically:

  • questions about a few details
  • point out missing test code
  • a recommendation about the name (and meaning) of the new flag

next_interp = PyInterpreterState_Next(subinterp);
if (subinterp != PyInterpreterState_Main()) {
PyThreadState_Swap(subinterp->tstate_head);
Py_EndInterpreter(subinterp->tstate_head);
Copy link
Member

Choose a reason for hiding this comment

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

This fails if the interp.tstate_head is still running (has a frame). We may want to consider a more graceful approach to dealing with subinterpreters that are still doing work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm yes, do you have any further thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

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

I'll have to give it more thought (and take the time to queue up details into my mental cache 😄).

Copy link
Contributor

Choose a reason for hiding this comment

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

We've recently hit this problem with lingering subinterpreter with existing frames.

I solved it by adding Py_CLEAR before the Py_EndInterpreter call (which is similar to how PyThreadState_Clear handles existing frames) and now it seems to work and end gracefully. While that might not be the best solution (and I don't see in subinterpreters much to see possible issues with that), I guess it's not worse than Fatal error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ericsnowcurrently any new thoughts here? We might be better off having a call to chat through everything :)

@@ -1181,10 +1222,14 @@ static int test_audit_subinterpreter(void)
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
_testembed_Py_Initialize();

PyThreadState *mainstate = PyThreadState_Get();
Copy link
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
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?

Copy link
Contributor Author

@LewisGaul LewisGaul Oct 20, 2020

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
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.

PyGILState_Release(gilstate);

PyEval_RestoreThread(mainstate);
Py_Finalize();
Copy link
Member

Choose a reason for hiding this comment

The 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:

  • the subinterpreter has multiple threads still running?
  • what about daemon threads? (yeah, it's mean of me to ask 😉)
  • the subinterpreter's tstate_head is still running?
  • someone calls Py_NewInterpreter() while interpreters are being cleaned up?
  • someone calls Py_NewInterpreter() while finalization is otherwise still running?
  • someone calls Py_NewInterpreter() after finalization is finished?

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

  • Should all of the test logic be in _testembed.c, or should that just be performing the C API calls with most of the test logic being in test_embed.py?
    • I'm only aware of how to register an atexit handler from Python code.
    • How should the interaction between test_embed.py and _testembed.c work?

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.

Copy link
Member

Choose a reason for hiding this comment

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

  • Should all of the test logic be in _testembed.c, or should that just be performing the C API calls with most of the test logic being in test_embed.py?

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).

  • I'm only aware of how to register an atexit handler from Python code.

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.)

  • How should the interaction between test_embed.py and _testembed.c work?

I'll need more context.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

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

At least some of the embedding tests already use PyRun_SimpleString() to run Python code inside the created interpreter, and that's also what I had in mind for the suggested atexit test case above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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!)?

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@csabella
Copy link
Contributor

@LewisGaul, please take a look at the code reviews and requested changes. Thank you!

@LewisGaul
Copy link
Contributor Author

@csabella Yes I haven't forgotten this, I've been a bit busy. I have plans to work on this, hopefully soon.

@csabella
Copy link
Contributor

@LewisGaul, thank you for the update. I just wanted to make sure you were aware that it had been reviewed. 🙂

@ncoghlan
Copy link
Contributor

ncoghlan commented Jan 25, 2020 via email

@ericsnowcurrently
Copy link
Member

I don't think we'd be helping anyone by making the flag itself ambiguous,

Fair enough. I guess I'm thinking about future stuff, but we can address that later (when needed). 😄

@@ -1450,8 +1467,10 @@ new_interpreter(PyThreadState **tstate_p)
}
_PyRuntimeState *runtime = &_PyRuntime;

if (!runtime->initialized) {
Copy link

Choose a reason for hiding this comment

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

I'd not remove this condition initialized is separate from allow_new, so having this check here still makes sense.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for any confusion, @soltysh. _PyRuntimeState,interpreters.allow_new was added in this PR to solve the problem of interpreters being created when they shouldn't be (e.g. during runtime finalization). You could say the flag specifically means "new_interpreter() can be called currently". 😄 So the change here is correct.

Copy link

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 runtime->initialized and runtime->interpreters.allow_new?

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 :)

@LewisGaul
Copy link
Contributor Author

LewisGaul commented Oct 20, 2020

I've resolved conflicts with upstream.

Current status:

  • Question from @vstinner over whether this behaviour should be changed [while there's still special treatment of the 'main' interpreter], see https://bugs.python.org/issue38865#msg364573.
  • Many additional cases need testing:
    • the subinterpreter has multiple threads still running?
    • what about daemon threads?
    • the subinterpreter's tstate_head is still running?
    • someone calls Py_NewInterpreter() while interpreters are being cleaned up?
    • someone calls Py_NewInterpreter() while finalization is otherwise still running?
    • someone calls Py_NewInterpreter() after finalization is finished?

Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

Choose a reason for hiding this comment

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

Thanks for sticking with this! You're on the right track. I've noted 2 things that should be adjusted. I'll address the test cases separately.

We also discussed some refactoring that would help establish as safer order of operations during runtime finalization (and likewise initialization). However, those can be handled separately.

@ericsnowcurrently
Copy link
Member

ericsnowcurrently commented Oct 22, 2020

I don't see Victor's concerns as a problem for this PR. The impact is low and the result is an error before anything happens rather than doing anything improper or unexpected. I'll leave a note in the issue to that effect.

  • Many additional cases need testing:

    • the subinterpreter has multiple threads still running?
    • what about daemon threads?
    • the subinterpreter's tstate_head is still running?
    • someone calls Py_NewInterpreter() while interpreters are being cleaned up?
    • someone calls Py_NewInterpreter() while finalization is otherwise still running?
    • someone calls Py_NewInterpreter() after finalization is finished?

I still think each of those cases should be covered by tests, except maybe the one about daemon threads. I'm not sure how you would write a test that wouldn't fail sporadically with the current behavior. Maybe block a daemon thread and unblock it in an atexit handler (which would be triggered after non-daemon threads would have already finished. Then you can verify that the daemon thread did not block Py_FinalizeEx() from progressing past the wait-for-threads point.

}

// Finalize sub-interpreters.
runtime->interpreters.allow_new = 0;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@LewisGaul LewisGaul Nov 23, 2020

Choose a reason for hiding this comment

The 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.

@vstinner
Copy link
Member

As I wrote in https://bugs.python.org/issue36225 I'm not excited by the idea of finalizing subinterpreters by executing their object finalizers in the main interpreter.

@LewisGaul
Copy link
Contributor Author

As I wrote in https://bugs.python.org/issue36225 I'm not excited by the idea of finalizing subinterpreters by executing their object finalizers in the main interpreter.

Thanks for the input @vstinner. @ericsnowcurrently it would be good to get your thoughts here. I'm happy to change the implementation to satisfy whichever solution we settle on.

* before finalizing the runtime.
*/
if (PyErr_ResourceWarning(NULL, 1,
"extra %zd interpreters", num_destroyed)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure why, but for some reason this warning isn't being output in the tests on Windows only. Anyone have any ideas?

@encukou encukou changed the title bpo-36225: Finalise subinterpreters in Py_FinalizeEx() gh-80406: Finalise subinterpreters in Py_FinalizeEx() Mar 28, 2024
@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

The following commit authors need to sign the Contributor License Agreement:

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants