◐ Shell
reader mode source ↗
Skip to content

gh-80406: Finalise subinterpreters in Py_FinalizeEx()#17575

Closed
LewisGaul wants to merge 21 commits into
python:mainfrom
LewisGaul:finalise-subinterps
Closed

gh-80406: Finalise subinterpreters in Py_FinalizeEx()#17575
LewisGaul wants to merge 21 commits into
python:mainfrom
LewisGaul:finalise-subinterps

Conversation

@LewisGaul

@LewisGaul LewisGaul commented Dec 11, 2019

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.

@LewisGaul

Copy link
Copy Markdown
Contributor Author

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

@LewisGaul

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.

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

@ericsnowcurrently ericsnowcurrently left a comment

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.

Hide comment

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

@bedevere-bot

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.

@csabella

Copy link
Copy Markdown
Contributor

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

@LewisGaul

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

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

@ncoghlan

ncoghlan commented Jan 25, 2020 via email

Copy link
Copy Markdown
Contributor

@ericsnowcurrently

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

@LewisGaul

LewisGaul commented Oct 20, 2020

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?

42 hidden items Load more…

@ericsnowcurrently ericsnowcurrently left a comment

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.

Hide comment

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

ericsnowcurrently commented Oct 22, 2020

Copy link
Copy Markdown
Member

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.

@vstinner

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.

@LewisGaul

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.

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

CLA signed

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

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

Copy link
Copy Markdown
Member

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

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.