Bug report
Bug description:
The following short program will always run for 10 seconds on Windows, no matter how quickly you press ctrl+C.
fromthreadingimportThreadimporttimet=Thread(target=time.sleep, args=(10,))
t.start()
try:
print("Joining ...")
t.join()
print("Joined without exception")
exceptKeyboardInterrupt:
print("Keyboard interrupt")The program does print "Keyboard interrupt", so the signal handler does actually get run and the exception gets thrown. It just doesn't interrupt the join. Indeed, if the thread function is changed to one that runs forever then this code would wait forever too.
The following expanded version, with a custom signal handler, shows that the signal handler is not run until the join() completes. (As opposed to the possibility that it runs straight away, but then the join() wait resumes before raising the exception. Unlikely I know.)
Version with custom signal handler
importsignalfromthreadingimportThreadimporttimestart_time=time.monotonic()
defpr(s):
print(f"{time.monotonic() -start_time:.2f}{s}")
defsig_handler(signal, frame):
pr("Ctrl-C")
raiseKeyboardInterruptsignal.signal(signal.SIGINT, sig_handler)
t=Thread(target=time.sleep, args=(10,))
t.start()
try:
pr("Joining ...")
t.join()
pr("Joined without exception")
exceptKeyboardInterrupt:
pr("Keyboard interrupt")If ctrl+c is pressed within 10 seconds then this prints:
0.00 Joining ...
10.00 Ctrl-C
10.00 Keyboard interrupt
Related issues:
One of the comments on #66021 is about this Thread.join() rather than the real topic of that issue, but they are told "This is a different issue: bpo-29971. Currently, threading.Lock.acquire() cannot be interrupted by CTRL+C." This suggests to me that #74157 is very related to Thread.join() not being interruptable. But it's not exactly the same ... because it's marked as fixed and this issue still persists.
CPython versions tested on:
3.10, 3.12
Operating systems tested on:
Windows
Bug report
Bug description:
The following short program will always run for 10 seconds on Windows, no matter how quickly you press ctrl+C.
The program does print "Keyboard interrupt", so the signal handler does actually get run and the exception gets thrown. It just doesn't interrupt the join. Indeed, if the thread function is changed to one that runs forever then this code would wait forever too.
The following expanded version, with a custom signal handler, shows that the signal handler is not run until the
join()completes. (As opposed to the possibility that it runs straight away, but then thejoin()wait resumes before raising the exception. Unlikely I know.)Version with custom signal handler
If ctrl+c is pressed within 10 seconds then this prints:
Related issues:
Thread.join()not returning even after the thread is actually finished. In contrast, this issue is about wantingThread.join()to complete before the thread is finished.Lock.acquire()rather thanThread.join().One of the comments on #66021 is about this
Thread.join()rather than the real topic of that issue, but they are told "This is a different issue: bpo-29971. Currently, threading.Lock.acquire() cannot be interrupted by CTRL+C." This suggests to me that #74157 is very related toThread.join()not being interruptable. But it's not exactly the same ... because it's marked as fixed and this issue still persists.CPython versions tested on:
3.10, 3.12
Operating systems tested on:
Windows