service/rpccommon: halt before detach in Stop (#2677)

* service/rpccommon: halt before detach in Stop

* Addd IsRunning check

Co-authored-by: Polina Sokolova <polinasok@users.noreply.github.com>
This commit is contained in:
polinasok 2021-09-01 10:31:37 -07:00 committed by GitHub
parent 45f44bc218
commit 5d158820f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 3 deletions

@ -254,7 +254,7 @@ func TestContinue(t *testing.T) {
cmd := exec.Command(dlvbin, "debug", "--headless", "--continue", "--accept-multiclient", "--listen", listenAddr)
cmd.Dir = buildtestdir
stdout, err := cmd.StdoutPipe()
assertNoError(err, t, "stderr pipe")
assertNoError(err, t, "stdout pipe")
defer stdout.Close()
assertNoError(cmd.Start(), t, "start headless instance")
@ -299,7 +299,7 @@ func TestChildProcessExitWhenNoDebugInfo(t *testing.T) {
// search the running process named fix.Name
cmd := exec.Command("ps", "-aux")
stdout, err := cmd.StdoutPipe()
assertNoError(err, t, "stderr pipe")
assertNoError(err, t, "stdout pipe")
defer stdout.Close()
assertNoError(cmd.Start(), t, "start `ps -aux`")
@ -330,7 +330,7 @@ func TestRedirect(t *testing.T) {
catfixture := filepath.Join(protest.FindFixturesDir(), "cat.go")
cmd := exec.Command(dlvbin, "debug", "--headless", "--continue", "--accept-multiclient", "--listen", listenAddr, "-r", catfixture, catfixture)
stdout, err := cmd.StdoutPipe()
assertNoError(err, t, "stderr pipe")
assertNoError(err, t, "stdout pipe")
defer stdout.Close()
assertNoError(cmd.Start(), t, "start headless instance")

@ -90,10 +90,14 @@ func NewServer(config *service.Config) *ServerImpl {
// Stop stops the JSON-RPC server.
func (s *ServerImpl) Stop() error {
s.log.Debug("stopping")
close(s.stopChan)
if s.config.AcceptMulti {
s.listener.Close()
}
if s.debugger.IsRunning() {
s.debugger.Command(&api.DebuggerCommand{Name: api.Halt}, nil)
}
kill := s.config.Debugger.AttachPid == 0
return s.debugger.Detach(kill)
}

@ -1892,6 +1892,39 @@ func TestAcceptMulticlient(t *testing.T) {
<-serverDone
}
func TestForceStopWhileContinue(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("couldn't start listener: %s\n", err)
}
serverStopped := make(chan struct{})
disconnectChan := make(chan struct{})
go func() {
defer close(serverStopped)
defer listener.Close()
server := rpccommon.NewServer(&service.Config{
Listener: listener,
ProcessArgs: []string{protest.BuildFixture("http_server", protest.AllNonOptimized).Path},
AcceptMulti: true,
DisconnectChan: disconnectChan,
Debugger: debugger.Config{
Backend: "default",
},
})
if err := server.Run(); err != nil {
panic(err)
}
<-disconnectChan
server.Stop()
}()
client := rpc2.NewClient(listener.Addr().String())
client.Disconnect(true /*continue*/)
time.Sleep(10 * time.Millisecond) // give server time to start running
close(disconnectChan) // stop the server
<-serverStopped // Stop() didn't block on detach because we halted first
}
func TestClientServerFunctionCall(t *testing.T) {
protest.MustSupportFunctionCalls(t, testBackend)
withTestClient2("fncall", t, func(c service.Client) {