GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-80406: Finalise subinterpreters in Py_FinalizeEx() by LewisGaul · Pull Request #17575 · python/cpython

Conversation

@LewisGaul

@LewisGaul LewisGaul commented Dec 11, 2019 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • 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.

  • Issue: [subinterpreters] Lingering subinterpreters should be implicitly cleared on shutdown #80406

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
LewisGaul added 2 commits November 21, 2019 18:25
@LewisGaul
Add test suggested by ncoghlan
23af5f5
@LewisGaul
Finalise sub-interpreters in Py_FinalizeEx()
433663c
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Dec 11, 2019
@bedevere-bot bedevere-bot added the awaiting review label Dec 11, 2019
@LewisGaul

LewisGaul commented Dec 11, 2019

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

LewisGaul added 2 commits December 13, 2019 18:32
@LewisGaul
Improve test name
48e1cfc
@LewisGaul
Switch back to main threadstate in test_audit_subinterpreter before c…
0400634
…alling Py_Finalize()
@LewisGaul

LewisGaul commented Dec 13, 2019

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently
ericsnowcurrently self-requested a review December 14, 2019 00:08
@blurb-it
📜🤖 Added by blurb_it.
b79649c
ericsnowcurrently
ericsnowcurrently requested changes Dec 20, 2019
View reviewed changes

@ericsnowcurrently ericsnowcurrently left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/pylifecycle.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/pylifecycle.c Outdated
next_interp = PyInterpreterState_Next(subinterp);
if (subinterp != PyInterpreterState_Main()) {
PyThreadState_Swap(subinterp->tstate_head);
Py_EndInterpreter(subinterp->tstate_head);

@ericsnowcurrently ericsnowcurrently Dec 20, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LewisGaul LewisGaul Jan 21, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ericsnowcurrently ericsnowcurrently Jan 24, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@kulikjak kulikjak Jun 5, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LewisGaul LewisGaul Oct 20, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/pylifecycle.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Include/internal/pycore_pystate.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/pylifecycle.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/pylifecycle.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Programs/_testembed.c
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
_testembed_Py_Initialize();

PyThreadState *mainstate = PyThreadState_Get();

@ericsnowcurrently ericsnowcurrently Dec 20, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LewisGaul LewisGaul Jan 21, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LewisGaul LewisGaul Oct 20, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LewisGaul LewisGaul Oct 20, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Programs/_testembed.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Programs/_testembed.c Outdated
PyGILState_Release(gilstate);

PyEval_RestoreThread(mainstate);
Py_Finalize();

@ericsnowcurrently ericsnowcurrently Dec 20, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ncoghlan ncoghlan Dec 30, 2019

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LewisGaul LewisGaul Jan 21, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ericsnowcurrently ericsnowcurrently Jan 24, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ericsnowcurrently ericsnowcurrently Jan 24, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ncoghlan ncoghlan Jan 25, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ericsnowcurrently reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@LewisGaul LewisGaul Oct 20, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Programs/_testembed.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the awaiting review label Dec 20, 2019
@bedevere-bot

bedevere-bot commented Dec 20, 2019

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added the awaiting changes label Dec 20, 2019
ncoghlan
ncoghlan reviewed Dec 30, 2019
View reviewed changes
Comment thread Include/internal/pycore_pystate.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

@csabella

csabella commented Jan 12, 2020

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@LewisGaul

LewisGaul commented Jan 12, 2020

Copy link
Copy Markdown
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 and ericsnowcurrently reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@csabella

csabella commented Jan 12, 2020

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

LewisGaul added 2 commits January 21, 2020 23:35
@LewisGaul
Markups including: switch from 'finalizing' flag to 'allow_new', add …
8b1e7d9
…test to test_embed.py
@LewisGaul
Merge branch 'finalise-subinterps' of github.com:LewisGaul/cpython in…
fd6073a
…to finalise-subinterps
ericsnowcurrently
ericsnowcurrently requested changes Jan 24, 2020
View reviewed changes
Comment thread Lib/test/test_embed.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ncoghlan

ncoghlan commented Jan 25, 2020 via email

Copy link
Copy Markdown
Contributor
On Sat., 25 Jan. 2020, 7:45 am Eric Snow, ***@***.***> wrote: ***@***.**** commented on this pull request. ------------------------------ In Include/internal/pycore_pystate.h <#17575 (comment)>: > @@ -226,6 +226,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 finalizing; I guess it's a bit vague. "new" what? Honestly, the flag represents more than just the availability of subinterpreters. So I'd probably feel more comfortable with "ready" (or "available"), which would mean "the full capability of the C-API is available" (including subinterpreters). @ncoghlan <https://github.com/ncoghlan>, what do you think?
My expectation with the naming suggestion is that we use the flag to mean "`Py_NewInterpreter` will now work". It's true it could be seen as ambiguous with the more general meaning "allow new objects" (based on the name of the tp_new slot and associated Python magic method), so I'd also be OK with expanding it to be the more explicit "allow_new_interpreters". I don't think we'd be helping anyone by making the flag itself ambiguous, though, so I think we should stick with the concrete meaning of "`Py_NewInterpreter` will always fail immediately when this is not set".
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Jan 27, 2020

Copy link
Copy Markdown
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). 😄

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

soltysh
soltysh reviewed Feb 7, 2020
View reviewed changes
Comment thread Python/pylifecycle.c
}
_PyRuntimeState *runtime = &_PyRuntime;

if (!runtime->initialized) {

@soltysh soltysh Feb 7, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ericsnowcurrently ericsnowcurrently Feb 7, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@soltysh soltysh Feb 7, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LewisGaul LewisGaul Oct 20, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@soltysh soltysh mentioned this pull request Feb 7, 2020
Lingering subinterpreters should be implicitly cleared on shutdown ericsnowcurrently/multi-core-python#57
Open
LewisGaul and others added 2 commits October 20, 2020 14:50
@LewisGaul
Merge branch 'master' into finalise-subinterps
4bbd58f
@LewisGaul @ericsnowcurrently
Use '_' for unused variable in test_embed.py
1095e66
Co-authored-by: Eric Snow <[email protected]>
@LewisGaul

LewisGaul commented Oct 20, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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?
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ericsnowcurrently
ericsnowcurrently reviewed Oct 22, 2020
View reviewed changes
Comment thread Include/internal/pycore_runtime.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

@LewisGaul
Fix struct position of 'allow_new' flag
675285d
ericsnowcurrently
ericsnowcurrently requested changes Oct 22, 2020
View reviewed changes

@ericsnowcurrently ericsnowcurrently left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Include/internal/pycore_runtime.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/pylifecycle.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Oct 22, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • 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.

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

LewisGaul added 6 commits October 22, 2020 23:19
@LewisGaul
Add handling for unsupported case of calling Py_Finalize() from a sub…
8e21788
…interpreter
@LewisGaul
Emit resource warning when calling Py_Finalize() with unfinalized sub…
606c068
…interpreters
@LewisGaul
Update Py_FinalizeEx() docs
e0789b0
@LewisGaul
Update test for resource warning when implicitly finalizing subinterp…
dda99ce
…reters
@LewisGaul
Tidy up test_finalize_subinterps() testcase
847e8d2
@LewisGaul
Add testcase for calling Py_Finalize() from a subinterpreter
a2fb0fc
vstinner
vstinner reviewed Oct 27, 2020
View reviewed changes
Comment thread Python/pylifecycle.c
}

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

@vstinner vstinner Oct 27, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LewisGaul LewisGaul Nov 23, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/pylifecycle.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/pylifecycle.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/pylifecycle.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Oct 27, 2020

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@LewisGaul

LewisGaul commented Nov 23, 2020

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

LewisGaul added 5 commits November 23, 2020 20:15
@LewisGaul
Tweak subinterpreters still running ResourceWarning handling
d234528
@LewisGaul
Make calling PyFinalizeEx() from a subinterpreter a Py_FatalError
46a8619
@LewisGaul
Acquire interpreters mutex before setting allow_new=0 in PyFinalizeEx()
c89c0e5
@LewisGaul
Merge remote-tracking branch 'upstream/master' into finalise-subinterps
c285f52
@LewisGaul
Add back in the 'interp' variable to PyFinalizeEx() to fix the build
95cbfd4
LewisGaul
LewisGaul commented Nov 23, 2020
View reviewed changes
Comment thread Python/pylifecycle.c
* before finalizing the runtime.
*/
if (PyErr_ResourceWarning(NULL, 1,
"extra %zd interpreters", num_destroyed)) {

@LewisGaul LewisGaul Nov 23, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner vstinner mentioned this pull request Apr 10, 2022
[subinterpreters] Can Py_Finalize() be called if the current interpreter is not the main interpreter? #83046
Closed
@ncoghlan ncoghlan mentioned this pull request Apr 10, 2022
[subinterpreters] Lingering subinterpreters should be implicitly cleared on shutdown #80406
Closed
@ezio-melotti ezio-melotti removed the CLA signed label Jul 13, 2022
@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

python-cla-bot Bot commented Apr 6, 2025

Copy link
Copy Markdown

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

  • [email protected]

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Apr 7, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 7, 2026
@StanFromIreland

StanFromIreland commented May 3, 2026

Copy link
Copy Markdown
Member

Thanks for the interesting in contributing, however I'm closing as this is stale, awaiting changes, and CLA unsigned.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland StanFromIreland closed this May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner left review comments
@ncoghlan ncoghlan ncoghlan left review comments
@ericsnowcurrently ericsnowcurrently ericsnowcurrently requested changes
+2 more reviewers
@soltysh soltysh soltysh left review comments
@kulikjak kulikjak kulikjak left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting changes stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

11 participants

@LewisGaul @bedevere-bot @csabella @ncoghlan @ericsnowcurrently @vstinner @StanFromIreland @soltysh @kulikjak @the-knights-who-say-ni @ezio-melotti
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.