Skip to content

Commit

Permalink
fix(blame): fix possible race condition
Browse files Browse the repository at this point in the history
It is possible for the buffer to change during the async function. To
avoid this, before applying the extmark, check that the cursor is in the
same position. If it is the extmark should be ok to apply and at the
very least will avoid the `line value outside range` error.

Fixes #312
  • Loading branch information
lewis6991 committed Oct 18, 2021
1 parent 02b9799 commit 2a1f3e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lua/gitsigns/current_line_blame.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions teal/gitsigns/current_line_blame.tl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ M.reset = function(bufnr: integer)
pcall(api.nvim_buf_del_var, bufnr, 'gitsigns_blame_line_dict')
end

-- Update function, must be called in async context
M.update = void(function()
M.reset()

Expand All @@ -49,6 +50,13 @@ M.update = void(function()

M.reset(bufnr)

local lnum1 = api.nvim_win_get_cursor(0)[1]
if lnum ~= lnum1 or not api.nvim_buf_is_loaded(bufnr) then

This comment has been minimized.

Copy link
@jdrouhard

jdrouhard Oct 18, 2021

Should this also check that current_buf() == bufnr in case the cursor is on the same line of a different buffer when the async function returns?

This comment has been minimized.

Copy link
@lewis6991

lewis6991 Oct 18, 2021

Author Owner

Yeah good idea.

-- Cursor has moved during events; abort
-- Buffer is no longer loaded; abort
return
end

api.nvim_buf_set_var(bufnr, 'gitsigns_blame_line_dict', result)
if config.current_line_blame_opts.virt_text then
api.nvim_buf_set_extmark(bufnr, namespace, lnum-1, 0, {
Expand Down

0 comments on commit 2a1f3e6

Please sign in to comment.