From 227dc81036e1835b9521c0cf852ba44f86f2393d Mon Sep 17 00:00:00 2001 From: sqshq Date: Sun, 9 Jun 2019 10:37:05 -0400 Subject: [PATCH] close interactive shell cmd on session restart --- data/int_pty.go | 8 +++++--- data/int_shell.go | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/data/int_pty.go b/data/int_pty.go index 4c623f8..dd9725a 100644 --- a/data/int_pty.go +++ b/data/int_pty.go @@ -25,7 +25,7 @@ type PtyInteractiveShell struct { item *Item variables []string cmd *exec.Cmd - File io.WriteCloser + file io.WriteCloser ch chan string errCount int } @@ -50,7 +50,7 @@ func (s *PtyInteractiveShell) init() error { }() s.cmd = cmd - s.File = file + s.file = file s.ch = channel _, err = file.Read(make([]byte, 4096)) @@ -65,10 +65,12 @@ func (s *PtyInteractiveShell) init() error { func (s *PtyInteractiveShell) execute() (string, error) { - _, err := io.WriteString(s.File, fmt.Sprintf(" %s\n", s.item.sampleScript)) + _, err := io.WriteString(s.file, fmt.Sprintf(" %s\n", s.item.sampleScript)) if err != nil { s.errCount++ if s.errCount > errorThreshold { + _ = s.cmd.Wait() + _ = s.file.Close() s.item.ptyShell = nil // restart session } return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err)) diff --git a/data/int_shell.go b/data/int_shell.go index f6b5c3a..70fa7dd 100644 --- a/data/int_shell.go +++ b/data/int_shell.go @@ -24,6 +24,7 @@ func (s *BasicInteractiveShell) init() error { cmd := exec.Command("sh", "-c", *s.item.initScript) enrichEnvVariables(cmd, s.variables) + cmd.Wait() stdout, err := cmd.StdoutPipe() if err != nil { @@ -72,6 +73,7 @@ func (s *BasicInteractiveShell) execute() (string, error) { if err != nil { s.errCount++ if s.errCount > errorThreshold { + _ = s.cmd.Wait() s.item.basicShell = nil // restart session } return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err))