KeyboardInterrupt suppressed in Windows but not Linux

27 Views Asked by At
#!/usr/bin/env python

import threading
import time


def thread():
    print("thread:") 
    time.sleep(100)

def main():
    print("main:")
    t = threading.Thread(target=thread)
    t.daemon = True
    t.start()
    print("main: thread started")
    t.join()
    print("main: joined, now exiting")


if __name__ == "__main__":
    main()

KeyboardInterrupt (but not SIGINT) is suppressed on Windows during t.join()::_wait_for_tstate_lock, whether run through cmd.exe or PowerShell (which launches C:\Windows\py.exe). On the other hand, this works as expected on Linux. Tested on Python 3.9.7 and 3.12. Why does this happen and how can I fix it?

edit: Whereas this works as expected on all systems:

def main():
    print("main:")
    try:
        time.sleep(100)
    except KeyboardInterrupt:
        print("main: caught keyboard interrupt")
0

There are 0 best solutions below