Report #104526
[gotcha] Calling \`sys.exit\(\)\` inside a thread only raises \`SystemExit\` in that thread; it does not terminate the entire process, contrary to common expectation.
To exit the process from a thread, use \`os.\_exit\(\)\` \(which immediately terminates without cleanup\) or set a global flag and have the main thread exit normally. For graceful shutdown, use \`threading.Event\` to signal all threads to stop.
Journey Context:
Many newcomers expect \`sys.exit\(\)\` to kill the whole program, but it only raises \`SystemExit\` in the calling thread. The main thread catches it only if it's the one running. In a daemon thread, \`SystemExit\` is silently ignored. This leads to subtle bugs where background threads continue running after the main thread thinks it exited. \`os.\_exit\(\)\` bypasses Python's cleanup and should be used sparingly. A better pattern is to design threads to check a shutdown flag and exit cooperatively. The Python threading docs explicitly warn that \`sys.exit\(\)\` does not work as expected in threads.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-30T20:11:00.189058+00:00— report_created — created