Skip to content

Commit

Permalink
fix: on_stdout and on_exit potential race
Browse files Browse the repository at this point in the history
attempt at #51
  • Loading branch information
gsuuon committed Feb 23, 2024
1 parent 27779f6 commit 94d6c80
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lua/model/util/system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ local function system(cmd, args, opts, on_stdout, on_error, on_exit, stdin_text)
local stdin = assert(uv.new_pipe(false), 'Failed to open stdin pipe')

local _error_output = ''
local _did_finish_read_stdin = false
local _can_exit = false

local handle = assert(
uv.spawn(
Expand All @@ -27,7 +29,13 @@ local function system(cmd, args, opts, on_stdout, on_error, on_exit, stdin_text)
-- success
if exit_code == 0 then
if on_exit ~= nil then
on_exit()
vim.schedule(function()
if _did_finish_read_stdin then
on_exit()
else
_can_exit = true
end
end)
end

return
Expand All @@ -50,7 +58,14 @@ local function system(cmd, args, opts, on_stdout, on_error, on_exit, stdin_text)

uv.read_start(stderr, function(err, text)
assert(not err, err)
if text then

if text == nil then -- nil means EOF
if _can_exit and on_exit then
on_exit()
else
_did_finish_read_stdin = true
end
else
_error_output = _error_output .. text
end
end)
Expand Down

0 comments on commit 94d6c80

Please sign in to comment.