Skip to content

Long wait when exiting neovim #254

Closed

Description

@pirey

After I open opencode buffer, every time I want to exit I have to wait for quite some time.

I took a peek at the opencode server shutdown function and noticed that the shutdown_promise is only resolved when there is no job, thus the callback in VimLeavePre will wait for entire 2000ms before exiting.

I try to resolve the promise after killing the job in the shutdown function and wait time is slightly reduced, although still noticeable.

    vim.defer_fn(function()
      if job and job.pid then
        pcall(function()
          job:kill(9) -- SIGKILL
        end)
        self.shutdown_promise:resolve(true) -- added here
      end
    end, 500)

some demo for comparison of before and after I applied the promise resolve.

Screen.Recording.2026-02-09.at.06.39.18.mov

Activity

  1. sudo-tee commented on Feb 9, 2026

    @sudo-tee
    Owner

    Seems related to #251

    The shutdown sequence is problematic on OSX

  2. added a commit that references this issue on Feb 9, 2026
    d864fb8
  3. sudo-tee commented on Feb 9, 2026

    @sudo-tee
    Owner

    @pirey

    I played with the shutdown of the server in a PR, please let me know if it resolves the issue for you. As I was not experiencing this issue

    #255

  4. pirey commented on Feb 10, 2026

    @pirey
    ContributorAuthor

    Hey thanks.

    I tried checking out the branch and found that the wait time is reduced from 2000ms to around 700ms-ish.

    I'm using this to do simple time measurement

      vim.api.nvim_create_autocmd("VimLeavePre", {
        callback = function()
          vim.g._start_time = vim.loop.hrtime()
        end,
      })
    
      vim.api.nvim_create_autocmd("VimLeave", {
        callback = function()
          local duration = (vim.loop.hrtime() - (vim.g._start_time or 0)) / 1e6
          local msg = ("Exit duration: %.2fms\n"):format(duration)
          vim.fn.writefile({ msg }, vim.fn.stdpath("config") .. "/exit_time.log", "a")
        end,
      })

    and this is the comparison before and after i checkout the branch

    Exit duration: 2144.10ms
    Exit duration: 765.21ms
    

    So I guess the fix is kinda working, but having to wait over 500ms is still noticeable and I wonder if we can reduce it further.

  5. sudo-tee commented on Feb 10, 2026

    @sudo-tee
    Owner

    Thanks for confirming.

    I will still work on it for a bit

  6. added a commit that references this issue on Feb 10, 2026
    89373f5
  7. sudo-tee commented on Feb 10, 2026

    @sudo-tee
    Owner

    @pirey

    I simplified the shutdown logic to the minimum and added some logging

    you can enable it in your config

    {
       logging {
          enabled = true,
          level = 'debug'
       }
    }

    Could you do me a favor and enable the log even if it works for you so I can see if there is an error in one of the SIGTERM or SIGKILL ?

    file path should be ~/.local/state/nvim/opencode.log

    Thank you very much

  8. added a commit that references this issue on Feb 10, 2026
    0c01952
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions