Skip to content

Commit

Permalink
fix(lsp): buffer messages until connected to server (#28507)
Browse files Browse the repository at this point in the history
`handle:write(msg)` can fail if the socket is not yet connected to the
server.

Should address #28398 (comment)
  • Loading branch information
mfussenegger committed Apr 26, 2024
1 parent a736e84 commit 47dbda9
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion runtime/lua/vim/lsp/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,23 @@ function M.connect(host_or_path, port)
or assert(uv.new_tcp(), 'Could not create new TCP socket')
)
local closing = false
-- Connect returns a PublicClient synchronously so the caller
-- can immediately send messages before the connection is established
-- -> Need to buffer them until that happens
local connected = false
-- size should be enough because the client can't really do anything until initialization is done
-- which required a response from the server - implying the connection got established
local msgbuf = vim.ringbuf(10)
local transport = {
write = function(msg)
handle:write(msg)
if connected then
local _, err = handle:write(msg)
if err and not closing then
log.error('Error on handle:write: %q', err)
end
else
msgbuf:push(msg)
end
end,
is_closing = function()
return closing
Expand Down Expand Up @@ -679,6 +693,10 @@ function M.connect(host_or_path, port)
handle:read_start(M.create_read_loop(handle_body, transport.terminate, function(read_err)
client:on_error(M.client_errors.READ_ERROR, read_err)
end))
connected = true
for msg in msgbuf do
handle:write(msg)
end
end
if port == nil then
handle:connect(host_or_path, on_connect)
Expand Down

0 comments on commit 47dbda9

Please sign in to comment.