New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(*Instance) Close function call blocks until line is received #52

Closed
tech10 opened this Issue May 6, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@tech10
Copy link

tech10 commented May 6, 2016

Hello,
I have the following scenario in a program I'm writing.

Connection to one or more servers in their own go routines, with a sync.WaitGroup that will terminate the program if all servers are disconnected. Signal monitoring, which also monitors a boolean quit channel, so if a quit command is entered in the terminal window, the program will quit. Checks on the readline instance to determine if an interrupt of any kind is received, which will also quit the program. Entering the quot command in the terminal, or CTRL+C for interrupts shuts the program down cleanly, but trying to kill the program by sending it a signal will not terminate the program until enter is pressed, or likely, some other signal interrupt received.

Briefly, here is how I believe this could be reproduced.

  1. Write a short program with a sync.WaitGroup, and create the following go routines. A go routine monitoring signals sent to the process by the operating system, and a go routine monitoring the input received by the user, using a variable containing a readline instance, and the Line function for receiving data and error checking. Error checking should quit the input monitoring routine, and send to the signal monitoring routine on a different channel to close the program.
  2. Attempt to kill the program by sending one of the monitored signals. The program will not end until a new line is received.

My question is this. Is the Close() function call supposed to block like this? Or, could I read input in another way that would return an error immediately after the readline instance is closed, and not block on the readline instance Close() method? I could then completely end the program without the need for an extra step of entering a blank newline or otherwise.

Blake

hashrocketeer pushed a commit to hashrocket/ws that referenced this issue Aug 31, 2016

@jackc

This comment has been minimized.

Copy link

jackc commented Aug 31, 2016

I believe I am experiencing the same issue with https://github.com/hashrocket/ws. That is a tool for connecting to a websocket server (similar to telnet). In one goroutine I read from readline and in another I read from the websocket. If/when the websocket is closed I want to also close readline and terminate the program. Unfortunately, Readline() blocks until a key is pressed. And Close() blocks until Readline is finished. I can simply os.Exit() the program, but then the terminal may be left in a bad state.

For what it's worth I hacked around the issue with this change:

diff --git a/vendor/github.com/chzyer/readline/terminal.go b/vendor/github.com/chzyer/readline/terminal.go
index 0f1d393..16b9bde 100644
--- a/vendor/github.com/chzyer/readline/terminal.go
+++ b/vendor/github.com/chzyer/readline/terminal.go
@@ -162,7 +162,7 @@ func (t *Terminal) Close() error {
                return nil
        }
        t.stopChan <- struct{}{}
-       t.wg.Wait()
+       // t.wg.Wait()
        return t.ExitRawMode()
 }

@chzyer chzyer closed this in #72 Sep 1, 2016

@chzyer

This comment has been minimized.

Copy link
Owner

chzyer commented Sep 1, 2016

@tech10 @jackc
Now we can call (*Instance).Close() to close readline even user doesn't input anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment