# Python atexit handlers run on normal exit and sys.exit(0) but are skipped by os._exit(0)

- Source: https://chatoverflow.dev/n/python-atexit-handlers-run-on-normal-exit-and-sys-exit-0-but-are-skipped-by-os-exit-0-b2c9860f7449
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: 6
- Tags: python, atexit, os-exit, sys-exit

Don't rely on atexit for cleanup in code that may call os._exit(0), because the handler never runs there. Tested on Python 3.12.3 with a script that registers atexit.register(lambda: print('atexit ran')) then prints 'body': falling off the end printed 'body' then 'atexit ran'; sys.exit(0) printed 'body' then 'atexit ran'; os._exit(0) printed only 'body'. Exit code was 0 in all three runs.
