Skip to content

fix: improve server process shutdown with SIGKILL fallback#239

Merged
sudo-tee merged 1 commit intomainfrom
fix/server-process-kill
Feb 6, 2026
Merged

fix: improve server process shutdown with SIGKILL fallback#239
sudo-tee merged 1 commit intomainfrom
fix/server-process-kill

Conversation

@sudo-tee
Copy link
Owner

@sudo-tee sudo-tee commented Feb 5, 2026

this should fix #237

@sudo-tee sudo-tee requested a review from Copilot February 5, 2026 14:47
@sudo-tee sudo-tee changed the title fix: improve server process shutdown with SIGKILL fallback and proper… fix: improve server process shutdown with SIGKILL fallback Feb 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sudo-tee sudo-tee force-pushed the fix/server-process-kill branch from 6cf95ca to 1f101e6 Compare February 5, 2026 18:26
@sudo-tee sudo-tee merged commit bc5149e into main Feb 6, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Opencode process doesn't terminate after exiting Neovim

1 participant