What OS signal does the vscode-go debugger send to delve-dap when I terminate debugging
Asked Answered
N

1

6

I want to capture the OS signal and do some exit jobs when I terminate the vscode golang debugger.

I have code as below:

sigalChan := make(chan os.Signal, 1)
signal.Notify(sigalChan, syscall.SIGINT, syscall.SIGTERM)
<-sigalChan
doSomeJobs()

but it doesn't work. Anyone can tell me how to figure it out? Maybe the signal type is not SIGINT or SIGTERM?

Nippur answered 24/3, 2022 at 12:39 Comment(6)
Consume the <-signalChan value inside a goroutine and it should work.Gandhi
Actually I do run that code inside a goroutine but it doesn't work when I debug in vscsode. However when I run my program by "go run main.go" and exit by Ctrl+C, it works. I am confused.Nippur
What do you mean by "debug in vscsode"?Gandhi
in vscode, press "f5" to debugNippur
If the debugger is sent a signal you're not going to receive it in your program. The debugger however is probably not even sent a signal, just an exit command so it can shut itself down.Refined
I think so. It means that I can not do exit jobs when I am debugging.Nippur
N
14

I find out a solution. Just set "console" to "integratedTerminal" in launch.json to make delve server foreground, then I can use "ctrl+c" to terminate the debugging process, and signal can be received in my program.

{
    // launch.json
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/main.go",
            "console": "integratedTerminal"
        }
    ]
}
Nippur answered 26/3, 2022 at 10:4 Comment(1)
It helps somewhat, but the debug task is now in "Runtime Error" and needs to be stopped and restarted anyways.Colunga

© 2022 - 2024 — McMap. All rights reserved.