Skip to content

Commit

Permalink
terminal: prompt to kill remote if process exited (go-delve#1621)
Browse files Browse the repository at this point in the history
  • Loading branch information
briandealwis authored and derekparker committed Jul 17, 2019
1 parent ebe7976 commit 95d619e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terminal
import (
"fmt"
"io"
"net/rpc"
"os"
"os/signal"
"runtime"
Expand Down Expand Up @@ -353,6 +354,18 @@ func (t *Term) handleExit() (int, error) {

s, err := t.client.GetState()
if err != nil {
if isErrProcessExited(err) && t.client.IsMulticlient() {
answer, err := yesno(t.line, "Remote process has exited. Would you like to kill the headless instance? [Y/n] ")
if err != nil {
return 2, io.EOF
}
if answer {
if err := t.client.Detach(true); err != nil {
return 1, err
}
}
return 0, err
}
return 1, err
}
if !s.Exited {
Expand Down Expand Up @@ -404,3 +417,9 @@ func (t *Term) loadConfig() api.LoadConfig {

return r
}

// isErrProcessExited returns true if `err` is an RPC error equivalent of proc.ErrProcessExited
func isErrProcessExited(err error) bool {
rpcError, ok := err.(rpc.ServerError)
return ok && strings.Contains(rpcError.Error(), "has exited with status")
}
20 changes: 20 additions & 0 deletions pkg/terminal/terminal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package terminal

import (
"errors"
"net/rpc"
"runtime"
"testing"

Expand Down Expand Up @@ -85,3 +87,21 @@ func TestSubstitutePath(t *testing.T) {
}
}
}

func TestIsErrProcessExited(t *testing.T) {
tests := []struct {
name string
err error
result bool
}{
{"empty error", errors.New(""), false},
{"non-ServerError", errors.New("Process 33122 has exited with status 0"), false},
{"ServerError with zero status", rpc.ServerError("Process 33122 has exited with status 0"), true},
{"ServerError with non-zero status", rpc.ServerError("Process 2 has exited with status 25"), true},
}
for _, test := range tests {
if isErrProcessExited(test.err) != test.result {
t.Error(test.name)
}
}
}

0 comments on commit 95d619e

Please sign in to comment.