3

I am trying to find out how to proper way to use RemoteChannel inside of a macro. In a function or the REPL, the following code works:

addprocs(3)
r = RemoteChannel(3)
@spawnat(3,put!(r,10))
fetch(r) # Gives 10

However, if I put that same stuff in a macro:

macro rrtest(src,val)
  quote
    r = RemoteChannel($(esc(src)))
    @spawnat($(esc(src)), put!(r, $(esc(val))))
    println(fetch(r))
  end
end

and then call it with the same arguments

@rrtest(3,10)

then the REPL just stalls. Is there something wrong with using RemoteChannels like this?

Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81

1 Answers1

2
macro rrtest(src,val)
     quote
       r = RemoteChannel($(esc(src))) #Using a `Future` here maybe be better
       remotecall_wait(r_i->put!(r_i, $(esc(val))), $(esc(src)), r)
       wait(r);
       println(fetch(r))
     end
   end

The wait(r) should not be required -- fetch is supposed call wait when used on a Future or RemoteChannel. But it does seem to be, sometimes.

Changing the @spawnat to a remotecall means you can pass in the r, without that, it gets. I think there are off things with how macro-hygine nests with closures created themself with macros. (@spawnat) creates closures inside another macro. It is awkaward to reasonabout. In general I find @spawnat harder to reason about, than remote_call.

The reason it needs to be remotecall_wait is because otherwise, there is no garentee when its contents will run. Which means what is happening to r is itself unclear. I feel like it should be safe, but it does not seem to be. I think because the waiting for r, rather than waiting for the remotecall that sets r is never certain to allow that remotecall to run.

In Conclusion:

  • prefer remotecall to @spawnat, particularly in macros, for sake of being easier to reason about.
  • Sometimes you have to wait for things before fetching them. That is probably a bug
  • sometimes waiting for X, when X is to be set by a remotecall (or @spawnat) returning future Y, requires waiting for Y first. Also probably a bug
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137