Skip to content

Commit a2e7cf4

Browse files
committed
propagate errors on wait(::RemoteRef) and remotecall_wait
1 parent 8ada3cf commit a2e7cf4

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

base/multi.jl

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,9 @@ function remotecall_wait(f, w::Worker, args...)
749749
rv.waitingfor = w.id
750750
rr = RemoteRef(w)
751751
send_msg(w, CallWaitMsg(f, args, rr2id(rr), prid))
752-
wait(rv)
752+
v = fetch(rv.c)
753753
delete!(PGRP.refs, prid)
754+
isa(v, RemoteException) && throw(v)
754755
rr
755756
end
756757

@@ -783,8 +784,18 @@ function call_on_owner(f, rr::RemoteRef, args...)
783784
end
784785
end
785786

786-
wait_ref(rid, args...) = (wait(lookup_ref(rid).c, args...); nothing)
787-
wait(r::RemoteRef, args...) = (call_on_owner(wait_ref, r, args...); r)
787+
function wait_ref(rid, callee, args...)
788+
v = fetch_ref(rid, args...)
789+
if isa(v, RemoteException)
790+
if myid() == callee
791+
throw(v)
792+
else
793+
return v
794+
end
795+
end
796+
nothing
797+
end
798+
wait(r::RemoteRef, args...) = (call_on_owner(wait_ref, r, myid(), args...); r)
788799

789800
fetch_ref(rid, args...) = fetch(lookup_ref(rid).c, args...)
790801
fetch(r::RemoteRef, args...) = call_on_owner(fetch_ref, r, args...)
@@ -796,19 +807,23 @@ put_ref(rid, args...) = put!(lookup_ref(rid), args...)
796807
put!(rr::RemoteRef, args...) = (call_on_owner(put_ref, rr, args...); rr)
797808

798809
take!(rv::RemoteValue, args...) = take!(rv.c, args...)
799-
take_ref(rid, args...) = take!(lookup_ref(rid), args...)
800-
take!(rr::RemoteRef, args...) = call_on_owner(take_ref, rr, args...)
810+
function take_ref(rid, callee, args...)
811+
v=take!(lookup_ref(rid), args...)
812+
isa(v, RemoteException) && (myid() == callee) && throw(v)
813+
v
814+
end
815+
take!(rr::RemoteRef, args...) = call_on_owner(take_ref, rr, myid(), args...)
801816

802817
close_ref(rid) = (close(lookup_ref(rid).c); nothing)
803818
close(rr::RemoteRef) = call_on_owner(close_ref, rr)
804819

805820

806821
function deliver_result(sock::IO, msg, oid, value)
807822
#print("$(myid()) sending result $oid\n")
808-
if is(msg,:call_fetch)
823+
if is(msg,:call_fetch) || isa(value, RemoteException)
809824
val = value
810825
else
811-
val = oid
826+
val = :OK
812827
end
813828
try
814829
send_msg_now(sock, ResultMsg(oid, val))
@@ -903,7 +918,7 @@ end
903918
function handle_msg(msg::CallWaitMsg, r_stream, w_stream)
904919
@schedule begin
905920
rv = schedule_call(msg.response_oid, ()->msg.f(msg.args...))
906-
deliver_result(w_stream, :call_wait, msg.notify_oid, wait(rv))
921+
deliver_result(w_stream, :call_wait, msg.notify_oid, fetch(rv.c))
907922
end
908923
end
909924

test/parallel_exec.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,24 @@ catch ex
402402
@test collect(1:5) == sort(map(x->parse(Int, x), errors))
403403
end
404404

405-
try
406-
remotecall_fetch(()->throw(ErrorException("foobar")), id_other)
407-
error("unexpected")
408-
catch ex
409-
@test typeof(ex) == RemoteException
410-
@test typeof(ex.captured) == CapturedException
411-
@test typeof(ex.captured.ex) == ErrorException
412-
@test ex.captured.ex.msg == "foobar"
405+
macro test_remoteexception_thrown(expr)
406+
quote
407+
try
408+
$(esc(expr))
409+
error("unexpected")
410+
catch ex
411+
@test typeof(ex) == RemoteException
412+
@test typeof(ex.captured) == CapturedException
413+
@test typeof(ex.captured.ex) == ErrorException
414+
@test ex.captured.ex.msg == "foobar"
415+
end
416+
end
417+
end
418+
419+
for id in [id_other, id_me]
420+
@test_remoteexception_thrown remotecall_fetch(()->throw(ErrorException("foobar")), id)
421+
@test_remoteexception_thrown remotecall_wait(()->throw(ErrorException("foobar")), id)
422+
@test_remoteexception_thrown wait(remotecall(()->throw(ErrorException("foobar")), id))
413423
end
414424

415425
# The below block of tests are usually run only on local development systems, since:

0 commit comments

Comments
 (0)