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>
  • Loading branch information
staticfloat and DilumAluthge authored Feb 19, 2022
1 parent 928f63c commit 623ceb7
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,25 @@ process failed, or if the process attempts to print anything to stdout.
"""
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)
close(P)
waitkill(P)
rethrow()
end
close(P.in)
if !eof(P.out)
close(P.out)
waitkill(P)
throw(_UVError("open(do)", UV_EPIPE))
end
success(P) || pipeline_error(P)
Expand Down

0 comments on commit 623ceb7

Please sign in to comment.