Skip to content

Commit

Permalink
Don't create tasks for sync calls
Browse files Browse the repository at this point in the history
  • Loading branch information
savq committed Sep 23, 2022
1 parent 61e248d commit e72897d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
37 changes: 19 additions & 18 deletions src/Malt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,26 @@ function _send_msg(port::UInt16, msg)
return socket
end

# TODO: Unwrap TaskFailedExceptions
function _promise(socket)
@async begin
# Close socket even if there's a serialization exception
try
response = deserialize(socket)
response.result
catch e
rethrow(e)
finally
close(socket)
end
function _recv(socket)
try
response = deserialize(socket)
response.result
catch e
rethrow(e)
finally
close(socket)
end
end

function _send(w::Worker, msg)::Task
# Don't talk to the dead
function _send(w::Worker, msg)
isrunning(w) || throw(TerminatedWorkerException())
_promise(_send_msg(w.port, msg))
_recv(_send_msg(w.port, msg))
end

# TODO: Unwrap TaskFailedExceptions
function _send_async(w::Worker, msg)::Task
isrunning(w) || throw(TerminatedWorkerException())
@async(_recv(_send_msg(w.port, msg)))
end


Expand All @@ -122,7 +123,7 @@ julia> fetch(promise)
```
"""
function remotecall(f, w::Worker, args...; kwargs...)
_send(w, _new_call_msg(true, f, args..., kwargs...))
_send_async(w, _new_call_msg(true, f, args..., kwargs...))
end


Expand All @@ -147,7 +148,7 @@ end
Shorthand for `fetch(Malt.remotecall(…))`. Blocks and then returns the result of the remote call.
"""
function remotecall_fetch(f, w::Worker, args...; kwargs...)
fetch(_send(w, _new_call_msg(true, f, args..., kwargs...)))
_send(w, _new_call_msg(true, f, args..., kwargs...))
end


Expand All @@ -157,7 +158,7 @@ end
Shorthand for `wait(Malt.remotecall(…))`. Blocks and discards the resulting value.
"""
function remotecall_wait(f, w::Worker, args...; kwargs...)
wait(_send(w, _new_call_msg(false, f, args..., kwargs...)))
_send(w, _new_call_msg(false, f, args..., kwargs...))
end


Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ end
struct $(stub_type_name) end
end)

@test_throws(TaskFailedException,
@test_throws(Exception,
m.remote_eval_fetch(w, quote
$stub_type_name()
end),
Expand All @@ -118,7 +118,7 @@ end
struct $stub_type_name2 <: Exception end
end)

@test_throws(TaskFailedException,
@test_throws(Exception,
m.remote_eval_fetch(w, quote
throw($stub_type_name2())
end),
Expand All @@ -128,7 +128,7 @@ end
## Catching unknown exceptions and returning them as values also causes an exception.
## (just like any other unknown type).

@test_throws(TaskFailedException,
@test_throws(Exception,
m.remote_eval_fetch(w, quote
try
throw($stub_type_name2())
Expand Down

0 comments on commit e72897d

Please sign in to comment.