ndrean

ndrean

I am circling around with the following problem and no post helped me so far.
I have a GenServer module named MyApp.Repo here that runs GenServer.cast but GenServer.call returns an error and kills the process.

GenServer MyApp.Repo terminating
** (ArgumentError) unknown registry: MyApp.Repo

When I start the app, I check:

iex> Enum.member?(Process.registered(), MyApp.Repo) 
=> true

The GenServer module MyApp.Repo is supervised as a child by the Application supervisor.

children = [
      MyAppWeb.Telemetry,
      {Phoenix.PubSub, name: MyApp.PubSub, adapter: Phoenix.PubSub.PG2},
      MyApp.Repo,
     MyAppWeb.Endpoint
  ...

MyApp.Repo module is a basic GenServer that registers a :name:

def start_link(),
  do: GenServer.start_link(__MODULE__, [], name: __MODULE__)
 
def save_with_cast(user),
  do: GenServer.cast(__MODULE__, {:new, user})

def save_with_call(user) do
  IO.inspect(user, label: "CALL______")  
  GenServer.call(__MODULE__, {:new, user})
end
 
def handle_call({:new, user}, _from,_state) do
    IO.puts("CALL______")
    Phoenix.PubSub.broadcast_from(MyApp.PubSub, self(), @topic, {:perform_new, user})
  {:reply, user, [])
end

def handle_cast({:new, user}, _state) do
   Phoenix.PubSub.broadcast_from(MyApp.PubSub, self(), @topic, {:perform_new, user})
  {:noreply, []}
end

def handle_info({:perform_new, message}, _state) do
  ...

MyApp.save_with_cast(user) runs :ok but not MyApp.save_with_call(user).

Showing Posts 1 to 10

msimonborg

msimonborg

Can you paste the implementations of handle_cast/2 and handle_call/3, and the stacktrace of the ArgumentError?

ndrean

ndrean OP

I added some code above, and the stacktrace is below:

<< this is sent by the client function >>
CALL____: {"aa@mail.co.uk",
 "SFMyNTY.g2gDbQAAAA1hYUBtYWlsLmNvLnVrbgYAJtMGyoEBYVo.W9C4G6ovv49k_Q-eL-mIUI84DLgDzKw9Le2nwavVuKQ",
 "3efb4325-d1f6-4002-840b-0a2e717c99d8", 1656951853878154000}
<< the server is never called >>
[error] GenServer MyApp.Repo terminating
** (ArgumentError) unknown registry: MyApp.Repo
    (elixir 1.13.4) lib/registry.ex:1338: Registry.info!/1
    (elixir 1.13.4) lib/registry.ex:834: Registry.unregister/2
    (my_app 0.1.0) lib/my_app/repo.ex:138: MyApp.Repo.terminate/2
    (stdlib 4.0.1) gen_server.erl:1158: :gen_server.try_terminate/3
    (stdlib 4.0.1) gen_server.erl:1348: :gen_server.terminate/10
    (stdlib 4.0.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message (from #PID<0.596.0>): {:new, {"aa@mail.co.uk", "SFMyNTY.g2gDbQAAAA1hYUBtYWlsLmNvLnVrbgYAJtMGyoEBYVo.W9C4G6ovv49k_Q-eL-mIUI84DLgDzKw9Le2nwavVuKQ", "3efb4325-d1f6-4002-840b-0a2e717c99d8", 1656951853878154000}}
State: []
Client #PID<0.596.0> is alive
msimonborg

msimonborg

Where does the message variable come from?

Is MyApp.Repo pubsub broadcasting to itself?

What is in your terminate/2 callback? It looks like you might be trying to unregister from a nonexistent registry with Registry.unregister(__MODULE__, key)

ndrean

ndrean OP

message was user in fact, I copied the wrong one. I changed the names to be more readable.

Then I added this because I saw this somewhere and wasn’t sure :slight_smile:

def terminate(reason, state) do
    Phoenix.PubSub.unsubscribe(MyApp.Repo, @topic)
    {:stop, reason, state}
  end

OK, I removed this. Now I have a different error:

[error] GenServer MyApp.Repo terminating
** (FunctionClauseError) no function clause matching in MyApp.Repo.handle_call/3
    (my_app 0.1.0) lib/my_app/repo.ex:83:MyApp.Repo.handle_call({:new, {"aa@mail.co.uk", "SFMyNTY.g2gDbQAAAA1hYUBtYWlsLmNvLnVrbgYAcMIZyoEBYVo.IFUl3rJgl7u3RpcHTjTC9VJzZJSQ-TXmBg4DT6BXBYU", "c29df058-f55e-4047-bf86-5b3ddb5fa17b", 1656953094779893000}}, {#PID<0.596.0>, [:alias | #Reference<0.603041372.4001431555.215745>]}, [])

This was my inspiration

It was a typo (:nw instead of :new in the handle_call). cast and call run normal. Thanks for the guidance on this terminatefunction. So the typo killed or stopped and the termination function destroyed things I believe.

msimonborg

msimonborg

There it is!

unsubscribe/2, like broadcast and subscribe and friends, needs a valid PubSub name as the first argument.

Phoenix.PubSub.unsubscribe(MyApp.PubSub, @topic) would be correct, it infers the pid to unsubscribe automatically. That is the cause of your ** (ArgumentError) unknown registry: MyApp.Repo. Unsubscribing in the terminate/2 callback is not necessary by the way, you are subscribed by pid which is automatically cleaned up if the process restarts. Also you do not need pubsub at all to send messages to yourself, just use send(self(), {:perform_new, user}) if MyApp.Repo is the only process interested in the message. And even then, you only need to do this if you require async execution, otherwise you can implement the perform_new directly in the original callback. Having separate call and cast handlers that implement the same message sending to self() like this is kind of an anti-pattern in my opinion, call implies that it’s a synchronous operation but you are forcing it to be async so there’s no reason to have a call implementation.

All that said, this error is raising because your process is already crashing somewhere else. If you remove this terminate/2 callback entirely you may get a more helpful error message about what the real problem is.

msimonborg

msimonborg

Double check the handle_call/3 function head, this is indicating you made a mistake there. Or you might need to recompile() your project if you’ve added this function since starting your session

msimonborg

msimonborg

With the way you’re editing your posts as you solve your problem it makes the thread harder to follow for anyone else reading it, and makes it look like I’m replying to a problem you already solved lol. Might want to post new developments as new comments especially if previous posts have already been replied to.

ndrean

ndrean OP

ok! Thanks!

ndrean

ndrean OP

Yes, this needs an explanation. I am “pubsubing” to other nodes, not sending messages to self()

msimonborg

msimonborg

Gotcha, that makes sense

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
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
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
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews