Skip to content

Commit

Permalink
Catch failure to open Worker and write out port
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumbiegel committed Sep 19, 2024
1 parent a3d48f2 commit 29fdd5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/Malt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,33 @@ mutable struct Worker <: AbstractWorker
function Worker(; env=String[], exeflags=[])
# Spawn process
cmd = _get_worker_cmd(; env, exeflags)
proc = open(Cmd(
cmd;
detach=true,
windows_hide=true,
), "w+")
err = Pipe()
proc = open(
pipeline(
Cmd(
cmd;
detach=true,
windows_hide=true,
),
stderr = err
),
"w+"
)

# Keep internal list
__iNtErNaL_get_running_procs()
push!(__iNtErNaL_running_procs, proc)

# Block until reading the port number of the process (from its stdout)
port_str = readline(proc)
port = parse(UInt16, port_str)
port = tryparse(UInt16, port_str)
if port === nothing
Base.kill(proc, Base.SIGTERM)
close(err.in)
err_t = Threads.@spawn read(err, String)
err_output = fetch(err_t)
error("""Failed to start worker process correctly. Expected to read port from stdout, got "$port_str" instead. Stderr output was:\n$err_output""")
end

# Connect
socket = Sockets.connect(port)
Expand Down
5 changes: 5 additions & 0 deletions test/exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,8 @@ struct LocalStruct end
m.stop(w)
@test !m.isrunning(w)
end

@testset "Failure to spin up Worker" begin
e = @catcherror Malt.Worker(; exeflags = ["-t invalid"])
@test occursin("ERROR: julia: -t", e.msg)
end

0 comments on commit 29fdd5d

Please sign in to comment.