Skip to content

TermClose callback crashes with 'Invalid buffer id' when closing terminal with Ctrl+D #124

@daniel-san-pedro

Description

@daniel-san-pedro

Description

When closing a terminal pane with Ctrl+D (sending EOF to the shell), the TermClose autocommand in file_refresh.lua crashes because the buffer is already invalid by the time the callback fires.

Error

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

Steps to Reproduce

  1. Open Neovim
  2. Open any terminal in a split (:sp | te or :vs | te)
  3. Press Ctrl+D to close the terminal shell

Suggested Fix

Add a buffer validity check before calling nvim_buf_get_name in the TermClose callback:

callback = function(args)
  if not vim.api.nvim_buf_is_valid(args.buf) then
    return
  end
  local buf_name = vim.api.nvim_buf_get_name(args.buf)
  -- ...
end

https://github.com/greggh/claude-code.nvim/blob/main/lua/claude-code/file_refresh.lua#L103-L104

Environment

  • Neovim: v0.10+
  • Plugin version: latest (via lazy.nvim)
  • OS: macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions