dlv: make headless servers quit automatically when client disconnects (#895)

This commit is contained in:
Alessandro Arzilli 2017-06-26 20:45:13 +02:00 committed by Derek Parker
parent f934029077
commit 50f6382307
6 changed files with 19 additions and 13 deletions

@ -455,6 +455,8 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
Stop(bool) error
}
disconnectChan := make(chan struct{})
// Create and start a debugger server
switch APIVersion {
case 1, 2:
@ -467,6 +469,8 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
WorkingDir: WorkingDir,
Backend: Backend,
CoreFile: coreFile,
DisconnectChan: disconnectChan,
}, Log)
default:
fmt.Printf("Unknown API version: %d\n", APIVersion)
@ -496,7 +500,10 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
fmt.Printf("API server listening at: %s\n", listener.Addr())
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT)
<-ch
select {
case <-ch:
case <-disconnectChan:
}
err = server.Stop(true)
} else {
// Create and start a terminal

@ -76,17 +76,6 @@ func TestBuild(t *testing.T) {
stdout, err := cmd.StdoutPipe()
assertNoError(err, t, "stdout pipe")
cmd.Start()
done := make(chan struct{})
defer func() {
if runtime.GOOS != "windows" {
cmd.Process.Signal(os.Interrupt)
} else {
// sending os.Interrupt on windows is not supported
cmd.Process.Kill()
}
<-done
cmd.Wait()
}()
scan := bufio.NewScanner(stdout)
// wait for the debugger to start
@ -95,7 +84,6 @@ func TestBuild(t *testing.T) {
for scan.Scan() {
// keep pipe empty
}
close(done)
}()
client := rpc2.NewClient(listenAddr)
@ -104,4 +92,7 @@ func TestBuild(t *testing.T) {
if !state.Exited {
t.Fatal("Program did not exit")
}
client.Detach(true)
cmd.Wait()
}

@ -698,6 +698,7 @@ func (p *Process) Detach(kill bool) error {
if p.process != nil {
p.process.Kill()
<-p.waitChan
p.process = nil
}
return p.bi.Close()
}

@ -31,4 +31,7 @@ type Config struct {
// Selects server backend.
Backend string
// DisconnectChan will be closed by the server when the client disconnects
DisconnectChan chan<- struct{}
}

@ -45,6 +45,7 @@ func (c *RPCClient) LastModified() time.Time {
}
func (c *RPCClient) Detach(kill bool) error {
defer c.client.Close()
out := new(DetachOut)
return c.call("Detach", DetachIn{kill}, out)
}

@ -321,6 +321,9 @@ func (s *ServerImpl) serveJSONCodec(conn io.ReadWriteCloser) {
}
}
codec.Close()
if !s.config.AcceptMulti && s.config.DisconnectChan != nil {
close(s.config.DisconnectChan)
}
}
// A value sent as a placeholder for the server's response value when the server