Exit the whole process if any thread throws an unhandled exception
Project description
unhandled_exit
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.
Quickstart
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
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for unhandled_exit-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b5e6bcb54d818398b36196e9b5bfa18175438edc6ff3c4550e6ac7f3ac089ca |
|
MD5 | ba8f67d08c3db6fcd7d6ccacac43090f |
|
BLAKE2b-256 | a3ac805673cd4b55dbf66bfed5ef7ba97a619a0ef097b00fb722d82bcc9959c5 |