This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Runaway programs often become unresponsive to CTRL-C
Type: Stage:
Components: Interpreter Core Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, bernie
Priority: normal Keywords:

Created on 2009-09-13 18:10 by bernie, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg92576 - (view) Author: Bernie H. Innocenti (bernie) Date: 2009-09-13 18:10
On startup, the Python interpreter changes the default behavior
of SIGINT, which results in  many Python programs to ignore the
keyboard interrupt exactly in the situations when users are
most likely to use it (i.e.: when the program becomes unresponsive).

Minimal testcase:
 $ echo "void foo() { for(;;) {} }" >foo.c
 $ gcc -shared -o foo.so foo.c
 $ python -c 'import ctypes;ctypes.CDLL("./foo.so").foo()'
 ^C^C^C
 ^C
 ^C DAMN!
 ^C

This scenario mimics a Python program calling some blocking library
function.  It can also happen with IO-bound functions if they loop
on read() and don't abort on short reads.

One might be tempted to say "this behavior of the Python intepreter
is by design" and suggest users to use CTRL-\ instead of CTRL-C.
However, this non-standard behavior is very annoying for users who
expect ^C to work on UNIX systems.  In fact, no other compiled or
interpreted language I know of behaves this way, and Python should
not be the only exception.

While I see the usefulness of KeyboardInterrupt from the programmer
point of view, only a minority of programs actually need to trap
SIGINT and do something with it.  Other language runtimes require
the programmer to manually trap SIGINT when needed.  The Python
interpreter could maintain backwards compatibility by enabling
automatic SIGINT trapping when entering a  "try" block that would
intercept KeyboardInterrupt.
msg92577 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-09-13 18:18
This is a fundemental behavior that will never change. If you dislike
it, you can remove the signal handler for it with the signal module.
msg92580 - (view) Author: Bernie H. Innocenti (bernie) Date: 2009-09-13 18:35
> This is a fundemental behavior that will never change. If you
> dislike it, you can remove the signal handler for it with the
> signal module.

What?  We could break the syntax of "print" statements and
cannot change this minor detail that afftects many 1% of all
Python programs?

As a matter of fact, for 2 years I've been using this in my
/usr/lib64/python2.6/sitecustomize.py:

----cut-----
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
----cut-----

^C has been working perfectly ever since.  So far, I have not
yet found a single Python program where restoring the default
behavior of SIGINT causes real issues, but there may certainly
be a few.

Granted, this is just a kludge, not a perfect fix, but from a
user perspective, it already improves upon the current behavior
(i.e. more pros than cons).

At least, this is my personal experience.  If you're skeptical,
please try this workaround yourself for a few months and let me
know what breaks for you.
msg92581 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-09-13 18:40
Regardless of whether this is a good idea or not, it's too broad a
change for the bug tracker. Please bring it up on python-ideas if you
wish to pursue the topic.
msg92582 - (view) Author: Bernie H. Innocenti (bernie) Date: 2009-09-13 18:48
Ok
History
Date User Action Args
2022-04-11 14:56:52adminsetgithub: 51150
2009-09-13 18:48:31berniesetmessages: + msg92582
2009-09-13 18:40:28benjamin.petersonsetmessages: + msg92581
2009-09-13 18:35:58berniesetmessages: + msg92580
2009-09-13 18:18:13benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg92577

resolution: works for me
2009-09-13 18:10:48berniecreate