Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service/dap: refine teardown logic #2414

Merged
merged 12 commits into from
Apr 21, 2021
Merged

Conversation

polinasok
Copy link
Collaborator

@polinasok polinasok commented Apr 4, 2021

Updates #1515

}

func (s *Server) stopDebugSession(killProcess bool) {
_, err := s.debugger.Command(&api.DebuggerCommand{Name: api.Halt}, nil)
Copy link
Collaborator Author

@polinasok polinasok Apr 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't rpc server halt in Stop()?

func (s *ServerImpl) Stop() error {
if s.config.AcceptMulti {
close(s.stopChan)
s.listener.Close()
}
kill := s.config.Debugger.AttachPid == 0
return s.debugger.Detach(kill)
?
On a related note, the terminology collision here is unfortunate: https://github.com/go-delve/delve/blob/master/Documentation/api/ClientHowto.md#gracefully-ending-the-debug-session
From the RPC client point of view, it appears that

  • Detach = detach debugger from the process (maybe kill it) + close client connection
  • Disconnect = close client connection
    But the client how-to uses a phrase "disconnecting a running program" as a case where Detach is called. And then we have DAP where disconnect request is sent to "disconnect from the debuggee and to terminate the debug adapter". Sigh.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wild guess - isn't it because it's the rpc client who is supposed to call Halt (or Disconnect) command before the server stops. Not sure about the case where the debugging stops because client unexpectedly crashes or gets disconnected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's precisely the case I was thinking about. The client takes care of Halt before Detach. But if there is an error reading rpc messages from client, the server will go into shutdown mode and call Stop() without halting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a particular reason, halting would probably be better. And the client howto should say detach where it says ""disconnecting a running program".

@polinasok
Copy link
Collaborator Author

polinasok commented Apr 5, 2021

Test failure is unrelated:
=== RUN Test1Issue419
Start
common_test.go:18: failed assertion at integration1_test.go:1023: Continue() - thread 2700 does not exist
--- FAIL: Test1Issue419 (1.75s)

@polinasok
Copy link
Collaborator Author

cc @hyangah

service/dap/server.go Outdated Show resolved Hide resolved
}

func (s *Server) stopDebugSession(killProcess bool) {
_, err := s.debugger.Command(&api.DebuggerCommand{Name: api.Halt}, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wild guess - isn't it because it's the rpc client who is supposed to call Halt (or Disconnect) command before the server stops. Not sure about the case where the debugging stops because client unexpectedly crashes or gets disconnected.

service/dap/server.go Show resolved Hide resolved
service/dap/server.go Outdated Show resolved Hide resolved
service/dap/server.go Outdated Show resolved Hide resolved
service/dap/server.go Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@polinasok polinasok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL

service/dap/server.go Show resolved Hide resolved
service/dap/server.go Outdated Show resolved Hide resolved
service/dap/server.go Outdated Show resolved Hide resolved
service/dap/server.go Outdated Show resolved Hide resolved
@polinasok
Copy link
Collaborator Author

Failing tests are unrelated:
TestIssue419github.com/go-delve/delve/pkg/proc
TestAttachDetachgithub.com/go-delve/delve/pkg/proc

Copy link
Contributor

@hyangah hyangah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I don't know enough to review the Halt + Detach logic so still need review from delve maintainers. Other parts look good to me.

service/dap/server.go Outdated Show resolved Hide resolved
p := s.noDebugProcess
s.noDebugProcess = nil
defer s.mu.Unlock()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyangah Was this meant to be regular unlock to minimize scope (hence the temp var p var)? With my new change, the entire function gets locked (so I shouldn't need p anymore). Not ideal? But at the same time, what else is there to do when we are existing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, this is a bug - I meant s.mu.Unlock(), not defer s.mu.Unlock. :-)

Yeah, the intention was to reduce the scope - so the goroutine that's blocked onLaunchRequest (waiting for the termination of s.nodebugProcess) can proceed as soon as it's unblocked when p's killed. But you're right. We don't need to be too clever here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying. I remove the temp var "p".

Copy link
Member

@aarzilli aarzilli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@polinasok
Copy link
Collaborator Author

@suzmue
TestStepOutPreservesGoroutine is failing because it hit program termination when it expected to stop after a step.
This is not related to my comment and nodebug changes. I can't reproduce it locally - a race in the test? I think it would help to enable --log-output=dap,debugger in these tests, so failed logs have more info.

@polinasok
Copy link
Collaborator Author

@suzmue
TestStepOutPreservesGoroutine is failing because it hit program termination when it expected to stop after a step.
This is not related to my comment and nodebug changes. I can't reproduce it locally - a race in the test? I think it would help to enable --log-output=dap,debugger in these tests, so failed logs have more info.

Fix at #2417

@derekparker
Copy link
Member

Needs rebase, I'll TAL tomorrow.

@polinasok
Copy link
Collaborator Author

Needs rebase, I'll TAL tomorrow.

I am working on the merge.

FailedToAttach, "Failed to attach", err.Error())
func() {
s.mu.Lock()
defer s.mu.Unlock() // Make sure to unlock in case of panic that will become internal error
Copy link
Collaborator Author

@polinasok polinasok Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why this did not show up before the merge, but there is a test in TestBadAttachRequest with negative attach pid that resulted in a panic from debugger.New that skipped releasing the lock and then disconnect was forever waiting to acquire it. I changed onLaunchRequest in a similar way as well just in case.

@polinasok
Copy link
Collaborator Author

polinasok commented Apr 15, 2021

Unrelated TeamCity failure:
TestPackageRenames github.com/go-delve/delve/service/test

@polinasok
Copy link
Collaborator Author

@derekparker Is there more to be done to get this PR in? It would be great to get it out of the way to resolve more conflicts in subsequent PRs. Thank you.

@derekparker derekparker merged commit e141c47 into go-delve:master Apr 21, 2021
@polinasok polinasok deleted the Teardown branch May 4, 2021 22:27
suzmue pushed a commit to suzmue/delve that referenced this pull request Jun 4, 2021
* service/dap: refine teardown logic

* Address review comments + add missing lock/unlock

* Narrow lock scope

* Update comments only

* Remove redundan temp var from stopNoDebugProcess

* Clarify comment

* Set debugger to nil after detach to prevent dup teardown in Stop()

Co-authored-by: Polina Sokolova <polinasok@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants