fix(lsp): wait for exit_timeout on quit #37597

Problem:
When quitting Nvim, LSP servers will not be force-stopped, even if
ClientConfig.exit_timeout is set to an integer.

    pkill emmylua_ls; VIMRUNTIME=runtime/ nvim --clean -u repro.lua repro.lua ; waitpid $(pgrep emmylua_ls)

    vim.lsp.config('foo', { cmd = { 'emmylua_ls' }, exit_timeout = 1000 })
    vim.lsp.enable('foo')
    vim.defer_fn(vim.cmd.quit, 500)

Solution:
On VimExit, wait up to `exit_timeout:integer` milliseconds for servers
to exit. Do this with an explicit `vim.wait()`, because the actual
force-stop is deferred, and won't be attempted if Nvim exits before
then.

Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
This commit is contained in:
phanium
2026-02-15 20:33:49 +08:00
committed by GitHub
parent 3eb8a2784e
commit 5c4b64aae5

View File

@@ -1132,9 +1132,25 @@ api.nvim_create_autocmd('VimLeavePre', {
local active_clients = lsp.get_clients()
log.info('exit_handler', active_clients)
local max_timeout = 0
for _, client in pairs(active_clients) do
max_timeout = math.max(max_timeout, tonumber(client.exit_timeout) or 0)
client:stop(client.exit_timeout)
end
if max_timeout > 10 then
api.nvim_echo({
{
string.format('Waiting %ss for lsp exit (Press Ctrl-C to force exit)', max_timeout / 1e3),
'WarningMsg',
},
}, true, {})
end
vim.wait(max_timeout, function()
return vim.iter(active_clients):all(function(client)
return client.rpc.is_closing()
end)
end)
end,
})