Issue1313

classification
Title: KeyboardInterrupt does not catch ctrl-c
Type: Severity: normal
Components: Core Versions: Jython 2.5
Milestone:
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: amak Nosy List: amak, doublep, draghuram, fwierzbicki, marc, pekka.klarck, ph4r05, pjenvey, zyasoft
Priority: low Keywords: console

Created on 2009-04-11.19:11:38 by pekka.klarck, last changed 2015-03-11.23:35:18 by zyasoft.

Files
File name Uploaded Description Edit Remove
ctrlc.py pekka.klarck, 2009-04-11.19:11:37
TestJython.java ph4r05, 2012-02-25.16:07:58 workaround code, tiny Jython console
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
msg6428 (view) Author: Joey Jarosz (joey@cadence.com) Date: 2011-03-04.22:18:59
Any movement on this problem? I am still looking for a way to interrupt the interpreter.
msg6734 (view) Author: (doublep) Date: 2011-11-29.08:59:21
This is an important problem, as I expect (proper) programs to quit nicely after hitting Ctrl-C.  E.g. 'finally' and 'with' statements do what they promise to do and clean up correspondingly.  Instead, Ctrl-C just kills Jython scripts outright.
msg6784 (view) Author: ph4r05 SVK (ph4r05) Date: 2012-02-25.16:07:58
Hi everyone, 
i have worked on this problem several hours and now finally got solution.

I am using jython-2.5.3b1 library in my code. There should be attached my workaround code for this problem. It is complete Jython console, available to interrupt current code with SIGINT - worked for me 100%.

Short description of solution:
I use sighandler with few modifications as Marc suggested.
In sighandler I call to sys._jy_interpreter.interrupt(thp) where thp is ThreadState of main execution thread - here is need to differ between thread handling signals and main execution thread. Thus we need store ThreadState somewhere to reach it from different thread - sighandler.

Such call will cause Java Exception java.lang.Error and InteractiveConsole terminates. BUT when we start new InteractiveConsole, python state will remain same, thus environment objects will be still available as before SIGINT sending.

IMHO my workaround code could be easily adapted to fix this issue in jython, if needed. I leave this for others since I know Jython project only a few weeks.

Please consult attached workaround code. After start, it automatically registers own SIGINT handler. You can test it with this piece of code:
def fib( n ):
   if n == 0 or n == 1:
      return n
   else:
      return fib( n - 1 ) + fib( n - 2 )

Try running fib(10), this should work correctly. Next try to run fib(1000), this should freeze for a quite long time. Send SIGINT with CTRL-C. Code is interrupted and new instance of console is launched. Now you can verify that you can still call fib(5) correctly, as before freeze and console restart.

There is little problem with this solution - what if you really want to exit console? In this situation we can program not to start console again.

Hope this helps.
msg7405 (view) Author: Alan Kennedy (amak) Date: 2012-08-14.21:44:16
I think that this is to do with the JLineConsole.

If you change the "python.console" property in the registry file to "org.python.util.InteractiveConsole", i.e

python.console=org.python.util.InteractiveConsole

The ctrl-c starts working fine.

If you leave it at the default JLine console (http://jline.sourceforge.net/) then ctrl-c seems to get treated as a normal character (shows up as a heart symbol on my windows command line).

I suggest you try a different console class until this is fixed.

I'll look into how we can address this.

In the meantime, I would be interested in reports to see if changing the console class fixes the issue on various platforms.
msg7407 (view) Author: (doublep) Date: 2012-08-15.09:28:48
In my case I run scripts non-interactively.  Would changing console class affect anything in this case?
msg7408 (view) Author: (doublep) Date: 2012-08-15.09:34:14
By the way, I also use the workaround of installing a custom handler for signal.SIGINT now, to avoid ctrl-C killing the program without cleanup.   Custom handler sets an internal flag to True, then I have to periodically test (yes, manually, and this sucks!) it and raise a KeyboardInterrupt if it's set.

In my case it's very important as I rollback database changes and drop some temporary tables on exceptions.  With stock Jython ctrl-C leaves those tables existing, spoiling future runs.
msg7887 (view) Author: Philip Jenvey (pjenvey) Date: 2013-03-01.20:40:54
No, the console type wouldn't affect non interactive usage
msg9626 (view) Author: Jim Baker (zyasoft) Date: 2015-03-11.23:35:18
Duplicate of #1270
History
Date User Action Args
2015-03-11 23:35:18zyasoftsetstatus: open -> closed
resolution: duplicate
messages: + msg9626
nosy: + zyasoft
2013-03-01 20:40:54pjenveysetmessages: + msg7887
2013-02-26 18:04:22joey@cadence.comsetnosy: - joey@cadence.com
2013-02-25 22:01:42amaksetkeywords: + console
2013-02-19 18:34:35fwierzbickisetversions: + Jython 2.5, - 2.2.2, 2.5.1, 2.5b1, 25rc4
2012-08-15 09:34:14doublepsetmessages: + msg7408
2012-08-15 09:28:49doublepsetmessages: + msg7407
2012-08-14 21:44:17amaksetassignee: amak
messages: + msg7405
nosy: + amak
2012-02-25 16:07:59ph4r05setfiles: + TestJython.java
nosy: + ph4r05
messages: + msg6784
2011-11-29 08:59:21doublepsetnosy: + doublep
messages: + msg6734
versions: + 2.5.1
2011-03-04 22:19:00joey@cadence.comsetnosy: + joey@cadence.com
messages: + msg6428
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