Skip to content

fix(view): defer side-by-side render when a side's bufnr is still nil - #593

Open
Huyvux12 wants to merge 1 commit into
esmuellert:mainfrom
Huyvux12:fix/issue-592-nil-bufnr-guard
Open

Huyvux12 wants to merge 1 commit into
esmuellert:mainfrom
Huyvux12:fix/issue-592-nil-bufnr-guard

Conversation

@Huyvux12

Copy link
Copy Markdown

Summary

Fixes the crash in #592: refreshing a YAML diff with yaml-companion installed could throw update.lua:210: Invalid 'buf': Expected Lua number, and the view was then left unrendered.

Root cause

When the virtual side's content arrives from the (warm) git cache, render_when_reloaded queues render_everything with vim.schedule. A vim.wait inside an LspAttach handler — yaml-companion's request_sync — during the real side's :edit pumps that queue before open_real_file has returned, so modified_info.bufnr is still nil when the render runs. nvim_buf_is_valid(nil) throws instead of returning false, and simply returning would lose the render entirely because readiness fires only once.

Changes

  • lua/codediff/ui/view/side_by_side/update.lua: if either side's bufnr is still nil when render_everything runs, remember the attempt (render_deferred) instead of calling nvim_buf_is_valid on it, and re-arm the render with vim.schedule once M.update has loaded both sides.
  • tests/integration/ui/view/update_nil_bufnr_spec.lua: regression test. A FileType autocmd that calls vim.wait(50) stands in for yaml-companion's request_sync pump (no real LSP server needed).

Evidence

Base SHA: eee54e78e6f152a6eb03b090e95f7576c96a5ad9 (main), Neovim 0.12.6.

Before the fix — the new spec fails on main with the exact error from the issue:

.../side_by_side/update.lua:210: Invalid 'buf': Expected Lua number
  [C]: in function 'nvim_buf_is_valid'
  .../side_by_side/update.lua:210: in function <.../side_by_side/update.lua:200>
  [C]: in function 'wait'
  .../helpers.lua:163: in function 'open_real_file'
  .../side_by_side/update.lua:43: in function 'reload_side'
  .../side_by_side/update.lua:290: in function <.../side_by_side/update.lua:112>
0 passed, 1 failed

After the fix — the spec passes, and the view renders the change:

1 passed, 0 failed, 0 pending

Control case (in the same spec): an ordinary view created without the vim.wait pump renders its diff normally, before and after the fix.

Regression scope run:

./tests/run_tests.sh tests/unit/ui/view
  readiness_spec.lua: 5 passed, 0 failed
./tests/run_tests.sh tests/integration/ui/view
  ALL TESTS PASSED (15 spec files, including the new one)

stylua was not available in the environment where I ran the tests, so make lint was not run locally; the added lines follow the surrounding 2-space style and are well under the configured line width.

Scope and risks checked

  • Deliberately unchanged: the readiness/when_all mechanism, reload_side, open_real_file, and the conflict-mode render path. The conflict-mode inner guard reads the same locals, but only after the outer guard has passed, and nothing writes nil into them afterwards, so it cannot see a nil bufnr.
  • Checked the sibling render paths for the same crash class: side_by_side/create.lua and inline_view/{create,update}.lua only render after both buffers are assigned synchronously (or always hold real buffer numbers), so they cannot hit nvim_buf_is_valid(nil).
  • A deferred render from a superseded update is harmless: the re-armed render re-checks current() (generation) first.
  • Limitation: a full end-to-end repro with a real yamlls + yaml-companion was not set up; the test reproduces the exact re-entrancy (scheduled render pumped by vim.wait during :edit) and the exact crash site instead.

Closes #592

A scheduled render can run re-entrantly while M.update is still loading
the other side: vim.wait inside an LspAttach handler (e.g.
yaml-companion's request_sync) during open_real_file's :edit pumps the
vim.schedule queue, so the other side's bufnr may still be nil when
render_everything runs. nvim_buf_is_valid(nil) throws instead of
returning false, and dropping the render would leave the view
unrendered because readiness fires only once.

Defer the render instead: remember the attempt and re-arm it once both
sides are loaded.

Adds a regression test reproducing the crash before the fix.

Closes esmuellert#592
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.

Crash in side_by_side/update.lua:210 when yaml-companion is installed (same symptom as #298)

1 participant