claude-code.nvim icon indicating copy to clipboard operation
claude-code.nvim copied to clipboard

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

Open thsid opened this issue 5 months ago β€’ 1 comments

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.

thsid avatar Jun 14 '26 06:06 thsid

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 55c0cb59828fbc3bec744288286a46f5d5750b83 and 497f4302e9c500ada734923776d7b95d2e915009.

πŸ“’ 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

  • greggh/claude-code.nvim#124: This PR directly implements the vim.api.nvim_buf_is_valid(args.buf) check identified as the fix for the "Invalid buffer id" crash occurring when closing terminals with Ctrl+D.

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.

coderabbitai[bot] avatar Jun 14 '26 06:06 coderabbitai[bot]