Skip to content

fix: prevent crash with render-markdown.nvim on new file diff accept#224

Open
bluzername wants to merge 1 commit intocoder:mainfrom
bluzername:fix/diffoff-crash-render-markdown
Open

fix: prevent crash with render-markdown.nvim on new file diff accept#224
bluzername wants to merge 1 commit intocoder:mainfrom
bluzername:fix/diffoff-crash-render-markdown

Conversation

@bluzername
Copy link
Copy Markdown

Problem

Neovim crashes with exit code 139 (segfault) when you accept a new file diff by pressing :w while render-markdown.nvim is installed. Only happens with new files, existing file diffs work fine.

I was losing my neovim session every time Claude suggested a new markdown file and I tried to accept it. Very annoying because you lose all your buffers.

Root Cause

After the BufWriteCmd callback finishes _resolve_diff_as_saved() and returns, Neovim does a post-write redraw. At this point the buffer is still in diff mode, and render-markdown.nvim tries to render it during the redraw cycle. For new files (where no old file existed on disk), this combination triggers a segfault.

The issue reporter (@tomerlevy1) did excellent debugging with breadcrumb logging and confirmed that all Lua code completes successfully - the crash happens after the callback returns control back to Neovim's C code.

Fix

Added pcall(vim.cmd, "diffoff") after _resolve_diff_as_saved() but before return true in the BufWriteCmd callback. This explicitly turns off diff mode before Neovim does the post-write redraw, so when render-markdown.nvim runs during redraw the buffer is no longer in diff state.

Using pcall because if diffoff fails for whatever reason we still want the callback to complete normally.

callback = function()
    M._resolve_diff_as_saved(tab_name, new_buffer)
    pcall(vim.cmd, "diffoff")  -- prevents segfault with render-markdown.nvim
    return true
end,

Testing

  • Lua syntax verified
  • This is the exact fix that the issue author tested and confirmed prevents the crash
  • The change is minimal (one line) and uses pcall so it cannot introduce new errors
  • Only affects the BufWriteCmd callback for new file diffs, existing behavior is unchanged

Closes #218

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.

[BUG] Neovim crashes when accepting new file diff with render-markdown.nvim installed

1 participant