T0ha666

T0ha666

Hi.
I have a simple GenServer module like:

defmodule TestGenServer do
  @moduledoc false

  use GenServer

  def start_link(options), do: GenServer.start_link(__MODULE__, options, name: __MODULE__)

  @impl true
  def init(_options) do
    IO.inspect("INIT")
    {:ok, %{}, 0}
  end

  def fetch(), do: GenServer.call(__MODULE__, :fetch)

  @impl true
  def handle_call(:fetch, _from, state) do
    reply = :ok
    {:reply, reply, state}
  end

  @impl true
  def handle_info(:timeout, state) do
    IO.inspect("Timeout")
    {:noreply, state, 100}
  end
end

I’m trying to start it on other or slave node like.

Node.start(:'test@127.0.0.1')
pa = :code.get_path |> Enum.reduce([], &([~c"-pa", &1 | &2]))
{:ok, pid, node} = :peer.start_link(%{name: :test1, host: ~c"127.0.0.1", args: pa})
{:ok, p} = :rpc.call(node, TestGenServer, :start_link, [:ok])
:rpc.call(node, TestGenServer, :fetch, [])

And last call fails with

{:badrpc, {:EXIT, {:noproc, {GenServer, :call, [TelemetryMetricsMnesia, :fetch, 5000]}}}}

If I use :rpc.cast/4 to start GenServer everything works fine.

Is it expected behaviour for :npc.call/4 to terminate process after it returns?
I tried on Erlang 26.1.2 with Elixir 1.15.7 and Erlang 26.0.2 with Elixir 1.15.4. Both works in the same manner.

Showing Posts 1 to 8

LostKobrakai

LostKobrakai

RPC afaik works by having a process on the other node execute your code. That process starts and links your genserver. Then the process executing your code stops, so the genserver stops as well, as it was linked to that process.

Cast might work by accident if the process executing the first cast has not been stopped / and brought down the genserver before the call is executed.

You either want to start the genserver without linking it to the caller process or even better start it as a child of some supervisor running on the other node.

T0ha666

T0ha666 OP

Supervisor works in the same manner.

LostKobrakai

LostKobrakai

Are you trying to start a supervisor or trying to use one already running? The first option will have the exact same constraints I just described for the genserver. It doesn’t matter what process you start, if you use rpc you don’t want to link the started process to the rpc code executing process.

Not linking processes is however not a great thing to do, as they won’t be properly shut down when the system shuts down. So really the genserver (or other processes) should be linked to a supervisor in one of the supervision trees on that other node instead of being linked to the rpc code executing process.

T0ha666

T0ha666 OP

I’m trying to write UnitTest for distributed code. If there is another way I’m curious about it.

LostKobrakai

LostKobrakai

I described two options on how to resolve the problems you’re having. :peer is certainly the way to test things requiring distribution.

T0ha666

T0ha666 OP

:peer.call/4 works in the same manner.

LostKobrakai

LostKobrakai

It doesn’t matter how many layers you put over :rpc. The issue isn’t the method of transfering the code to be called to the other node. The issue is in the code you let it execute.

schneebyte

schneebyte

There is also :erlang.spawn_link
Or implement start/1 for your GenServer and use :erpc.call.
Or put your GenServer under a Supervisor and start your application on the peer node.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews