Skip to content

Commit 94f5f40

Browse files
committed
fix(file): Handle race condition during buffer creation
(rel. sindrets#392)
1 parent 766a4f2 commit 94f5f40

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lua/diffview/scene/window.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,16 @@ end
9292
Window.load_file = async.wrap(function(self, callback)
9393
assert(self.file)
9494

95-
if self.file.bufnr and api.nvim_buf_is_valid(self.file.bufnr) then
96-
return callback(true)
97-
end
95+
if self.file:is_valid() then return callback(true) end
9896

9997
local ok, err = pawait(self.file.create_buffer, self.file)
10098

99+
if ok and not self.file:is_valid() then
100+
-- The buffer may have been destroyed during the await
101+
ok = false
102+
err = "The file buffer is invalid!"
103+
end
104+
101105
if not ok then
102106
logger:error(err)
103107
utils.err(fmt("Failed to create diff buffer: '%s:%s'", self.file.rev, self.file.path), true)

lua/diffview/vcs/file.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ File.create_buffer = async.wrap(function(self, callback)
255255

256256
-- Revalidate buffer in case the file was destroyed before `produce_data()`
257257
-- returned.
258-
if not api.nvim_buf_is_valid(self.bufnr) then return end
258+
if not api.nvim_buf_is_valid(self.bufnr) then
259+
error("The buffer has been invalidated!")
260+
return
261+
end
259262
local bufopts = vim.deepcopy(File.bufopts)
260263

261264
if self.rev.type == RevType.STAGE and self.rev.stage == 0 then

0 commit comments

Comments
 (0)