From c71cc217214febfdec842802c0d58cc7cbacde8d Mon Sep 17 00:00:00 2001 From: Iwasaki Yudai Date: Fri, 28 Aug 2015 17:22:42 +0900 Subject: [PATCH] Send SIGHUP to process after closing PTY POSIX doesn't grantee that a blocked Read() operation will be released after closing the file. Moreover, the pty file left intact even after closing the file and the process keeps running in this case. --- app/client_context.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/client_context.go b/app/client_context.go index 572629c..a71ebea 100644 --- a/app/client_context.go +++ b/app/client_context.go @@ -73,6 +73,11 @@ func (context *clientContext) goHandleClient() { <-exit context.pty.Close() + + // Even if the PTY has been closed, + // Read(0 in processSend() keeps blocking and the process doen't exit + context.command.Process.Signal(syscall.SIGHUP) + context.command.Wait() context.connection.Close() log.Printf("Connection closed: %s", context.request.RemoteAddr) @@ -95,14 +100,10 @@ func (context *clientContext) processSend() { return } - writer, err := context.connection.NextWriter(websocket.TextMessage) + err = context.connection.WriteMessage(websocket.TextMessage, append([]byte{Output}, buf[:size]...)) if err != nil { return } - - writer.Write([]byte{Output}) - writer.Write(buf[:size]) - writer.Close() } }