KeyboardInterrupt 🐍 Python

KeyboardInterrupt (script stopped with Ctrl+C) / ^C traceback

The script was interrupted (Ctrl+C, SIGINT, or a container stop), producing a KeyboardInterrupt traceback.

Seen on: Python

Meaning

Not a bug in itself — but long-running scripts should catch it to clean up (close files, flush data) and exit gracefully. Docker sends SIGTERM first, which Python doesn’t handle by default.

Common causes

  • Manual Ctrl+C
  • Process manager or container stop
  • Infinite loop that had to be interrupted

⚡ Quick fix

  1. Wrap the main loop in try/except KeyboardInterrupt to clean up
  2. Handle SIGTERM with the signal module in containers
  3. Fix infinite loops if the interrupt was needed

Detailed fix by platform

Python

  1. python
    import signal, sys
    signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))

How to diagnose

  1. Trigger — Who interrupted it?
  2. Cleanup — Was data left half-written?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.