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
9 changes: 7 additions & 2 deletions lua/dap/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,20 @@ local function parse_chunk_loop()
end


function M.create_read_loop(handle_body)
function M.create_read_loop(handle_body, on_no_chunk)
local parse_chunk = coroutine.wrap(parse_chunk_loop)
parse_chunk()
return function (err, chunk)
if err then
utils.notify(err, vim.log.levels.ERROR)
vim.schedule(function()
utils.notify(err, vim.log.levels.ERROR)
end)
return
end
if not chunk then
if on_no_chunk then
on_no_chunk()
end
return
end
while true do
Expand Down
15 changes: 13 additions & 2 deletions lua/dap/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,20 @@ function Session:connect(adapter, opts, on_connect)
local address = addresses[1]
client:connect(address.addr, tonumber(adapter.port), function(conn_err)
if not conn_err then
client:read_start(rpc.create_read_loop(vim.schedule_wrap(function(body)
local handle_body = vim.schedule_wrap(function(body)
session:handle_body(body)
end)))
end)
client:read_start(rpc.create_read_loop(handle_body, function()
client:shutdown()
client:close()
local s = dap().session()
if s == session then
vim.schedule(function()
utils.notify('Debug adapter disconnected', vim.log.levels.INFO)
end)
dap().set_session(nil)
end
end))
end
on_connect(conn_err)
end)
Expand Down
10 changes: 10 additions & 0 deletions tests/integration_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ describe('dap with fake server', function()
reason = 'unknown',
})
end)

it('resets session if connection disconnects without terminat event', function()
local session = run_and_wait_until_initialized(config)
assert.are_not.same(nil, dap.session())
assert.are.same(session, dap.session())
server.stop()
vim.wait(1000, function() return dap.session() == nil end, 100)
assert.are.same(nil, server.client.socket)
assert.are.same(nil, dap.session())
end)
end)
9 changes: 7 additions & 2 deletions tests/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ function Client:send_response(request, body)
body = body,
}
table.insert(self.spy.responses, payload)
self.socket:write(rpc.msg_with_content_length(json_encode(payload)))
if self.socket then
self.socket:write(rpc.msg_with_content_length(json_encode(payload)))
end
end


Expand Down Expand Up @@ -98,7 +100,10 @@ function M.spawn()
spy = spy,
stop = function()
if client.socket then
client.socket:close()
client.socket:shutdown(function()
client.socket:close()
client.socket = nil
end)
end
end,
}
Expand Down