aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2020-12-15 10:15:51 +0100
committerBjörn Linse <bjorn.linse@gmail.com>2020-12-23 23:58:56 +0100
commit95352f490a24f5e3a1cc788d297105e4eadf1481 (patch)
treee1df3123527756ee6f67f80b81ce333618126bf5
parentd0668b36a3e2d0683059baead45bea27e2358e9c (diff)
downloadrneovim-95352f490a24f5e3a1cc788d297105e4eadf1481.tar.gz
rneovim-95352f490a24f5e3a1cc788d297105e4eadf1481.tar.bz2
rneovim-95352f490a24f5e3a1cc788d297105e4eadf1481.zip
rpc: don't handle stale requests on already closed channel
-rw-r--r--src/nvim/msgpack_rpc/channel.c6
-rw-r--r--test/functional/api/server_notifications_spec.lua16
2 files changed, 22 insertions, 0 deletions
diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c
index 68ef4cd41e..a0b439ac45 100644
--- a/src/nvim/msgpack_rpc/channel.c
+++ b/src/nvim/msgpack_rpc/channel.c
@@ -377,6 +377,10 @@ static void request_event(void **argv)
Channel *channel = e->channel;
MsgpackRpcRequestHandler handler = e->handler;
Error error = ERROR_INIT;
+ if (channel->rpc.closed) {
+ // channel was closed, abort any pending requests
+ goto free_ret;
+ }
Object result = handler.fn(channel->id, e->args, &error);
if (e->type == kMessageTypeRequest || ERROR_SET(&error)) {
// Send the response.
@@ -391,6 +395,8 @@ static void request_event(void **argv)
} else {
api_free_object(result);
}
+
+free_ret:
api_free_array(e->args);
channel_decref(channel);
xfree(e);
diff --git a/test/functional/api/server_notifications_spec.lua b/test/functional/api/server_notifications_spec.lua
index 29cd38ef0d..9ee2570798 100644
--- a/test/functional/api/server_notifications_spec.lua
+++ b/test/functional/api/server_notifications_spec.lua
@@ -3,6 +3,8 @@ local eq, clear, eval, command, nvim, next_msg =
helpers.eq, helpers.clear, helpers.eval, helpers.command, helpers.nvim,
helpers.next_msg
local meths = helpers.meths
+local exec_lua = helpers.exec_lua
+local retry = helpers.retry
describe('notify', function()
local channel
@@ -72,4 +74,18 @@ describe('notify', function()
nvim('unsubscribe', 'event1')
eq(2, eval('1+1')) -- Still alive?
end)
+
+ it('cancels stale events on channel close', function()
+ if helpers.pending_win32(pending) then return end
+ local catchan = eval("jobstart(['cat'], {'rpc': v:true})")
+ eq({id=catchan, stream='job', mode='rpc', client = {}}, exec_lua ([[
+ vim.rpcnotify(..., "nvim_call_function", 'chanclose', {..., 'rpc'})
+ vim.rpcnotify(..., "nvim_subscribe", "daily_rant")
+ return vim.api.nvim_get_chan_info(...)
+ ]], catchan))
+ eq(2, eval('1+1')) -- Still alive?
+ eq({false, 'Invalid channel: '..catchan},
+ exec_lua ([[ return {pcall(vim.rpcrequest, ..., 'nvim_eval', '1+1')}]], catchan))
+ retry(nil, 3000, function() eq({}, meths.get_chan_info(catchan)) end) -- cat be dead :(
+ end)
end)