Skip to content

Commit 623ceb7

Browse files
Ensure that open(::Function, ::Cmd) waits for termination (JuliaLang#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 <dilum@aluthge.com>
1 parent 928f63c commit 623ceb7

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

base/process.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,25 @@ process failed, or if the process attempts to print anything to stdout.
402402
"""
403403
function open(f::Function, cmds::AbstractCmd, args...; kwargs...)
404404
P = open(cmds, args...; kwargs...)
405+
function waitkill(P::Process)
406+
close(P)
407+
# 0.1 seconds after we hope it dies (from closing stdio),
408+
# we kill the process with SIGTERM (15)
409+
local t = Timer(0.1) do t
410+
process_running(P) && kill(P)
411+
end
412+
wait(P)
413+
close(t)
414+
end
405415
ret = try
406416
f(P)
407417
catch
408-
kill(P)
409-
close(P)
418+
waitkill(P)
410419
rethrow()
411420
end
412421
close(P.in)
413422
if !eof(P.out)
414-
close(P.out)
423+
waitkill(P)
415424
throw(_UVError("open(do)", UV_EPIPE))
416425
end
417426
success(P) || pipeline_error(P)

0 commit comments

Comments
 (0)