You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems to me that threads running when sys.exit() is called during signal handling are killed immediately. It further seems that the try ... finally contract is adhered to on the main thread but broken in other threads.
When running this code and hitting CTRL-C after a short while I get the following output:
$ python testthread.py
Begin f
^Ctermination_signal_handler
Got SystemExit in main
End main
The output indicates that the thread t was killed right away, leaving the finally (and the except SystemExit clause in f) unexecuted.
My expectation would have been that either (XOR)
(a) SystemExit would be propagated to all threads
(b) SystemExit raised on the main thread and the other threads are left running
(c) Nothing happens
According to the Python doc, non daemonic threads should not be killed when the main thread exits. Also, according to several SO posts, the expected behavior would more be in the area of (b).
Note: The same code without the signal handling is behaving in a similar way. The only difference there is that the KeyboardInterupt trace is also logged to the console.
Am I missing something?
Your environment
CPython versions tested on: 3.11.4
Operating system and architecture: macOS-13.4.1-arm64-arm-64bit
The text was updated successfully, but these errors were encountered:
I can reproduce this using the latest 3.12 branch as well as a build of 3.10. But 3.13.0rc2 and main already appear to do what you would expect and the thread is still running keeping the process alive.
I added the labels for the older versions I tested, but 3.11 and 3.10 are in security fix only mode; there's a chance someone might work on this for 3.12 but at least our soon to be latest version is already fixed.
Bug report
It seems to me that threads running when
sys.exit()
is called during signal handling are killed immediately. It further seems that thetry ... finally
contract is adhered to on the main thread but broken in other threads.Consider the following pice of Python code:
When running this code and hitting
CTRL-C
after a short while I get the following output:The output indicates that the thread
t
was killed right away, leaving thefinally
(and theexcept SystemExit
clause inf
) unexecuted.My expectation would have been that either (XOR)
SystemExit
would be propagated to all threadsSystemExit
raised on the main thread and the other threads are left runningAccording to the Python doc, non daemonic threads should not be killed when the main thread exits. Also, according to several SO posts, the expected behavior would more be in the area of (b).
Note: The same code without the signal handling is behaving in a similar way. The only difference there is that the
KeyboardInterupt
trace is also logged to the console.Am I missing something?
Your environment
macOS-13.4.1-arm64-arm-64bit
The text was updated successfully, but these errors were encountered: