dlv: make headless servers quit automatically when client disconnects (#895)
This commit is contained in:
parent
f934029077
commit
50f6382307
@ -455,6 +455,8 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
|
|||||||
Stop(bool) error
|
Stop(bool) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
disconnectChan := make(chan struct{})
|
||||||
|
|
||||||
// Create and start a debugger server
|
// Create and start a debugger server
|
||||||
switch APIVersion {
|
switch APIVersion {
|
||||||
case 1, 2:
|
case 1, 2:
|
||||||
@ -467,6 +469,8 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
|
|||||||
WorkingDir: WorkingDir,
|
WorkingDir: WorkingDir,
|
||||||
Backend: Backend,
|
Backend: Backend,
|
||||||
CoreFile: coreFile,
|
CoreFile: coreFile,
|
||||||
|
|
||||||
|
DisconnectChan: disconnectChan,
|
||||||
}, Log)
|
}, Log)
|
||||||
default:
|
default:
|
||||||
fmt.Printf("Unknown API version: %d\n", APIVersion)
|
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())
|
fmt.Printf("API server listening at: %s\n", listener.Addr())
|
||||||
ch := make(chan os.Signal)
|
ch := make(chan os.Signal)
|
||||||
signal.Notify(ch, syscall.SIGINT)
|
signal.Notify(ch, syscall.SIGINT)
|
||||||
<-ch
|
select {
|
||||||
|
case <-ch:
|
||||||
|
case <-disconnectChan:
|
||||||
|
}
|
||||||
err = server.Stop(true)
|
err = server.Stop(true)
|
||||||
} else {
|
} else {
|
||||||
// Create and start a terminal
|
// Create and start a terminal
|
||||||
|
@ -76,17 +76,6 @@ func TestBuild(t *testing.T) {
|
|||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
assertNoError(err, t, "stdout pipe")
|
assertNoError(err, t, "stdout pipe")
|
||||||
cmd.Start()
|
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)
|
scan := bufio.NewScanner(stdout)
|
||||||
// wait for the debugger to start
|
// wait for the debugger to start
|
||||||
@ -95,7 +84,6 @@ func TestBuild(t *testing.T) {
|
|||||||
for scan.Scan() {
|
for scan.Scan() {
|
||||||
// keep pipe empty
|
// keep pipe empty
|
||||||
}
|
}
|
||||||
close(done)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
client := rpc2.NewClient(listenAddr)
|
client := rpc2.NewClient(listenAddr)
|
||||||
@ -104,4 +92,7 @@ func TestBuild(t *testing.T) {
|
|||||||
if !state.Exited {
|
if !state.Exited {
|
||||||
t.Fatal("Program did not exit")
|
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 {
|
if p.process != nil {
|
||||||
p.process.Kill()
|
p.process.Kill()
|
||||||
<-p.waitChan
|
<-p.waitChan
|
||||||
|
p.process = nil
|
||||||
}
|
}
|
||||||
return p.bi.Close()
|
return p.bi.Close()
|
||||||
}
|
}
|
||||||
|
@ -31,4 +31,7 @@ type Config struct {
|
|||||||
|
|
||||||
// Selects server backend.
|
// Selects server backend.
|
||||||
Backend string
|
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 {
|
func (c *RPCClient) Detach(kill bool) error {
|
||||||
|
defer c.client.Close()
|
||||||
out := new(DetachOut)
|
out := new(DetachOut)
|
||||||
return c.call("Detach", DetachIn{kill}, out)
|
return c.call("Detach", DetachIn{kill}, out)
|
||||||
}
|
}
|
||||||
|
@ -321,6 +321,9 @@ func (s *ServerImpl) serveJSONCodec(conn io.ReadWriteCloser) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
codec.Close()
|
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
|
// A value sent as a placeholder for the server's response value when the server
|
||||||
|
Loading…
Reference in New Issue
Block a user