Skip to main content

atexit

Callbacks registered with atexit run during normal interpreter shutdown, in LIFO order. They do not run on os._exit() or fatal signals.

Source-of-record: Modules/atexitmodule.c, atexit docs.

API

FunctionBehaviour
register(func, *args, **kwargs)Add a callback; returns func (decorator-friendly).
unregister(func)Remove every registration of func.
_run_exitfuncs()Force-run all callbacks (testing).
_clear()Drop the registry without running callbacks.

Ordering

Callbacks run last-in, first-out. Exceptions are caught and printed via sys.excepthook; they do not stop subsequent callbacks.

Gopy status

AreaState
register / unregisterComplete.
LIFO ordering at shutdownComplete.
Exception isolationComplete.

Reference

  • CPython 3.14: atexit.
  • Modules/atexitmodule.c.
  • module/atexit/. gopy port.