mat-hek

mat-hek

Membrane Core Team

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?

Showing Posts 1 to 8

zachallaun

zachallaun

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

mat-hek

mat-hek OP

Membrane Core Team

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.

al2o3cr

al2o3cr

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

mat-hek

mat-hek OP

Membrane Core Team

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

zachallaun

zachallaun

From my reading, you’d do something like

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

mat-hek OP

Membrane Core Team

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.

zachallaun

zachallaun

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.

mat-hek

mat-hek OP

Membrane Core Team

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews