When I try running a simple process on the other node, I get this error:
iex(dev@127.0.0.1)7> caller = self()
#PID<0.115.0>
iex(dev@127.0.0.1)8> Node.spawn(:"suv@127.0.0.1", fn -> send(caller, {:response, 1+2}) end)
#PID<14963.130.0>
11:43:45.064 [error] Process #PID<14963.130.0> on node :"suv@127.0.0.1" raised an exception
** (BadFunctionError) function #Function<43.125776118/0 in :erl_eval.expr/6> is invalid, likely because it points to an old version of the code
:erlang.apply/2
iex(dev@127.0.0.1)9> nil
The error implies :erl_eval.expr/6 is missing, so my first guess was differing versions of Elixir and/or Erlang/OTP on each node, but both have elixir 1.15 and OPT 26 as far as I can tell:
Re erl_eval in #Function<43.125776118/0 in :erl_eval.expr/6> I think it just means that the anonymous function was defined in IEx. It might or might not mean that erl_eval is the problem. It might as well be Kernel.send/1, if that module / function were updated between 1.15.5 and 1.15.8, which they probably were, at least in “version”.
For my own curiosity, how could I verify where the issue might lie? Look at the Kernel.send source code for each of the Elixir versions or is there a better way?
I would guess that the closures capture module versions (MD5 by default) in addition to module and function names and since they are different in different versions of Elixir, it results in an error. And maybe sending Node.spawn(:"suv@127.0.0.1", Kernel, :send, [caller, {:response, 1+2}]) doesn’t concern itself with module versions and just runs apply(Kernel, :send, [caller, {:response, 1+2}]) so it works across Elixir versions. But it can possibly produce different results from if this was executed locally. But I guess that’s usually expected with distributed systems.
When a local fun is called, the same version of the code that created the fun is called (even if a newer version of the module has been loaded).
That version seems to be stored in :new_uniq attribute. Maybe it does some rolling hash of the versions of all the modules mentioned in the function body.
When spawning a remote process like this, does the ‘work’ (1+2 in this case) get evaluated on the local node?
I’d like the ‘work’ to be evaluated on the remote node, and when I replace 1+2 with Node.self(), it appears that Node.self()is evaluated locally, and that the remote node is sending the already computed value back to the local node:
You can make the called function process its args and then that part would be handled by the remote node too:
iex(suv@127.0.0.1)> defmodule Worker do
def do_work, do: {:response, Node.self()}
end
iex(suv@127.0.0.1)> Worker.do_work()
#==> {:response, :"suv@127.0.0.1)"}
# -------------------- back to dev@127.0.0.1 -------------------------
iex(dev@127.0.0.1)> :erpc.call(:"suv@127.0.0.1", Worker, :do_work, [])
#==> {:response, :"suv@127.0.0.1)"}
:erpc.call here just executes the function remotely and sends back the results, similar to your Node.spawn example with send. I just wanted to mention it as it’s a pretty useful module.
Cool, that’s what I figured. So you either need code on the remote node which you can invoke from the local node, or pass a function to the remote node (assuming both nodes have compatible versions).
Thanks for the tip on :erpc.call, that looks pretty useful