Node.spawn/2 working from IEX but not from within module

Hey, anyone know why I’m getting this error when Node.spawn ing from within a module? It works fine from IEX.

Here’s the code:

def handle_cast({:link_house, id}, state) do
    # TODO: Get node hostname from hashring using house id
    session_pid = self()

    Node.spawn(:"hivencore1@Phineas-MacBook-Pro", fn ->
      HivenCore.Connectivity.Remote.link_session_to_house(id, %{pid: session_pid, id: "test"})
    end)

    {:noreply, state}
end

Also note that it doesn’t matter what I put within the function, it’ll always give that error.

I also receive the error on the remote node as well as the calling node.

Thanks!

Someone offsite managed to help me pinpoint the issue!

The solution is to use Node.spawn/4 instead of Node.spawn/2

So, in my case the code should be changed to this:

 Node.spawn(:"hivencore1@Phineas-MacBook-Pro", HivenCore.Connectivity.Remote, :link_session_to_house, [id, %{pid: session_pid, id: "test"}])

You can read José’s comment on GitHub as to why this happens here: https://github.com/elixir-lang/elixir/issues/1520#issuecomment-21870850

1 Like