Skip to content

atexit.register_early pre-finalization callback API #126168

@ncoghlan

Description

@ncoghlan

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions