@@ -56,7 +56,7 @@ M.spawn = function(opts, fn_transform, fn_done)
5656 local EOL = opts .EOL or " \n "
5757 local output_pipe = uv .new_pipe (false )
5858 local error_pipe = uv .new_pipe (false )
59- local write_cb_count , on_exit_called = 0 , nil
59+ local write_cb_count , read_cb_count , on_exit_called = 0 , 0 , nil
6060 local prev_line_content = nil
6161
6262 if opts .fn_transform then fn_transform = opts .fn_transform end
@@ -91,8 +91,9 @@ M.spawn = function(opts, fn_transform, fn_done)
9191 table.insert (args , tostring (opts .cmd ))
9292 end
9393
94+ local handle , pid
9495 --- @diagnostic disable-next-line : missing-fields
95- local handle , pid = uv .spawn (shell , {
96+ handle , pid = uv .spawn (shell , {
9697 args = args ,
9798 stdio = { nil , output_pipe , error_pipe },
9899 cwd = opts .cwd ,
@@ -113,12 +114,15 @@ M.spawn = function(opts, fn_transform, fn_done)
113114 verbatim = _is_win ,
114115 }, function (code , signal )
115116 on_exit_called = true
116- if write_cb_count == 0 and not output_pipe :is_active () then
117+ if write_cb_count == 0
118+ and read_cb_count == 0
119+ and not output_pipe :is_active ()
120+ then
117121 -- Do not call `:read_stop` or `:close` here as we may have data
118122 -- reads outstanding on slower Windows machines (#1521), only call
119123 -- `finish` if all our `uv.write` calls are completed and the pipe
120124 -- is no longer active (i.e. no more read cb's expected)
121- finish (code , signal , 1 )
125+ finish (code , signal , 1 , pid )
122126 end
123127 end )
124128
@@ -211,10 +215,14 @@ M.spawn = function(opts, fn_transform, fn_done)
211215 end
212216
213217 local read_cb = function (err , data )
214- local read = function () _read_cb (err , data ) end
218+ read_cb_count = read_cb_count + 1
219+ local read = function ()
220+ _read_cb (err , data )
221+ read_cb_count = read_cb_count - 1
222+ end
215223 -- Avoid "attempt to yield across C-call boundary"
216- -- if vim.in_fast_event() then vim.schedule(read) else read() end
217- read ()
224+ if vim .in_fast_event () then vim .schedule (read ) else read () end
225+ -- read()
218226 end
219227
220228 local err_cb = function (err , data )
0 commit comments