Avoid race condition processing the same buffer twice, quickly.

The diffing part of the plugin diffs two files which are written afresh
each time.  When the same buffer was processed twice in quick
succession, the second process to write a file could write it before the
first process had finished reading it.  This manifested as the "from"
file being read as empty, causing diff to report that all the lines in
the file had been added.

This commit adds a counter to disambiguate successive temporary files.
This commit is contained in:
Andy Stewart
2018-11-17 11:29:41 +00:00
parent 947737c76f
commit 32d53f134a

View File

@@ -13,6 +13,7 @@ let s:c_flag = s:git_supports_command_line_config_override()
let s:temp_from = tempname() let s:temp_from = tempname()
let s:temp_buffer = tempname() let s:temp_buffer = tempname()
let s:counter = 0
" Returns a diff of the buffer against the index or the working tree. " Returns a diff of the buffer against the index or the working tree.
" "
@@ -89,6 +90,11 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort
" git-diff). " git-diff).
let buff_file = s:temp_buffer.'.'.a:bufnr let buff_file = s:temp_buffer.'.'.a:bufnr
" Add a counter to avoid a similar race with two quick writes of the same buffer.
" Use a modulus greater than a maximum reasonable number of visible buffers.
let s:counter = (s:counter + 1) % 20
let buff_file .= '.'.s:counter
let extension = gitgutter#utility#extension(a:bufnr) let extension = gitgutter#utility#extension(a:bufnr)
if !empty(extension) if !empty(extension)
let buff_file .= '.'.extension let buff_file .= '.'.extension
@@ -104,6 +110,9 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort
" reading it (with git-diff). " reading it (with git-diff).
let from_file = s:temp_from.'.'.a:bufnr let from_file = s:temp_from.'.'.a:bufnr
" Add a counter to avoid a similar race with two quick writes of the same buffer.
let from_file .= '.'.s:counter
if !empty(extension) if !empty(extension)
let from_file .= '.'.extension let from_file .= '.'.extension
endif endif