Skip to content

debug: revisit remote attach disconnect/shutdown implementation #748

@polinasok

Description

@polinasok

We must differentiate between single- and multi-client debug server when stopping the debug session and disconnecting. Our code appears to be written with the assumption that the remote server is always run with --accept-multiclient, so instead of halting and detaching, it just closes the rpc connection to the server (see here). Leaving the server running makes sense only if it can accept more client connections. Otherwise, we will have a lingering server that is unresponsive and can no longer be connected to (see #497). If the server is not multi-client (check with RPCServer.IsMulticlient), it should be killed with halt+detach on disconnect. When the server is killed, we should follow the same pattern as local launch/attach, killing the target process if it was launched with dlv debug, and letting it run if it was attached to with dlv attach.

We should fix the logging in the code to accurately log when we halt+detach and kill the server/process and when we just close the connection and leave them running. Currently we print "HaltRequest" even when we do not halt, which makes the logs quite confusing.

Optionally we could extend the above default behavior with an additional option to kill the server. This is what
dlv cli does interactively. I do not know of an interactive way to do this in vscode, but we could come up with a launch.json attribute to specify upfront that the server should be terminated on disconnect. Also filed microsoft/debug-adapter-protocol#175 to get this flag into DAP and then vscode.

(dlv) c
Ctrl-c
Would you like to [p]ause the target (returning to Delve's prompt) or [q]uit this client (leaving the target running) [p/q]? 
(dlv) p
(dlv) exit
Would you like to kill the headless instance? [Y/n] y
Would you like to kill the process? [Y/n]

Interestingly, vscode-go adapter always restarts a halted target process on disconnect while dlv cli does not. This could be another option to add to the debug configuration. Also filed microsoft/debug-adapter-protocol#177 to get this flag into DAP and then vscode.

The shutdown behavior can be explored with the following simple program

func main() {
	for i := 0; true; i++ {
		fmt.Println("================== i", i)
		time.Sleep(2 * time.Second)
	}
}

And with the following launch.json configuration comparing to dlv connect :2345:

            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "${workspaceFolder}",
            "port": 2345,
            //"trace":"verbose",
            "host": "127.0.0.1"

Then the cases are as follows:

dlv debug foo.go --headless —api-version=2 --listen=:2345 --accept-multiclient [--continue]
  • TODO: Clearly log that we are only closing connection (so users know they need to clean up the server)
  • TODO: Optionally add a flag to terminate server (which also terminates the target)
./foo.go; dlv attach <foopid> --headless —api-version=2 --listen=:2345 --accept-multiclient [--continue]
  • TODO: Clearly log that we are only closing connection (so users know they need to clean up the server)
  • TODO: Optionally add a flag to terminate server (which does not terminate the target by default)
  • TODO: See if supportTerminateDebuggee would give us a prompt to terminate the target
dlv debug foo.go --headless —api-version=2 --listen=:2345
  • TODO: Use halt+detach to terminate server (which also terminates the target)
./foo.go; dlv attach <foopid> --headless —api-version=2 --listen=:2345
  • TODO: Use halt+detach to terminate server (which does not terminate the target)
  • TODO: See if supportTerminateDebuggee would give us a prompt to terminate the target

@quoctruong

Metadata

Metadata

Assignees

Labels

DebugIssues related to the debugging functionality of the extension.FrozenDueToAge

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions