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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews