mfussenegger / nvim-dap

Debug Adapter Protocol client implementation for Neovim
GNU General Public License v3.0
5.09k stars 179 forks source link

`disconnect` command listener not working with debugpy and long-running programs #1264

Open abhinavnatarajan opened 2 weeks ago

abhinavnatarajan commented 2 weeks ago

Debug adapter definition and debug configuration

Using the following minimal configuration for nvim-dap with the debugpy adapter, I cannot get the disconnect listener to work correctly.

return {
    "mfussenegger/nvim-dap",
    keys = {
        { "<Leader>Dt", function() require("dap").toggle_breakpoint() end, desc = "Toggle Breakpoint" },
        { "<Leader>Ds", function() require("dap").continue() end, desc = "Start" },
        {
            "<Leader>Dd",
            function()
                require("dap").disconnect({ terminateDebuggee = false })
                require("dap").close()
            end,
            desc = "Disconnect debugger"
        },
    },
    config = function(_, opts)
        local dap = require("dap")

        dap.listeners.before.disconnect.cb =
                function()
                    vim.notify("disconnected")
                end
        dap.listeners.before.event_terminated.cb = 
                function()
                    vim.notify("terminated")
                end
        dap.listeners.before.event_exited.cb = 
                function()
                    vim.notify("exited")
                end
        dap.adapters.python = {
                    type = 'executable',
                    command = vim.fn.exepath('debugpy-adapter'), -- debugpy installed via Mason
                    options = {
                        source_filetype = 'python',
                    },
                },
        dap.configurations.python = {
            {
                type = 'python',
                request = 'launch',
                name = "Launch file",
                program = "${file}",
                python = '/usr/bin/python3'
            },
        }
    end
}

Debug adapter version

debugpy v1.8.1

Steps to Reproduce

Add a breakpoint on line 2 of the following file, and start the debugger.

import time
time.sleep(5) # set a breakpoint on this line
print('Nothing in particular')

When the debugger stops at the breakpoint, hit <leader>Dd to disconnect.

Expected Result

I should get a notification with the message disconnected.

Actual Result

No notification is raised, but I get [Process exited 0] in the DAP terminal. The behaviour also depends on the sleep time. If I change the second line in the python file to time.sleep(0.1), the line Nothing in particular is printed to the DAP terminal before the process exit. I tried increasing the value of disconnect_timeout_sec in the adapter options, but this did not change the behaviour.