From 6a85f349664a6765aef2d14d470b5f196ebebea1 Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Wed, 21 Apr 2021 22:39:19 +0200 Subject: [PATCH] debugger: report error when switching goroutine is impossible (#2424) Due to variable shadowing the SwitchGoroutine command never failed. --- service/dap/server.go | 8 +++++--- service/dap/server_test.go | 7 +++++-- service/debugger/debugger.go | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/service/dap/server.go b/service/dap/server.go index a8575bf6..d644ef5f 100644 --- a/service/dap/server.go +++ b/service/dap/server.go @@ -1023,14 +1023,16 @@ func stoppedGoroutineID(state *api.DebuggerState) (id int) { func (s *Server) doStepCommand(command string, threadId int) { // Use SwitchGoroutine to change the current goroutine. - state, err := s.debugger.Command(&api.DebuggerCommand{Name: api.SwitchGoroutine, GoroutineID: threadId}, nil) + _, err := s.debugger.Command(&api.DebuggerCommand{Name: api.SwitchGoroutine, GoroutineID: threadId}, nil) if err != nil { - s.log.Errorf("Error switching goroutines while stepping: %e", err) + s.log.Errorf("Error switching goroutines while stepping: %v", err) // If we encounter an error, we will have to send a stopped event // since we already sent the step response. stopped := &dap.StoppedEvent{Event: *newEvent("stopped")} stopped.Body.AllThreadsStopped = true - if state != nil { + if state, err := s.debugger.State(false); err != nil { + s.log.Errorf("Error retrieving state: %e", err) + } else { stopped.Body.ThreadId = stoppedGoroutineID(state) } stopped.Body.Reason = "error" diff --git a/service/dap/server_test.go b/service/dap/server_test.go index eaea3b45..67881d6a 100644 --- a/service/dap/server_test.go +++ b/service/dap/server_test.go @@ -2202,9 +2202,12 @@ func TestNextAndStep(t *testing.T) { client.ExpectStepInResponse(t) expectStop("main.inlineThis", 5) - client.NextRequest(-10000 /*this is ignored*/) + client.NextRequest(-1000) client.ExpectNextResponse(t) - expectStop("main.inlineThis", 6) + if se := client.ExpectStoppedEvent(t); se.Body.Reason != "error" || se.Body.Text != "unknown goroutine -1000" { + t.Errorf("got %#v, want Reaspon=\"error\", Text=\"unknown goroutine -1000\"", se) + } + handleStop(t, client, 1, "main.inlineThis", 5) }, disconnect: false, }}) diff --git a/service/debugger/debugger.go b/service/debugger/debugger.go index 6504bc02..850419a9 100644 --- a/service/debugger/debugger.go +++ b/service/debugger/debugger.go @@ -1095,7 +1095,8 @@ func (d *Debugger) Command(command *api.DebuggerCommand, resumeNotify chan struc withBreakpointInfo = false case api.SwitchGoroutine: d.log.Debugf("switching to goroutine %d", command.GoroutineID) - g, err := proc.FindGoroutine(d.target, command.GoroutineID) + var g *proc.G + g, err = proc.FindGoroutine(d.target, command.GoroutineID) if err == nil { err = d.target.SwitchGoroutine(g) }