Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch failure to open Worker and write out port #85

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just err_output = read(err, String)? Can you add a comment?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I think this was an artifact of me moving things around. It doesn't make sense this way anymore.

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 m.Worker(; exeflags = ["-t invalid"])
@test occursin("ERROR: julia: -t", e.msg)
end
Loading