How to update a Registry value?

Background

I have a process registered in Registry. I know this because I get the following results upon using lookup:


iex(33)> Registry.lookup(MyRegistry, {Worker, 1})                       
[{#PID<0.237.0>, [available: true]}]

Problem

The problem here is that I can’t update it no matter what:

iex(34)> Registry.update_value(MyRegistry, {Worker, 1}, fn _x -> [] end)
:error

I always get an error. Now, I know the process is registered and I know the key exists because of the lookup. But somehow I can’t update the value.

Questions

What am I doing wrong?

You can only update it from the registered process itself.

It would be rather nice to have this documented (it isn’t). How can I contribute ?

It’s kinda implicit in “Updates the value for key for the current process”. You could amend it on the elixir github repo.

So what this means is that there is no way for an external process to change the Registry value of another process, correct?

Yep. Just like you cannot register another process to a registry. It would be prone to race-conditions afaik.

I guess the only solution here would be to ask the process to update it’s own registry via a message. I find (personally) this information would be useful in the documentation, although I will only considering adding it if you and José agree.

The way I see it, for a seasoned Elixir developer (which I am not [yet]) this may be obvious.

For my understanding, it is documented in Registry.update_value/3 very well, the first sentence says (emphasis mine):

Updates the value for key for the current process in the unique registry .

You can make a PR. If there’s something wrong with it I’m sure the team will let you know how to improve it.

1 Like

I think we can add a note that says: “If you attempt to update a key that does not belong to the current process, it will raise an ArgumentError.”

2 Likes