Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lua/flutter-tools/runners/debugger_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ function DebuggerRunner:send(cmd, quiet, on_response)
end
local request = command_requests[cmd]
if request ~= nil then
-- The adapter reports the end of the session only after the VM service has gone away
if request == "terminate" then vm_service.disconnect() end
dap.session():request(request, nil, function() end)
return
end
Expand All @@ -483,6 +485,7 @@ function DebuggerRunner:send(cmd, quiet, on_response)
end

function DebuggerRunner:cleanup()
vm_service.disconnect()
if dap.session() then dap.terminate() end
end

Expand Down
10 changes: 8 additions & 2 deletions lua/flutter-tools/vm_service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ local function schedule_reconnect(reason)
if on_error then on_error(reason) end
return
end
if target.attempts == 1 then
-- A drop is also how a debug session ends, before the adapter reports it: warn only once the first
-- reconnect attempt has failed
if target.attempts == 2 then
ui.notify("Lost the connection to the VM service (" .. reason .. "), reconnecting", ui.WARN)
end
local gen = generation
Expand All @@ -128,12 +130,16 @@ end
local function on_opened(gen, ws)
if gen ~= generation or not target then return ws:close() end
socket, connected = ws, true
local warned = target.attempts >= 2
target.attempts = 0
-- Listen again to the streams that were registered before the connection was lost
for stream_id in pairs(event_handlers) do
M.request("streamListen", { streamId = stream_id })
end
if target.opened then return ui.notify("Reconnected to the VM service", ui.INFO) end
if target.opened then
if warned then ui.notify("Reconnected to the VM service", ui.INFO) end
return
end
target.opened = true
if target.on_connect then target.on_connect() end
end
Expand Down
18 changes: 18 additions & 0 deletions tests/debugger_runner_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,22 @@ describe("debugger runner", function()

assert.are.same({ KEEP = "1", LOG_LEVEL = "debug", RETRIES = "3" }, launched.env)
end)

it("disconnects from the VM service before asking the adapter to quit", function()
local calls = {}
local vm_service, session = package.loaded["flutter-tools.vm_service"], dap.session
package.loaded["flutter-tools.vm_service"] = {
disconnect = function() table.insert(calls, "disconnect") end,
}
dap.session = function()
return { request = function(_, command) table.insert(calls, command) end }
end

local ok, err = pcall(runner.send, runner, "quit")
package.loaded["flutter-tools.vm_service"], dap.session = vm_service, session

assert(ok, err)

assert.are.same({ "disconnect", "terminate" }, calls)
end)
end)
50 changes: 50 additions & 0 deletions tests/vm_service_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,56 @@ describe("vm_service", function()
assert.is_false(vm_service.is_connected())
end)

describe("warnings", function()
local ui = require("flutter-tools.ui")
local notify, warnings, infos

before_each(function()
notify, warnings, infos = ui.notify, {}, {}
ui.notify = function(msg, level)
table.insert(level == ui.WARN and warnings or infos, msg)
end
end)

after_each(function() ui.notify = notify end)

local function connect()
local connected = false
vm_service.connect(uri(server), function() connected = true end)
assert.is_true(vim.wait(2000, function() return connected end))
end

it("stay quiet when the dropped connection comes back at once", function()
server = start_server()
connect()
server.connections[1].client:close()
assert.is_true(vim.wait(5000, function() return #server.connections == 2 end))
assert.is_true(vim.wait(2000, function() return vm_service.is_connected() end))
assert.are.same({}, warnings)
assert.are.same({}, infos)
end)

it("stay quiet when the session ends right after the service goes away", function()
server = start_server()
connect()
server.stop()
server = nil
vim.wait(100)
vm_service.disconnect()
vim.wait(1000)
assert.are.same({}, warnings)
end)

it("report the lost connection once the first reconnect attempt fails", function()
server = start_server()
connect()
server.stop()
server = nil
assert.is_true(vim.wait(2000, function() return #warnings > 0 end))
assert.are.equal(1, #warnings)
end)
end)

it("reports an error instead of retrying when it never managed to connect", function()
server = start_server()
local port = server.port
Expand Down
Loading