Skip to content

Commit

Permalink
Ensure that open(::Function, ::Cmd) waits for termination (#44078)
Browse files Browse the repository at this point in the history
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 <dilum@aluthge.com>
(cherry picked from commit 623ceb7)
  • Loading branch information
staticfloat authored and KristofferC committed Mar 15, 2022
1 parent 596714c commit 19187f1
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,26 @@ Returns the value returned by `f`.
"""
function open(f::Function, cmds::AbstractCmd, args...; kwargs...)
P = open(cmds, args...; kwargs...)
function waitkill(P::Process)
close(P)
# 0.1 seconds after we hope it dies (from closing stdio),
# we kill the process with SIGTERM (15)
local t = Timer(0.1) do t
process_running(P) && kill(P)
end
wait(P)
close(t)
end
ret = try
f(P)
catch
kill(P)
waitkill(P)
rethrow()
finally
close(P.in)
end
close(P.in)
if !eof(P.out)
waitkill(P)
throw(_UVError("open(do)", UV_EPIPE))
end
success(P) || pipeline_error(P)
return ret
Expand Down

0 comments on commit 19187f1

Please sign in to comment.