Skip to content

fix(loop): schedule debounced callbacks and check closed status#437

Merged
tanvirtin merged 1 commit intotanvirtin:mainfrom
cubatic45:fix/E5560
Mar 4, 2026
Merged

fix(loop): schedule debounced callbacks and check closed status#437
tanvirtin merged 1 commit intotanvirtin:mainfrom
cubatic45:fix/E5560

Conversation

@cubatic45
Copy link
Contributor

Environment

nvim –version
NVIM v0.11.6
Build type: Release
LuaJIT 2.1.1772148810

Description

When opening the commit UI, the following error occurs:

Error executing callback:
…/plenary.nvim/lua/plenary/async/async.lua:18:
The coroutine failed with this message:
vim/_editor.lua:0: E5560: nvim_exec2 must not be called in a fast event context

stack traceback:
[C]: in function ‘error’
…/plenary/async/async.lua:18: in function ‘callback_or_next’
…/plenary/async/async.lua:45: in function ‘step’
…/plenary/async/async.lua:48: in function ‘execute’
…/plenary/async/async.lua:118: in function ‘fn’
…/vgit.nvim/lua/vgit/core/loop.lua:34: in function <…>

The error is reproducible consistently on NVIM v0.11.6.

Analysis

The issue appears to be caused by calling vim.cmd() (which internally uses nvim_exec2) inside a libuv timer callback.

In loop.debounce:

function loop.debounce(fn, ms)
  local timer = vim.loop.new_timer()
  local closed = false

  local debounced = function(...)
    if closed then return end
    local argv = { ... }
    local argc = select('#', ...)

    timer:stop()
    timer:start(ms, 0, function()
      fn(unpack(argv, 1, argc))  -- here
    end)
  end

  -- Store cleanup function in registry keyed by the debounced function
  debounced_registry[debounced] = function()
    if not closed and timer and not timer:is_closing() then
      timer:stop()
      timer:close()
      closed = true
      timer = nil
    end
  end

  return debounced
end

Since vim.loop.new_timer() runs in a fast event context, executing editor-modifying APIs inside fn() can trigger:

E5560: nvim_exec2 must not be called in a fast event context

This behavior appears stricter in Neovim >= 0.9 and is reproducible in 0.11.6.

@tanvirtin tanvirtin merged commit 7e147e8 into tanvirtin:main Mar 4, 2026
1 of 2 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.

2 participants