Issue1313

classification
Title: KeyboardInterrupt does not catch ctrl-c
Type: Severity: normal
Components: Core Versions: 25rc4, 2.2.2, 2.5b1
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: draghuram, fwierzbicki, marc, pekka.klarck, pjenvey
Priority: low Keywords:

Created on 2009-04-11.19:11:38 by pekka.klarck, last changed 2010-08-22.22:47:03 by zyasoft.

Files
File name Uploaded Description Edit Remove
ctrlc.py pekka.klarck, 2009-04-11.19:11:37
Messages
msg4501 (view) Author: Pekka Klärck (pekka.klarck) Date: 2009-04-11.19:11:37
It seems that 'except KeyboardInterrupt' or even plain 'except' is not
enough to catch ctrl-c with Jython. This is unfortunate because it makes
it prevents stopping applications started from the command line gracefully. 

This problem can be tested with the attached script. When I execute it
with Jython (tested both with 2.2 and 2.5b3) on my OS X and press ctrl-c
it simply quits without even a traceback. With CPython it prints both
'KeyboardInterrupt' and 'Bye!' before exiting cleanly.

Issue 1746106 is probably related to this problem:
http://bugs.jython.org/issue1746106
msg4564 (view) Author: Raghuram Devarakonda (draghuram) Date: 2009-04-21.18:28:37
The same behaviour can be observed on Linux as well. If CTRL-C is
entered at the python command prompt, a KeyboardInterrupt is shown and
the command prompt appears again. This is handy when I am in the middle
of editing a line and just needs to get back to the prompt. How ever, if
the same is done in Jython, the interpreter exits. I tested with 2.5b3.
I am unable to add a new version to the tracker at the moment so I am
not selecting any version.
msg4645 (view) Author: (marc) Date: 2009-05-06.12:22:34
I'm currently using the following code as workaround:

import signal

def intHandler(signum, frame):
    print "user pressed ctrl-c"
    raise KeyboardInterrupt()

# Set the signal handler
signal.signal(signal.SIGINT, intHandler)

This installs a special handler for the interrupt signal, which raises a
KeyboardInterrupt.
msg5192 (view) Author: Pekka Klärck (pekka.klarck) Date: 2009-09-24.13:01:40
I tested the workaround suggested by marc on 2.5.1 rc 3 but it didn't
work. After adding those lines to the attached ctrlc.py script, I wasn't
able to stop the execution at all. Pressing ctrl-c just gave the
following traceback every time I hit it and even 'kill -9' left a
defunct java process.

^CException in thread "SIGINT handler" Traceback (most recent call last):
  File "/home/peke/Prog/jython2.5.1rc3/Lib/signal.py", line 113, in handle
    self.action(signal.getNumber(), None)
  File "Lataukset/ctrlc.py", line 5, in intHandler
    raise KeyboardInterrupt()
KeyboardInterrupt


The reason I'm interested about this issue is that I'd like to implement
a possibility to stop Robot Framework test execution gracefully from the
command line: http://code.google.com/p/robotframework/issues/detail?id=108
msg5653 (view) Author: Philip Jenvey (pjenvey) Date: 2010-04-11.00:07:44
It's not that straightforward to implement this like CPython does. Creating a signal handler that handles SIGINT is easy enough, but Java's signal support will call the handler in a brand new thread -- what you're really asking for is for Jython to always call the handler in the main thread like CPython does, interrupting any operation in progress
History
Date User Action Args
2010-08-22 22:47:03zyasoftsetpriority: low
2010-04-11 00:07:45pjenveysetnosy: + pjenvey
messages: + msg5653
2009-09-24 13:01:40pekka.klarcksetmessages: + msg5192
2009-08-06 14:47:48fwierzbickisetnosy: + fwierzbicki
2009-05-06 12:22:34marcsetmessages: + msg4645
2009-05-06 11:51:40marcsetversions: + 25rc4
2009-05-06 11:50:34marcsetnosy: + marc
2009-04-21 18:28:38draghuramsetnosy: + draghuram
messages: + msg4564
versions: + 2.2.2, 2.5b1
2009-04-11 19:11:38pekka.klarckcreate