Skip to content

Commit a9ae83d

Browse files
Ensure that open(::Function, ::Cmd) waits for termination (#44078)
On Windows, we observed occasional issues where an error within the function callback to the `open(::Function, ::Cmd)` method would cause problems due to assuming that the opened process had finished by the time the `open()` call was finished. In most cases this was true, however on Windows, it was found that we need to explicitly `wait()` upon the process object to ensure that all file handles held by the subprocess were properly closed by the time `open()` is finished. Co-authored-by: Dilum Aluthge <[email protected]> (cherry picked from commit 623ceb7)
1 parent 19de0ac commit a9ae83d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

base/process.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,26 @@ Returns the value returned by `f`.
389389
"""
390390
function open(f::Function, cmds::AbstractCmd, args...; kwargs...)
391391
P = open(cmds, args...; kwargs...)
392+
function waitkill(P::Process)
393+
close(P)
394+
# 0.1 seconds after we hope it dies (from closing stdio),
395+
# we kill the process with SIGTERM (15)
396+
local t = Timer(0.1) do t
397+
process_running(P) && kill(P)
398+
end
399+
wait(P)
400+
close(t)
401+
end
392402
ret = try
393403
f(P)
394404
catch
395-
kill(P)
405+
waitkill(P)
396406
rethrow()
397-
finally
398-
close(P.in)
407+
end
408+
close(P.in)
409+
if !eof(P.out)
410+
waitkill(P)
411+
throw(_UVError("open(do)", UV_EPIPE))
399412
end
400413
success(P) || pipeline_error(P)
401414
return ret

0 commit comments

Comments
 (0)