Search code examples
pythonbashgitkeyboardinterruptcontrol-c

Python script in Git Bash ignores keyboard interrupt Control C


Python script in Git Bash ignores keyboard interrupt Control C when running the script directly.

This is a simple code to test, called test_sleep_interrupt.py. It sleeps 10 times, 1 second each time. I will type Control C in between.

#!/usr/bin/env python
import time
for i in range(0, 10):
    print(f"sleep #{i}")
    time.sleep(1)

When I run the script directly, Control C is ignored

$ ./test_sleep_interrupt.py
sleep #0
sleep #1
sleep #2
(hitting Control-C many times, no effect)
sleep #3
sleep #4
sleep #5
sleep #6
sleep #7
sleep #8
sleep #9

When I run it through python, Control-C works immediately

$ python ./test_sleep_interrupt.py
sleep #0
sleep #1
(typed Control-C)
Traceback (most recent call last):
  File "./test_sleep_interrupt.py", line 5, in <module>
    time.sleep(1)
KeyboardInterrupt

What happens here? Is there a fix to make Control-C work when directly calling the script?

I am using Windows 10, Python 3.7.3, Git Bash is mintty-2.9.6


Solution

  • Following @ConnorLow's suggestion, I upgraded my Git for Windows to the latest version 2.28.0, which includes Git Bash mintty 3.2.0. This fixed the reported problem.