Skip to content

fix(file_refresh): guard against invalid buffer in TermClose autocmd#132

Open
thsid wants to merge 1 commit into
greggh:mainfrom
thsid:fix/file-refresh-invalid-buffer-on-termclose
Open

fix(file_refresh): guard against invalid buffer in TermClose autocmd#132
thsid wants to merge 1 commit into
greggh:mainfrom
thsid:fix/file-refresh-invalid-buffer-on-termclose

Conversation

@thsid

@thsid thsid commented Jun 14, 2026

Copy link
Copy Markdown

Problem

When exiting Neovim (or closing a terminal buffer), a TermClose autocmd in
file_refresh.lua calls nvim_buf_get_name(args.buf) at line 104. By the
time the event fires the buffer may already have been deleted, causing:

Error in TermClose Autocommands for "*":
Lua callback: ...claude-code.nvim/lua/claude-code/file_refresh.lua:104: Invalid buffer id: 2
stack traceback:
        [C]: in function 'nvim_buf_get_name'
        ...claude-code.nvim/lua/claude-code/file_refresh.lua:104: in function <...>

Fix

Add a nvim_buf_is_valid guard before the name lookup so the callback exits
early when the buffer is no longer valid:

if not vim.api.nvim_buf_is_valid(args.buf) then return end

This is the same defensive pattern applied to terminal.lua in #106 /
commit 1952ba2.

Testing

Reproduced by opening Claude Code inside Neovim, closing the terminal split,
then quitting Neovim — the error no longer appears after this change.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed terminal buffer cleanup logic to properly validate buffer state before performing cleanup operations, preventing potential errors when closing terminals.

nvim_buf_get_name raises an error when called with a buffer id that has
already been deleted by the time TermClose fires. Add an nvim_buf_is_valid
check before the name lookup so Neovim exits cleanly without showing the
'Invalid buffer id' Lua callback error.

Fixes the error:
  Lua callback: ...file_refresh.lua:104: Invalid buffer id: N
  stack traceback: [C]: in function 'nvim_buf_get_name'
@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a1ea3ac8-cd8c-45c1-bec5-22321ffd58dc

📥 Commits

Reviewing files that changed from the base of the PR and between 55c0cb5 and 497f430.

📒 Files selected for processing (1)
  • lua/claude-code/file_refresh.lua

📝 Walkthrough

Walkthrough

A single guard line is added to the TermClose autocommand callback in file_refresh.lua. Before executing any cleanup logic, the callback now calls vim.api.nvim_buf_is_valid(args.buf) and returns early if the buffer is no longer valid.

Changes

TermClose Buffer Validity Guard

Layer / File(s) Summary
Early return for invalid buffer in TermClose
lua/claude-code/file_refresh.lua
TermClose callback now calls vim.api.nvim_buf_is_valid(args.buf) and returns early, preventing updatetime restore, instance cleanup, and window close/delete from running on an invalid buffer.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Possibly related issues

Possibly related PRs

  • greggh/claude-code.nvim#106: Refactored the same TermClose autocommand in file_refresh.lua to use args.buf for cleanup; this PR adds a validity guard on top of that same code path.

Poem

🐇 A buffer may vanish, gone without a trace,
But now we check first before we give chase.
nvim_buf_is_valid — a tiny little test,
Keeps crashes at bay and lets Neovim rest.
One line of caution, a guard standing tall,
No invalid buffer shall cause us to fall! 🌿

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a buffer validity check to guard against invalid buffers in the TermClose autocmd.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant