Any way subscribe to Registry's 'unregister'?

I would find it quite useful to be able to get a callback when Registry removes a {pid, value} on process death (even if this is deferred).

Looking through Registry’s code, though, I couldn’t find the spot where it even handles this. :frowning:

Any ideas or line numbers in the repo would be appreciated! Thanks!

You’d probably be better off explicitly having one of your own processes monitor the process you wish to be messaged about.

2 Likes

I have hundreds of thousands of processes stored in the Registry with associated values already.

I assumed the Registry, since it’s already ‘monitoring’ all of them, would be a good place to include cleanup logic.

It could be that I should just write my own Registry variant with ETS – but I was hoping to stay as closely aligned to Elixir’s core as possible.

I think you should be able to use the listeners option to achieve what you want - https://hexdocs.pm/elixir/Registry.html#start_link/1

2 Likes

I think something like that would work – but I think the spot I’d like a callback from would be at this line (where listeners doesn’t seem to be involved, I think…):

But they are called here, directly in the unregister function which is what your question seemed to be about… is there some reason that’s not good enough/an edge case we’re missing you’re trying to handle?:

That’s what the documentation refers to. You are only notified of explicit registration/unregistration. If you want to know a process crashed, then you need to monitor it, as suggested by @benwilson512.

2 Likes

Awesome, thanks for the feedback everyone! :slight_smile:

For anyone interested, I ended up keeping Registry for concurrent access to the data – and then running a GenServer that subscribed to a pubsub that would explicitly check Process.alive? from the pids coming out of Registry for my edge cases.

Seems to work so far :slight_smile: