Why Ctrl+C doesn't stop a program — and how one except line causes it

Look at this script. It does nothing, just waits in a loop:
import time
while True:
try:
time.sleep(0.2)
except:
pass
Run it and press Ctrl+C. The terminal shows ^C — and the script keeps running. Press it ten times if you like.
The culprit is the bare except:. In Python, Ctrl+C turns into a KeyboardInterrupt exception, and an except like that catches it along with every other error. That's one of three common causes. Let's go through all of them — most common first.
First — how Ctrl+C actually works
Ctrl+C doesn't "kill" a program. The terminal sends it a polite request — the SIGINT signal, "please interrupt". By default the program dies on it. But it's allowed to catch the signal and decide for itself.
That gives you a quick diagnosis:
^Cshows up, the program keeps going — the signal arrived, but the program caught it. Causes 1 and 2.- Not even
^Cappears — the signal never reaches the program; the problem is the terminal. Cause 3.
Cause 1: the program catches Ctrl+C itself
The most common one. And often it's code an AI wrote "for robustness".
How to check. Look for a catch in the code:
- Python: a bare
except:orexcept BaseException:around the loop. - Node:
process.on('SIGINT', ...)with noprocess.exit()inside.
We tested both. The Python loop with except: survived SIGINT. Node with a handler printed bye? — and stayed alive too.
How to fix it.
- In Python write
except Exception:— it catches normal errors but letsKeyboardInterruptthrough. We checked: with it, the same loop dies on the very first Ctrl+C. - In Node, if you need a handler, end the program inside it: save what you need and call
process.exit(130). Code 130 is the traditional "interrupted by Ctrl+C" — more on numbers like that in the article on exit codes.
When you ask an AI for a "resilient loop", add to the prompt: "don't swallow KeyboardInterrupt or SIGINT". Otherwise you'll get a program you can't turn off.
Cause 2: the program is in Docker
You ran docker run -it my-app and press Ctrl+C. The signal reaches the container — but your node server.js inside keeps running.
It's about the number. Inside a container your program is often started first and gets process number 1. And for process number 1, the kernel ignores signals it hasn't set up a handler for. A normal program would die from SIGINT by default. Process number 1 won't.
How to check. docker exec <container> ps — if the PID 1 line shows your node or python, that's it.
How to fix it.
- Run with the flag
docker run --init .... Number 1 goes to a tiny helper that passes signals on properly. - Or handle SIGINT and SIGTERM in the program yourself: close connections and exit.
The same effect explains why docker stop sometimes waits exactly ten seconds: Docker asks politely first, then kills by force.
Cause 3: the signal never arrives
You press Ctrl+C and ^C doesn't even print. So the program isn't to blame — the channel itself is stuck.
Options:
- You accidentally pressed Ctrl+S. The terminal is paused and shows nothing. Press Ctrl+Q. This and other cases are covered in why the terminal is frozen.
- The SSH connection dropped. Wi-Fi blinked and your keys go nowhere. Press Enter, then
~and.— that's SSH's emergency exit. - The program runs in the background. If you started it with
&, Ctrl+C doesn't reach it. Find it withjobsorpgrepand stop it withkill.
If you need it stopped right now
A ladder from gentle to hard:
- Ctrl+\ — the SIGQUIT signal. Python doesn't catch it: our stubborn
except:loop died instantly. - Ctrl+Z, then
kill %1— pause the program and kill it by job number. Didn't help —kill -9 %1. - From another terminal window:
pgrep -f my-scriptfinds the number,killasks it to stop,kill -9cuts it off with no discussion.
kill -9 is the last resort: the program won't get to save data and close files.
Why does Ctrl+C copy in one place and stop things in another?
In graphical apps it means "copy". In the terminal it has historically meant "interrupt". Modern terminals usually put copy on Ctrl+Shift+C, and on a Mac on Cmd+C.
What does ^C in the terminal mean?
It's the terminal showing that you pressed Ctrl+C. By itself it stops nothing — the program stops if it agrees to. More handy combos are in the roundup of terminal keyboard shortcuts.
Will Ctrl+C kill the processes my program started?
Usually yes: the signal goes to the whole foreground process group. But anything the program deliberately detached from the terminal can survive. Check with pgrep.
Want to feel confident in the terminal? In KODiQ we break down traps like these in short lessons — with examples you can repeat.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





