Call GenServer from a remote node

I have the following code running on node “a”:

defmodule Devices.Teste do
  use GenServer

  def start_link() do
    IO.inspect(["a", self()])
    GenServer.start_link(__MODULE__, [], [])

  def a(pid) do
    IO.inspect(["b", self(), pid]), :a)

  @impl true
  def init([]) do
    IO.inspect(["c", self()])
    {:ok, []}

  @impl true
  def handle_call(:a, from, state) do
    IO.inspect(["d", :a, self(), from])
    {:reply, :xyz, state}

  @impl true
  def handle_info(x, state) do
    IO.inspect(["e", x, self()])
    {:noreply, state}

From a node b, I call

{:ok, pid} ="a@", Devices.Teste, :start_link, [])

then, the messages

["a", #PID<0.225.0>]
["c", #PID<0.226.0>]

are printed and the call returns

{:ok, #PID<20968.226.0>}

After that, I call"a@", Devices.Teste, :a, [pid])

then, the message

["b", #PID<0.227.0>, #PID<0.226.0>]

is printed, but I get the following error:

** (exit) {:exception, {:noproc, {GenServer, :call, [#PID<20968.226.0>, :a, 5000]}}}
    (kernel 8.5.4) erpc.erl:700:
    iex:2: (file)

If I call, :a)

no messages are printed and I get the following error

** (exit) exited in:<20968.214.0>, :a, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir 1.15.0) lib/gen_server.ex:1074:
    iex:2: (file)

What am I missing?
As long as I understand, I should use the “remote” pid that I get from the start_link function.

When the :erpc call is finished the spawned process on the remote node is terminated and it brings down the started child as well because they are linked.

Consider using start instead of start_link, and this approach should resolve the issue. Unlike start_link, start does not link the caller to the created processes. As a result, even after the caller terminates, the created processes remain active.

GenServer.start(__MODULE__, [], [])

There might be a more elegant solution to your problem, but I’m not fully aware of the entire context.

Be careful with ‘start’ because it can leave rogue processes in the system…


start did the trick. Thanks @anuarsaeed.

1 Like