Registry dont "unregister"

Hello,

Actually I’m having problem to unregister in Registry supervisor, when a user connects and send a especific message it check if the user if auth, if not create a GenServer than generate a name using Registry, but when the socket disconnect it unregister this name:

    fn(pid) ->
      account_login = AuthClient.get_account(pid) # Check for the name
      Registry.unregister(:accounts_registry, account_login) # Unregister the name, and exit the genserver (?)
      IO.inspect(account_login)
      IO.inspect(Registry.lookup(:accounts_registry, account_login)) #Show the actual names running
    end

But the result is this:

iex(1)> "test" # account_login
iex(1)> [{#PID<0.254.0>, nil}] # lookup, as show it dont unregister

Someone knows how I can fix this problem?

Greetings!

EDIT: When I terminate the GenServer directly ranch gives me this exception:

Ranch listener :MyApp had connection process started with MyApp.Sync.Handler:start_link/4 at #PID<0.251.0> exit with reason: {:normal, {GenServer, :call, [{:via, Registry, {:accounts_registry, "test"}}, :terminate, 5000]}}

I can think of two possible reasons:

  1. the call to register is somehow different to the unregister (e.g. different registry name, different key provided); this is probably less likely as it is easy to notice :slight_smile:
  2. register and unregister associate data with the current process (the process those functions are called from). so if you call register from process A and then unregister from process B, the unregister won’t “work”: the data set by the register call in process A will still be there

If it is the second case, then you need to either move the unregister call to the same process as is calling register, or you need to pick a different library to use … :slight_smile: I have used syn before with good results

1 Like

Oh allright not have sense,

But for example I create the registry on a GenServer, but if I call unregister on this same genserver still not working :frowning:

I’ll take a look to syn, I dont know a lot of erlang so will take some time :stuck_out_tongue:

Solved!

I was calling from the wrong GenServer process

Thanks for the help :smiley:

1 Like

np; glad you managed to resolve it! :slight_smile: