How to start_link a GenServer on an arbitrary node

Hi there,

I’d like to start_link a GenServer like this:

:rpc.call(node, GenServer, :start_link, [MyServer, options])

however, this fails, because the server gets linked to some process started by :rpc.call, not to the process that executes the call. How do I solve this?

1 Like

Can you start the GenServer and link to the resulting pid in a separate step using process.link?

I could, but it won’t be atomic. If the server crashes before I link it, the call to Process.link will raise an error. Even if I catch the error, what feels smelly, I won’t get the EXIT signal with the reason for the crash, which is sometimes needed.

This sounds like exactly the scenario that spawn_request/5 was added to OTP 23 to address.

1 Like

Hmm, interesting, but can’t see how to use this to spawn a GenServer, which only offers start and start_link

From my reading, you’d do something like

:erlang.spawn_request(
  node, 
  GenServer, 
  :start, 
  [YourModule, opts], 
  [:link]
)

Thanks, I definitely misunderstood the docs. And it seems I still don’t get how this is supposed to work…
When I spawn a task, it seems to work (using Task for simplicity)

iex(first@MacBook-Pro-9)2> :erlang.spawn_request :"second@MacBook-Pro-9", Task, :start, [fn ->   
...(first@MacBook-Pro-9)2> IO.puts("started")
...(first@MacBook-Pro-9)2> Process.register(self(), :task)
...(first@MacBook-Pro-9)2> Process.sleep(:infinity)
...(first@MacBook-Pro-9)2> end],
...(first@MacBook-Pro-9)2> [:link]
#Reference<0.2708312481.1393819650.127085>
started
iex(first@MacBook-Pro-9)3> flush
{:spawn_reply, #Reference<0.2708312481.1393819650.127085>, :ok,
 #PID<12056.121.0>}

On the other node, the process is not alive though

iex(second@MacBook-Pro-9)2> Process.alive? pid(0, 121, 0)
false

While the task is spawned under another PID

iex(second@MacBook-Pro-9)2> Process.whereis :task
#PID<0.122.0>

Another problem is that the :link option doesn’t seem to work

iex(first@MacBook-Pro-9)4> Process.flag :trap_exit, true
iex(second@MacBook-Pro-9)3> Process.exit(v, :shutdown)
true
iex(second@MacBook-Pro-9)4> Process.whereis :task
nil
iex(first@MacBook-Pro-9)5> flush
:ok

EDIT: ok I see that now. When spawn_request is called, some process is spawned on another node. If the link option is passed, the link is created to that process. Then, the spawned process spawns the actual process I want. So, to my understanding, this doesn’t establish a link to the GenServer/Task. Moreover, I need some additional logic to retrieve its PID.

What about starting the remote process with a function that calls start_link, returning the started process? You now have a “link chain” with the intermediary process linked to both your local process and remote GenServer, right? (Not tested.)

This doesn’t seem very pretty, but my goal at this point is figuring out something that works first.

defmodule RemoteSpawner do
  def start_link(node, m, f, a) do
    {_reply, _req, :ok, spawner_pid} =
      :erlang.spawn_request(
        node,
        __MODULE__,
        :start_and_distribute,
        [m, f, a],
        [:link]
      end)

    send(spawner_pid, self())
   
    receive do
      {pid, ^spawner_pid}
    end
  end

  def start_and_distribute(m, f, a) do
    {:ok, pid} = apply(m, f, a)
    distribute(pid)
  end

  def distribute(pid)
    receive do
      from -> send(from, {self(), pid})
    end

    distribute(pid)
  end
end

RemoteSpawner.start_link(node, GenServer, :start_link, [MyServer, arg])

Surely there has to be a better way though. Feels like we’re both missing something :joy:

Note: not tested, on phone.

Yeah, I think this would be done easier by using Node.spawn_link. The intermediary process should also trap exits, otherwise, it won’t be notified about exit with the reason :normal. Also, killing the intermediary process wouldn’t immediately kill the target process, as the exit signal can be trapped when it’s not ‘direct’ - this would have to be worked around if needed. As well as probably a couple more quirks I didn’t think of :stuck_out_tongue: