Skip to main content

unhandled_exit

CI PyPI version

unhandled_exit makes the whole process exit if an unhandled exception occours in a thread or in main.

Uses threading.excepthook which is only available in Python 3.8 and later. The automated tests test Python 3.8, 3.9 and 3.10.

Usage

import unhandled_exit
unhandled_exit.activate()

Why

When using Python with threads, an uncaught exception no longer exits the process, but only exits the affected thread.

This can make perfect sense if the application restarts threads as needed, or the threads are disposable.

But if the application does not handle a thread exit, the application can be left running in defunct state.

In this situation I (and some [1] other [2] people) [3]) find it preferable to have the whole process exit with a bang so the service manager can restart it and/or the developer can investigate what went wrong.

API Documentation

activate()

activate() enables the functionality of the unhandled_exit package. After calling activate(), the whole process will exit if an unhandled exception occurs in a thread or in main.

This works by chaining os._exit(1) after threading.excepthook and sys.excepthook.

It is safe to call activate() multiple times. The second and later calls will be no-ops.

deactivate()

deactivate() resets threading.excepthook and sys.excepthook to the values they had before activate().

It is safe to call deactivate() multiple times. The second and later calls will be no-ops.

If threading.excepthook and/or sys.excepthook have been changed between activate() and deactivate(), a warning is printed and this excepthook is left as-is.

Development

Get the code:

git clone https://github.com/rfjakob/unhandled_exit
cd unhandled_exit

Run the Test Suite (needs pip install hatch):

make test

Output should look like this:

python -m compileall -q .
hatch run test
========================== test session starts ===========================
platform linux -- Python 3.10.7, pytest-7.1.3, pluggy-1.0.0 -- /home/jakob/.local/share/hatch/env/virtual/unhandled-exit/PB23ljro/unhandled-exit/bin/python
cachedir: .pytest_cache
rootdir: /home/jakob.donotbackup/code/unhandled_exit
plugins: cov-4.0.0
collected 4 items                                                        

tests/tests.py::test_crash_main PASSED                             [ 25%]
tests/tests.py::test_crash_thread PASSED                           [ 50%]
tests/tests.py::test_crash_single PASSED                           [ 75%]
tests/tests.py::test_nocrash PASSED                                [100%]

=========================== 4 passed in 0.18s ============================

Publish to pypi (needs pip install hatch):

make publish

References

[1] https://stackoverflow.com/questions/49663124/cause-python-to-exit-if-any-thread-has-an-exception

[2] https://stackoverflow.com/questions/57208345/how-to-kill-process-if-thread-encounters-exception

[3] https://stackoverflow.com/questions/71051325/is-there-any-way-to-stop-all-workers-in-a-threadpool-if-any-of-the-worker-raise

Release files for unhandled_exit 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unhandled_exit 1.0.0
File Size Uploaded
unhandled_exit-1.0.0.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unhandled_exit 1.0.0
File Interpreter ABI Platform
unhandled_exit-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / unhandled_exit-1.0.0.tar.gz

Download URL unhandled_exit-1.0.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
935a4e752e98b2d76a0afba6b47c7a692231a08158a60e90df559985596f22fb
BLAKE2b-256 checksum
How to use checksums
5824f80ef91968bdbb2398774646e7fada0e71cddd07d006c612f86e62bdac5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.0

Release files / unhandled_exit-1.0.0-py3-none-any.whl

Download URL unhandled_exit-1.0.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
100df644ab9de07c7a33b4bd6553dcc14f42cdbf5de9a2cdb7e0dbe1c99850d8
BLAKE2b-256 checksum
How to use checksums
8af4c900d50aee7eead3170e0dd50a192d19f5df8ca7e0ece59b9f73a858215c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page