Ctrl-C firing twice

150 views
Skip to first unread message

Brad Wood

unread,
Dec 21, 2017, 3:08:20 PM12/21/17
to jline...@googlegroups.com
Whenever I press Ctrl-C while realLine() is running, I'm seeing it fire the UserInterruptException twice most of the time.  I'm on Windows inside of cmd.  This seems to happen whether or not I have the signalHandler in my builder.

If I add a sleep of 200 ms before dealing with it, I don't see the behavior.  In this instance, I'm just outputting the message to the user

Use the "exit" command to leave this shell.

and immediately entering another readLine() call.

I think what is happening is that my key down and key up are possibly both firing the handler, and since I'm immediately re-entering the readLine(), the second firing is getting captured as well.  That would seem to explain why sleeping for 200 ms before running readLine() again doesn't fire the handler a second time.  

Is this a bug?
Do I actually need to set the signal handler since it seems to work regardless?  

I also see that Ctrl-D throws an org.jline.reader.EndOfFileException.  Cool.  Is there any way you can help me understand what the possible signals are, what keystrokes create them, and what Java exceptions to expect?  I realize you said that JLine support the typical terminal signals, but that really doesn't help me know what keystrokes will generate what specific Java exceptions.  

From my Googling, it seems that the actual keystrokes may be interpreted different based on the termain I'm using so I understand if it's not the same across all OS's etc but a basic guide would be great here.

For instance, I'm seeing the standard *nix terminal is supposed to allow:
Ctrl+C - SIGINT
Ctrl+\ - SIGQUIT
Ctrl+Z - SIGTSTP

Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 

Guillaume Nodet

unread,
Dec 21, 2017, 3:38:22 PM12/21/17
to jline...@googlegroups.com
Could you check if the behavior is specific to windows maybe ?
You may be right about keydown / keyup, but that's specific on windows, as other OSes are plain character based.
The signal is raised here:

--
You received this message because you are subscribed to the Google Groups "jline-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jline-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brad Wood

unread,
Dec 21, 2017, 4:02:08 PM12/21/17
to jline...@googlegroups.com
I'll try to do some testing.  Right now this is all local on my Windows dev machine.

Do I actually need to set the signal handler since it seems to work regardless?  Are certain handlers set up by default?

Is there any way you can help me understand what the possible signals are, what keystrokes create them, and what Java exceptions to expect?

Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


Guillaume Nodet

unread,
Dec 21, 2017, 5:19:41 PM12/21/17
to jline...@googlegroups.com
The handlers are handled on the terminal.  Objects using the terminal may register handlers, this is the case for the LineReader, or advanced applications such as the nano editor, less pager, etc...

You may want to set an interrupt signal handler while "executing" the command.  You could intercept it and try to interrupt the command for example...

Brad Wood

unread,
Dec 21, 2017, 6:02:50 PM12/21/17
to jline...@googlegroups.com
You may want to set an interrupt signal handler while "executing" the command.  You could intercept it and try to interrupt the command for example...

Interesting.  So EndOfFileException and UserInterruptException are thrown only while readLine() is executing, but I can register my own handler that will get called ANY time? Does it run in its own thread?  I'm not sure I have a use ATM for this but if I'm understanding you correctly, it seems useful.  For example, if my CLI is downloading a large file, I could try and interrupt the process from this separate thread instead of the user having to sit and wait for the entire thing to finish?

Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


Brad Wood

unread,
Dec 26, 2017, 1:59:50 PM12/26/17
to jline-users
Can you help clarify for me, what it would look like to register my own handler and do my handlers fire in a separate thread that can be invoked even when readLine() is not currently executing?  

Guillaume Nodet

unread,
Dec 26, 2017, 3:00:00 PM12/26/17
to jline...@googlegroups.com
Only a single signal handler is active for a given signal.
When the LineReader starts reading a line, it will set its own handler for the INT, WINCH, CONT signals and will restore the previous one before returning from the readLine() call.

On Tue, Dec 26, 2017 at 7:59 PM, Brad Wood <bdw...@gmail.com> wrote:
Can you help clarify for me, what it would look like to register my own handler and do my handlers fire in a separate thread that can be invoked even when readLine() is not currently executing?  

--

Brad Wood

unread,
Dec 26, 2017, 3:15:37 PM12/26/17
to jline...@googlegroups.com
Thank you for the additional information, however it seems my specific questions still are open:
  1. How can I register a custom handler of my own? (code samples, docs, examples)
  2. Will my custom handler fire even when readLine() is not being executed?

Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


Guillaume Nodet

unread,
Dec 26, 2017, 3:32:33 PM12/26/17
to jline...@googlegroups.com
On Tue, Dec 26, 2017 at 9:15 PM, Brad Wood <bdw...@gmail.com> wrote:
Thank you for the additional information, however it seems my specific questions still are open:
  1. How can I register a custom handler of my own? (code samples, docs, examples)
Signals are used in various places: Less, Nano, Ssh, Tmux, TTop.
Those are good real use-cases.
 
  1. Will my custom handler fire even when readLine() is not being executed?
As I said, the LineReader will set its own handler at the beginning of the readLine() call and will restore it before exiting. So the answer is yes.

Brad Wood

unread,
Dec 26, 2017, 4:46:25 PM12/26/17
to jline-users
Ok, thanks.  I've made a ticket to circle back to this and see about adding some better Ctrl-C support to allow a user to try and interrupt a command that's in the middle of executing via a custom handler.  

Guillaume Nodet

unread,
Dec 26, 2017, 6:12:10 PM12/26/17
to jline...@googlegroups.com
Fwiw, if you really want to, you could also implement foreground / background jobs (the ending & + ^Z).
This is done with signal support:
On Tue, Dec 26, 2017 at 10:46 PM, Brad Wood <bdw...@gmail.com> wrote:
Ok, thanks.  I've made a ticket to circle back to this and see about adding some better Ctrl-C support to allow a user to try and interrupt a command that's in the middle of executing via a custom handler.  

--
Reply all
Reply to author
Forward
0 new messages