Threading Keyboard Interrupt issue

eryk sun eryksun at gmail.com
Wed May 29 14:03:55 EDT 2019


On 5/29/19, David Raymond <David.Raymond at tomtom.com> wrote:
>
> Keyboard interrupts are only received by the main thread, which in this case
> completes real quick.
>
> So what happens for me is that the main thread runs to completion instantly
> and leaves nothing alive to receive the keyboard interrupt, which means the
> loop thread will run forever until killed externally. (Task manager,
> ctrl-break, etc)

The main thread is still running in order to join non-daemon threads.
In Windows, the internal wait used to join a thread can't be
interrupted by Ctrl+C, unlike POSIX platforms.
The Windows build could be modified to support Ctrl+C in this case,
but I'm only certain about the current build that uses emulated
condition variables.

When I run the OP's script in Linux, acquiring the internal
thread-state lock (which normally waits until the lock is reset when
the thread exits) gets interrupted by the SIGINT signal, and
KeyboardInterrupt is raised:

    Exception ignored in: <module 'threading' from
'/usr/lib/python3.6/threading.py'>
    Traceback (most recent call last):
      File "/usr/lib/python3.6/threading.py", line 1294, in _shutdown
        t.join()
      File "/usr/lib/python3.6/threading.py", line 1056, in join
        self._wait_for_tstate_lock()
      File "/usr/lib/python3.6/threading.py", line 1072, in
_wait_for_tstate_lock
        elif lock.acquire(block, timeout):
    KeyboardInterrupt


More information about the Python-list mailing list