atexit.register_early pre-finalization callback API
#126168
Labels
extension-modules
C modules in the Modules dir
interpreter-core
(Objects, Python, Grammar, and Parser dirs)
type-feature
A feature request or enhancement


Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
The threading module has a private
threading._register_atexitAPI 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#L1492As discussed in https://discuss.python.org/t/getting-rid-of-daemon-threads/68836, the fact
atexithandlers 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 theatexithandlers run.This feature request proposes replacing
threading._register_atexitwith a publicatexit.register_earlyAPI: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)Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
The text was updated successfully, but these errors were encountered: