fix: improve server process shutdown with SIGKILL fallback#239
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the server shutdown process by adding a SIGKILL fallback mechanism and wrapping the VimLeave shutdown call in pcall for better error handling.
Changes:
- Added SIGKILL fallback that triggers 1 second after SIGTERM if the process hasn't exited
- Changed signal format from string literals to vim.loop.constants
- Wrapped VimLeavePre shutdown in pcall to prevent errors from blocking Vim exit
- Modified shutdown logic to rely on exit callback for promise resolution and field cleanup
- Updated test to spawn server properly and simulate process exit via callback
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| lua/opencode/opencode_server.lua | Implements SIGKILL fallback after 1s timeout, changes promise resolution timing, adds pcall protection |
| tests/unit/opencode_server_spec.lua | Updates test to properly mock vim.system, spawn server, and simulate exit callback |
Comments suppressed due to low confidence (1)
tests/unit/opencode_server_spec.lua:86
- The test doesn't validate the SIGKILL fallback behavior that was added in the implementation. The test only simulates the process exiting immediately after SIGTERM is sent (line 75), but doesn't test the case where SIGTERM fails and SIGKILL needs to be sent after 1 second. This leaves the new SIGKILL fallback logic untested. Consider adding a test case that verifies SIGKILL is sent when the process doesn't exit after SIGTERM.
it('shutdown resolves shutdown_promise and clears fields', function()
local server = OpencodeServer.new()
local exit_callback
-- Mock vim.system to capture the exit callback
vim.system = function(cmd, opts, on_exit)
exit_callback = on_exit
return { pid = 2, kill = function() end }
end
-- Spawn the server so the exit callback is set up
server:spawn({
cwd = '.',
on_ready = function() end,
on_error = function() end,
on_exit = function() end,
})
local resolved = false
server:get_shutdown_promise():and_then(function()
resolved = true
end)
-- Call shutdown (sends SIGTERM)
server:shutdown()
-- Simulate the process exiting by calling the exit callback
vim.schedule(function()
exit_callback({ code = 0, signal = 0 })
end)
vim.wait(100, function()
return resolved
end)
assert.is_true(resolved)
assert.is_nil(server.job)
assert.is_nil(server.url)
assert.is_nil(server.handle)
end)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6cf95ca to
1f101e6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
this should fix #237