Skip to content

GDB server gets killed too early on debug stop, causing the target to halt #1122

Description

@robin96c

Describe the bug
When I stop the debug session, the target usually (not always) remains halted. It doesn't matter when I exit from a halted or running target.

To Reproduce

  • STM32H7 microcontroller
  • cortex-debug v1.12.1
  • using PyOCD - see launch.json below

Expected behavior
The target should keep running. This is the expected behavior I get when manually starting a gdb + PyOCD terminal session. If I quit gdb, PyOCD will exit automatically as well and leave the target running.

Fix
I noticed that the PyOCD gdb-server gets forcefully killed too quickly. Normally it will exit on its own gracefully, and in such a case the target continues to run as expected. But I noticed that this graceful exit can take anywhere from a couple of milliseconds to 3 seconds. By that time, cortex-debug will have called gdb-exit and/or forcefully kill the gdb process. By increasing the timeout, the issue is completely fixed for my case.

My forked commit that fixes the issue for me: 3541297

Notice that I increased the timeout by a lot (10 seconds). This is because the PyOCD server will exit on its own anyway. So it doesn't just hang for 10 seconds, the debug session stops whenever PyOCD exits. I don't know if this is the same behavior for all gdb servers though.

Related issues: same symptoms, not sure if the cause is the same

launch.json
        {
            "name": "TESTING",
            "cwd": "${workspaceFolder}",
            "executable": "./build/app.elf",
            "request": "attach",
            "breakAfterReset": false,
            "type": "cortex-debug",
            "showDevDebugOutput": "both",
            "servertype": "pyocd",
        },
Additional logs
From /tmp/cortex-debug-server-exiting.log 

[2025-04-18T12:07:58.169Z] ppid=490172 pid=490172 TESTING: ******* Starting new session request type="attach"
[2025-04-18T12:07:58.187Z] ppid=490172 pid=490182 GDB started ppid=490172 pid=490182
[2025-04-18T12:08:02.328Z] ppid=490172 pid=490182 TESTING: Begin disconnectRequest
[2025-04-18T12:08:02.685Z] ppid=490172 pid=490182 GDB: exited
[2025-04-18T12:08:02.686Z] ppid=490172 pid=490182 TESTING: quitEvent: Killing server
[2025-04-18T12:08:02.686Z] ppid=490172 pid=490182 GDBServer(490184): forcing an exit with kill()
[2025-04-18T12:08:02.695Z] ppid=490172 pid=490182 TESTING: disconnectRequest sendResponse 3
[2025-04-18T12:08:02.697Z] ppid=490172 pid=490182 TESTING: quitEvent: sending VSCode TerminatedEvent
[2025-04-18T12:08:02.701Z] ppid=490172 pid=490182 GDBServer(490184): exited code=null signal=SIGTERM

[2025-04-18T12:03:27.861Z] ppid=479881 pid=479881 TESTING: ******* Starting new session request type="attach"
[2025-04-18T12:03:27.892Z] ppid=479881 pid=479891 GDB started ppid=479881 pid=479891
[2025-04-18T12:03:33.545Z] ppid=479881 pid=479891 TESTING: Begin disconnectRequest
[2025-04-18T12:03:34.263Z] ppid=479881 pid=479891 GDB Kill timer expired for a disconnect+exit, so forcing a kill
[2025-04-18T12:03:34.264Z] ppid=479881 pid=479891 GDB kill()
[2025-04-18T12:03:34.264Z] ppid=479881 pid=479891 GDB: exited
[2025-04-18T12:03:34.264Z] ppid=479881 pid=479891 TESTING: quitEvent: Killing server
[2025-04-18T12:03:34.264Z] ppid=479881 pid=479891 GDBServer(479893): forcing an exit with kill()
[2025-04-18T12:03:34.270Z] ppid=479881 pid=479891 TESTING: disconnectRequest sendResponse 3
[2025-04-18T12:03:34.275Z] ppid=479881 pid=479891 TESTING: quitEvent: sending VSCode TerminatedEvent
[2025-04-18T12:03:34.278Z] ppid=479881 pid=479891 GDBServer(479893): exited code=null signal=SIGTERM

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions