Skip to main content

Exit the whole process if any thread throws an unhandled exception

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unhandled_exit-1.0.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unhandled_exit-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file unhandled_exit-1.0.0.tar.gz.

File metadata

  • Download URL: unhandled_exit-1.0.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for unhandled_exit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 935a4e752e98b2d76a0afba6b47c7a692231a08158a60e90df559985596f22fb
MD5 f4353bc550a04696d7246dda70a43fd5
BLAKE2b-256 5824f80ef91968bdbb2398774646e7fada0e71cddd07d006c612f86e62bdac5c

See more details on using hashes here.

File details

Details for the file unhandled_exit-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for unhandled_exit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 100df644ab9de07c7a33b4bd6553dcc14f42cdbf5de9a2cdb7e0dbe1c99850d8
MD5 cbc34ccc980555a85273bb560702fdd3
BLAKE2b-256 8af4c900d50aee7eead3170e0dd50a192d19f5df8ca7e0ece59b9f73a858215c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page