Skip to content

Commit 6ca0e9c

Browse files
committed
fix(server): improve shutdown sequence to properly terminate child processes
- Refactors shutdown to send SIGTERM to child processes before SIGKILL - Adds detailed debug logging for process termination - Ensures resources are cleaned up consistently
1 parent c75913f commit 6ca0e9c

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

lua/opencode/opencode_server.lua

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,41 @@ function OpencodeServer:is_running()
5050
return self.job and self.job.pid ~= nil
5151
end
5252

53+
local function kill_process(pid, signal, desc)
54+
local log = require('opencode.log')
55+
local ok, err = pcall(vim.uv.kill, pid, signal)
56+
log.debug('shutdown: %s pid=%d sig=%d ok=%s err=%s', desc, pid, signal, tostring(ok), tostring(err))
57+
return ok, err
58+
end
59+
5360
function OpencodeServer:shutdown()
5461
local log = require('opencode.log')
5562
if self.shutdown_promise:is_resolved() then
5663
return self.shutdown_promise
5764
end
5865

5966
if self.job and self.job.pid then
67+
---@cast self.job vim.SystemObj
6068
local pid = self.job.pid
69+
local children = vim.api.nvim_get_proc_children(pid)
6170

62-
self.job = nil
63-
self.url = nil
64-
self.handle = nil
71+
if #children > 0 then
72+
log.debug('shutdown: process pid=%d has %d children (%s)', pid, #children, vim.inspect(children))
6573

66-
local ok_term, err_term = pcall(vim.uv.kill, pid, 15)
67-
log.debug('shutdown: SIGTERM pid=%d ok=%s err=%s', pid, tostring(ok_term), tostring(err_term))
74+
for _, cid in ipairs(children) do
75+
kill_process(cid, 15, 'SIGTERM child')
76+
end
77+
end
6878

69-
local ok_kill, err_kill = pcall(vim.uv.kill, pid, 9)
70-
log.debug('shutdown: SIGKILL pid=%d ok=%s err=%s', pid, tostring(ok_kill), tostring(err_kill))
79+
kill_process(pid, 15, 'SIGTERM')
80+
kill_process(pid, 9, 'SIGKILL')
7181
else
7282
log.debug('shutdown: no job running')
7383
end
7484

85+
self.job = nil
86+
self.url = nil
87+
self.handle = nil
7588
self.shutdown_promise:resolve(true)
7689
return self.shutdown_promise
7790
end

0 commit comments

Comments
 (0)