T0ha666
Unexpected :slave.call/4 or :peer.call/4 behaviour
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.
First Post!
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.
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








