feat(channel): add ChanClose event #40568

Problem:
Plugins using RPC sockets cannot detect when the peer closes a
`sockconnect()` channel, so reconnect logic has no reliable trigger.

Solution:
Add a `ChanClose` event with channel info before the channel is removed,
matching the existing `ChanOpen`/`ChanInfo` event model.
This commit is contained in:
Barrett Ruth
2026-07-04 14:21:25 -05:00
committed by GitHub
parent f0559997dd
commit d0a262db88
10 changed files with 63 additions and 5 deletions

View File

@@ -33,7 +33,7 @@ describe('channels', function()
source(init)
end)
pending('can connect to socket', function()
it('can connect to socket', function()
local server = n.new_session(true)
set_session(server)
local address = fn.serverlist()[1]
@@ -62,6 +62,46 @@ describe('channels', function()
eq({ 'notification', 'data', { id, { '' } } }, next_msg())
end)
it('emits ChanClose when RPC socket reaches EOF', function()
local client = get_session()
local server = n.new_session(true)
finally(function()
set_session(client)
if server then
server:close()
end
end)
set_session(server)
local address = fn.serverlist()[1]
set_session(client)
api.nvim_set_var('address', address)
exec_lua(function()
_G.closed_event = nil
vim.api.nvim_create_autocmd('ChanClose', {
callback = function()
_G.closed_event = vim.deepcopy(vim.v.event.info)
end,
})
end)
command("let g:id = sockconnect('pipe', address, {'rpc': v:true})")
local id = eval('g:id')
ok(id > 0)
eq(5, eval("rpcrequest(g:id, 'nvim_eval', '2+3')"))
server:close()
server = nil
set_session(client)
retry(nil, 3000, function()
local info = exec_lua(function()
return _G.closed_event
end)
eq(id, info.id)
eq('socket', info.stream)
eq('rpc', info.mode)
end)
end)
it('dont crash due to garbage in rpc #23781', function()
local client = get_session()
local server = n.new_session(true)