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 — bpo-36475: Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly by nanjekyejoannah · Pull Request #12667 · python/cpython

Conversation

@nanjekyejoannah

@nanjekyejoannah nanjekyejoannah commented Apr 2, 2019 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

I have added changes to finalize finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly.

https://bugs.python.org/issue36475

Sorry, something went wrong.

Uh oh!

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

All reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Apr 2, 2019
@bedevere-bot bedevere-bot added the awaiting review label Apr 2, 2019
@nanjekyejoannah

nanjekyejoannah commented Apr 2, 2019

Copy link
Copy Markdown
Contributor Author

cc @ericsnowcurrently , @ncoghlan .

All reactions

Sorry, something went wrong.

Uh oh!

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

@ncoghlan
ncoghlan requested a review from ericsnowcurrently April 7, 2019 07:38
ncoghlan
ncoghlan approved these changes Apr 7, 2019
View reviewed changes

@ncoghlan ncoghlan left a comment

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.

LGTM, but I'd like Eric to take a look as well. In particular, I'm not sure if we have any existing test cases for this behaviour, and whether it might make sense to add one to the embedding tests.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Apr 7, 2019
ZackerySpytz
ZackerySpytz reviewed Apr 22, 2019
View reviewed changes
Comment thread Misc/NEWS.d/next/Core and Builtins/2019-04-02-20-02-22.bpo-36475.CjRps3.rst Resolved

Uh oh!

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

@vstinner
vstinner requested review from pablogsal and pitrou April 24, 2019 13:27
@vstinner

vstinner commented Apr 24, 2019

Copy link
Copy Markdown
Member

@pablogsal @pitrou: Would you mind to review this change? It seems like you know the modified functions :-)

All reactions

Sorry, something went wrong.

Uh oh!

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

@pablogsal

pablogsal commented Apr 24, 2019 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

I would recommend adding a note in the doc for these functions as we did in fde9b33 .

Also, this code appears 3 times now in ceval.c so we could refactor this into a macro/inline function.

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed Apr 24, 2019
View reviewed changes
Comment thread Misc/NEWS.d/next/Core and Builtins/2019-04-02-20-02-22.bpo-36475.CjRps3.rst Resolved

Uh oh!

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

@pitrou

pitrou commented Apr 24, 2019

Copy link
Copy Markdown
Member

Hmm... this will exit any thread during finalization, not just daemon threads? I'm not sure why this is a good idea.

All reactions

Sorry, something went wrong.

Uh oh!

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

@nanjekyejoannah

nanjekyejoannah commented Apr 24, 2019

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting merge labels Apr 24, 2019
@bedevere-bot

bedevere-bot commented Apr 24, 2019

Copy link
Copy Markdown

Thanks for making the requested changes!

@ncoghlan: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ncoghlan

ncoghlan commented Apr 25, 2019

Copy link
Copy Markdown
Contributor

@pitrou PyEval_RestoreThread, and hence the Py_END_ALLOW_THREADS macro and the GILState APIs, already cause all threads to exit if they find the interpreter is already finalizing, but the interpreter joins all of its own non-daemon threads before it marks itself as finalizing:

cpython/Python/pylifecycle.c

Line 1150 in 0ef8c15

Py_FinalizeEx(void)

This PR just makes the lower level GIL acquisition APIs behave the same way PyEval_RestoreThread already does so that surviving daemon threads will exit rather than risking deadlocking based on exactly which API they call.

This does highlight that the change is worth mentioning in the porting section of the Python 3.8 What's New, though, along with "versionchanged" notes on the affected APIs.

All reactions

Sorry, something went wrong.

Uh oh!

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

@pitrou

pitrou commented Apr 25, 2019 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

I see. I'm always wary of the potential issues with such low-level changes, but you're right that we're already half way to the behaviour established by the PR, so I guess it's fine.

All reactions

Sorry, something went wrong.

Uh oh!

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

pitrou
pitrou previously requested changes Apr 25, 2019
View reviewed changes

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

I guess it's ok on the principle. Please see comments below.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/ceval.c Resolved

Uh oh!

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

@bedevere-bot

bedevere-bot commented Apr 25, 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 awaiting changes and removed awaiting change review labels Apr 25, 2019
ncoghlan
ncoghlan requested changes Apr 25, 2019
View reviewed changes

@ncoghlan ncoghlan left a comment

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.

The core functionality of the change still looks good to me, just some refactoring suggestions for the code, and a couple more documentation tweaks.

My proposed wording for the NEWS entry can be re-used as a bullet point in the What's New porting section, with an extra sentence on what to do about it:

PyEval_AcquireLock and PyEval_AcquireThread now terminate the current thread if called while the interpreter is finalizing, making them consistent with PyEval_RestoreThread, Py_END_ALLOW_THREADS, and PyGILState_Ensure. If this behaviour is not desired, guard the call by checking _Py_IsFinalizing or sys.is_finalizing.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Doc/c-api/init.rst Resolved

Uh oh!

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

Comment thread Misc/NEWS.d/next/Core and Builtins/2019-04-02-20-02-22.bpo-36475.CjRps3.rst Resolved

Uh oh!

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

Comment thread Python/ceval.c Resolved

Uh oh!

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

Comment thread Python/ceval.c Resolved

Uh oh!

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

Comment thread Doc/c-api/init.rst Outdated

@ncoghlan ncoghlan Apr 25, 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.

@vstinner Are you aware of any particular reason for _Py_IsFinalizing being a nominally private API? If we're recommending that folks call it in the documentation, that's a pretty strong sign that it should really be public.

(That can be broken out to a separate issue, though - it doesn't need to be resolved here)

Sorry, something went wrong.

Uh oh!

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

All reactions

@vstinner vstinner Apr 25, 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.

I don't know. Feel free to open an issue to propose to make it public if you want.

In my PR #12934, I modified the internal _Py_CURRENTLY_FINALIZING() macro to add a runtime parameter :-) (PR currently under review.)

Sorry, something went wrong.

Uh oh!

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

All reactions
@nanjekyejoannah nanjekyejoannah mentioned this pull request Apr 25, 2019
bpo-36479: Exit threads when interpreter is finalizing rather than runtime #12679
Closed
@nanjekyejoannah
Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly
4f6b32d
@nanjekyejoannah
nanjekyejoannah force-pushed the issue36475 branch from fe8a3aa to 4f6b32d Compare April 25, 2019 21:49
nanjekyejoannah added 2 commits April 25, 2019 18:02
@nanjekyejoannah
Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly
2c02944
@nanjekyejoannah
space fix
b8a1690
@nanjekyejoannah
Remove new line.
f2337be
@nanjekyejoannah

nanjekyejoannah commented Apr 25, 2019

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels Apr 25, 2019
@bedevere-bot

bedevere-bot commented Apr 25, 2019

Copy link
Copy Markdown

Thanks for making the requested changes!

@pitrou, @ncoghlan: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Apr 26, 2019
bpo-36356: Destroy the GIL at exit #12453
Merged
@vstinner

vstinner commented Apr 26, 2019

Copy link
Copy Markdown
Member

FYI I wrote PR #12453 "Destroy the GIL at exit".

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed Apr 26, 2019
View reviewed changes
Comment thread Python/ceval.c Resolved

Uh oh!

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

ncoghlan
ncoghlan approved these changes Apr 26, 2019
View reviewed changes

@ncoghlan ncoghlan left a comment

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.

This looks good to me now (noting the follow-up PR in #12679 to switch the thread cleanup to be per-interpreter rather than per-runtime)

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Doc/c-api/init.rst Outdated
:c:func:`PyEval_RestoreThread` is a higher-level function which is always
available (even when threads have not been initialized).

.. note::

@ncoghlan ncoghlan Apr 26, 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.

Ended up with two copies of this note.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting change review labels Apr 26, 2019
nanjekyejoannah added 3 commits April 26, 2019 13:52
@nanjekyejoannah
Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly
5daac23
@nanjekyejoannah
Merge branch 'issue36475' of https://github.com/nanjekyejoannah/cpython…
d14cedb
… into issue36475
@nanjekyejoannah
Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly
4e1d820
@nanjekyejoannah

nanjekyejoannah commented Apr 26, 2019

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting merge labels Apr 26, 2019
@bedevere-bot

bedevere-bot commented Apr 26, 2019

Copy link
Copy Markdown

Thanks for making the requested changes!

@pitrou, @ncoghlan: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed Apr 26, 2019
View reviewed changes
Comment thread Python/ceval.c Resolved

Uh oh!

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

@vstinner
vstinner dismissed pitrou’s stale review April 29, 2019 08:25

Joannah addressed Antoine's comments.

@vstinner
vstinner merged commit f781d20 into python:master Apr 29, 2019
@bedevere-bot bedevere-bot removed the awaiting change review label Apr 29, 2019
@vstinner

vstinner commented Apr 29, 2019

Copy link
Copy Markdown
Member

I am very scared by this change touching critical parts of Python, but I'm somehow confident that it's the right thing to do.

Antoine: Py_Finalize() explicitly waits until all non-daemon threads complete, so this change should impact "regular" threads: only daemon threads.

Note: I have no idea of the impact on performances, but IMHO correctness matters more than performance here. If there is an overhead, I expect it to be not significant on macro benchmarks, and low on micro-benchmarks.

--

Side note. I really hate the whole concept of daemon threads. Kill a thread anytime is just a very high risk of inconsistency. The thread can be killed while writing in the middle of a file, it can leave temporary files, etc. I would prefer to remove the concept of daemon threads and require developers to call pthread_kill() to make them understand what they are doing (something stupid IMHO :-)). Daemon threads make Python finalization very fragile, but the GIL + this PR make the finalization less fragile. Hint: search for "daemon" in Google Image. IMHO the name is well chosen.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Apr 29, 2019

Copy link
Copy Markdown
Member

I am not comfortable to backport this change to Python 3.7. It's too early to know how it will impact applications and how many complains we will get :-) If someone really wants to backport this scary change to 3.7, I would suggest to wait for 1 month after Python 3.8.0 final release.

All reactions

Sorry, something went wrong.

Uh oh!

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

@nanjekyejoannah
nanjekyejoannah deleted the issue36475 branch April 29, 2019 16:46
@vstinner vstinner mentioned this pull request May 2, 2019
bpo-36475: Make PyThread_exit_thread with _Py_NO_RETURN #13068
Merged
@ericsnowcurrently ericsnowcurrently mentioned this pull request Sep 23, 2023
PyThreadState_Swap() During Finalization Causes Immediate Exit (AKA Daemon Threads Are Still the Worst!) #109793
Closed
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 approved these changes
@pitrou pitrou pitrou left review comments
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently
@pablogsal pablogsal Awaiting requested review from pablogsal
+1 more reviewer
@ZackerySpytz ZackerySpytz ZackerySpytz 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

None yet

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.

8 participants

@nanjekyejoannah @vstinner @pablogsal @pitrou @bedevere-bot @ncoghlan @ZackerySpytz @the-knights-who-say-ni
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.