From a44521f46e6f79171d034e5cce1a4dc266d23e49 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sun, 3 Sep 2023 10:17:24 +0100 Subject: [PATCH] fix(vim.system): let on_exit handle cleanup after kill Fixes #25000 --- runtime/lua/vim/_system.lua | 11 +++++------ test/functional/lua/system_spec.lua | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/runtime/lua/vim/_system.lua b/runtime/lua/vim/_system.lua index 6f5e95eb24..ba3fc34e78 100644 --- a/runtime/lua/vim/_system.lua +++ b/runtime/lua/vim/_system.lua @@ -18,6 +18,7 @@ local uv = vim.uv --- @field stderr? string --- @class SystemState +--- @field cmd string[] --- @field handle? uv.uv_process_t --- @field timer? uv.uv_timer_t --- @field pid? integer @@ -63,11 +64,9 @@ local function new_systemobj(state) }, { __index = SystemObj }) end ---- @param signal integer +--- @param signal integer|string function SystemObj:kill(signal) - local state = self._state - state.handle:kill(signal) - close_handles(state) + self._state.handle:kill(signal) end local MAX_TIMEOUT = 2 ^ 31 @@ -159,7 +158,7 @@ end --- @return table local function base_env() - local env = vim.fn.environ() + local env = vim.fn.environ() --- @type table env['NVIM'] = vim.v.servername env['NVIM_LISTEN_ADDRESS'] = nil return env @@ -212,7 +211,7 @@ end local M = {} --- @param cmd string ---- @param opts uv.aliases.spawn_options +--- @param opts uv.spawn.options --- @param on_exit fun(code: integer, signal: integer) --- @param on_error fun() --- @return uv.uv_process_t, integer diff --git a/test/functional/lua/system_spec.lua b/test/functional/lua/system_spec.lua index 836d3a83b0..35b9d5cc37 100644 --- a/test/functional/lua/system_spec.lua +++ b/test/functional/lua/system_spec.lua @@ -54,4 +54,28 @@ describe('vim.system', function() end) end + it('kill processes', function() + exec_lua([[ + local signal + local cmd = vim.system({ 'cat', '-' }, { stdin = true }, function(r) + signal = r.signal + end) -- run forever + + cmd:kill('sigint') + + -- wait for the process not to exist + local done = vim.wait(2000, function() + return signal ~= nil + end) + + assert(done, 'process did not exit') + + -- Check the process is no longer running + vim.fn.systemlist({'ps', 'p', tostring(cmd.pid)}) + assert(vim.v.shell_error == 1, 'dwqdqd '..vim.v.shell_error) + + assert(signal == 2) + ]]) + end) + end)