Skip to content

fix: guard against deleted buffer in vim.schedule callbacks#514

Open
qufeiyan wants to merge 1 commit intostevearc:masterfrom
qufeiyan:fix/invalid-buffer-schedule-callback
Open

fix: guard against deleted buffer in vim.schedule callbacks#514
qufeiyan wants to merge 1 commit intostevearc:masterfrom
qufeiyan:fix/invalid-buffer-schedule-callback

Conversation

@qufeiyan
Copy link
Copy Markdown

@qufeiyan qufeiyan commented May 6, 2026

Problem

When using the system strategy, this error occurs intermittently:

vim.schedule callback: ...overseer.nvim/lua/overseer/strategy/system.lua:115:
  Invalid buffer id: 70
...

Root Cause

The on_output callback is wrapped with vim.schedule_wrap, which defers execution. Between scheduling and execution, the buffer may have been deleted by reset() or dispose() — a TOCTOU race condition. Same issue in on_exit's vim.schedule callback.

Fix

Add nvim_buf_is_valid guards at the top of both deferred callbacks, returning early if the buffer no longer exists.

Changes

  • lua/overseer/strategy/system.lua: two guard clauses (+6 lines)

Closes #513

🤖 Generated with Claude Code

In `system.lua`, the `on_output` callback is wrapped with `vim.schedule_wrap`
and the `on_exit` handler uses `vim.schedule`. Both can fire after the buffer
has been deleted by `reset()` or `dispose()`, causing:

  Invalid buffer id: <N>
  stack traceback:
    ...overseer.nvim/lua/overseer/strategy/system.lua:115:
      in function 'nvim_buf_line_count'

Add `nvim_buf_is_valid` guards before any buffer operations in deferred
callbacks.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@github-actions github-actions Bot requested a review from stevearc May 6, 2026 10:17
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.

system strategy: "Invalid buffer id" error from vim.schedule callback

1 participant