X Tutup
The Wayback Machine - https://web.archive.org/web/20250603052546/https://github.com/python/cpython/issues/126168
Skip to content

atexit.register_early pre-finalization callback API #126168

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

Closed
ncoghlan opened this issue Oct 30, 2024 · 1 comment
Closed

atexit.register_early pre-finalization callback API #126168

ncoghlan opened this issue Oct 30, 2024 · 1 comment
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@ncoghlan
Copy link
Contributor

ncoghlan commented Oct 30, 2024

Feature or enhancement

Proposal:

The threading module has a private threading._register_atexit API to support running process termination callbacks before the main thread attempts to join all non-daemon threads: https://github.com/python/cpython/blob/3.13/Lib/threading.py#L1492

As discussed in https://discuss.python.org/t/getting-rid-of-daemon-threads/68836, the fact atexit handlers can't be used to issue shutdown requests to non-daemon threads is a genuinely annoying limitation, with daemon threads sometimes needing to be used just so they survive until the atexit handlers run.

This feature request proposes replacing threading._register_atexit with a public atexit.register_early API:

  • callbacks registered this way would be called “early exit callbacks” (executed before finalization starts), while callbacks registered the traditional way would be “late exit callbacks” (executed just before the interpreter is marked as no longer initialized)
  • early exit callbacks would run before sys.is_finalizing() is set, so they'd be able to start new threads and subprocesses as normal (doing that would probably be a bad idea in most situations, but there may be valid reasons for it, such as cleanly dropping access to external resources)
  • early exit callbacks would be able to tell non-daemon threads to shut down before the main thread attempts to join them
  • early exit callbacks would be able to register new late exit callbacks to run
  • early exit callbacks would NOT be able to register additional early exit callbacks

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

@ncoghlan ncoghlan added type-feature A feature request or enhancement interpreter-core (Objects, Python, Grammar, and Parser dirs) extension-modules C modules in the Modules dir 3.14 bugs and security fixes labels Oct 30, 2024
@ncoghlan ncoghlan changed the title Expose threading._register_atexit as a public atexit.register_early API atexit.register_early pre-finalization callback API Oct 30, 2024
@picnixz picnixz removed the 3.14 bugs and security fixes label Dec 15, 2024
@ncoghlan
Copy link
Contributor Author

I decided I didn't like this variation on the idea: https://discuss.python.org/t/daemon-threads-and-background-task-termination/77604

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants
X Tutup