Skip to content

fix: defer render when buffer numbers aren't assigned yet - #591

Closed
jonsmithers wants to merge 2 commits into
esmuellert:mainfrom
jonsmithers:fix-render-nil-bufnr
Closed

jonsmithers wants to merge 2 commits into
esmuellert:mainfrom
jonsmithers:fix-render-nil-bufnr

Conversation

@jonsmithers

@jonsmithers jonsmithers commented Nov 19, 2026 •

Copy link
Copy Markdown

Problem

:CodeDiff can throw while switching files if something runs a blocking vim.wait() during the window switch inside open_real_file():

Invalid 'buf': Expected Lua number
stack traceback:
        [C]: in function 'nvim_buf_is_valid'
        .../codediff/ui/view/side_by_side/update.lua:210: in function <...update.lua:200>
        [C]: in function 'wait'
        ...
        [C]: in function 'nvim_set_current_win'
        .../codediff/ui/view/helpers.lua:162: in function 'open_real_file'
        .../codediff/ui/view/side_by_side/update.lua:43: in function 'reload_side'
        .../codediff/ui/view/side_by_side/update.lua:290: in function 'show'

I hit this with nvim-tree open: nvim_set_current_win triggers a BufEnter autocmd for the NvimTree_* buffer, nvim-tree reloads its git state and calls vim.wait(), and that drains scheduled callbacks. One of them is render_everything, which runs before open_real_file has returned. info.bufnr for the real side is still nil, so nvim_buf_is_valid(nil) throws.

Fix

In render_everything, if either bufnr isn't assigned yet, reschedule instead of continuing. By the next tick reload_side has returned and both buffers are set. Simply returning early would hide the error but never draw the diff, so I reschedule. The retry uses vim.defer_fn rather than vim.schedule: vim.wait() drains scheduled callbacks, so re-queueing with vim.schedule from inside the wait spins forever (nvim hangs), whereas a timer fires after the wait returns. current() is still checked first, so a stale session stops retrying.

render_everything is now declared before assignment so it can reference itself.

Related

Testing

Verified headless: :CodeDiff, then <CR> on a modified file in the explorer with nvim-tree's BufEnter autocmd active, returns without error or hang. The original stack trace was reproduced separately. The full test suite has not been run and no regression test is included.

🤖 Generated with Claude Code

vim.wait() drains scheduled callbacks, so re-queueing with vim.schedule
from inside the wait that blocks bufnr assignment spun forever.
@jonsmithers

Copy link
Copy Markdown
Author

jk this causes problems

@jonsmithers jonsmithers reopened this Nov 19, 2026
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