Skip to content

Conversation

@michalmuskala
Copy link
Contributor

Currently, for every registered process there's a potential race condition where when the process terminates, the supervisor can restart it before the registry processes the DOWN message.
This isn't possible to avoid in all situations, but for servers that trap_exit and process terminate calls, this is entirely possible.

This change implements this adding a call to gen:unregister_name just after users terminate callback is processed.

Currently, for every registered process there's a potential race condition
where when the process terminates, the supervisor can restart it before
the registry processes the DOWN message.
This isn't possible to avoid in all situations, but for servers that
trap_exit and process `terminate` calls, this is entirely possible.

This change implements this adding a call to `gen:unregister_name`
just after users `terminate` callback is processed.
@github-actions
Copy link
Contributor

github-actions bot commented Oct 15, 2025

CT Test Results

    2 files     97 suites   1h 4m 51s ⏱️
2 215 tests 2 163 ✅ 52 💤 0 ❌
2 601 runs  2 545 ✅ 56 💤 0 ❌

Results for commit 83e8109.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@rickard-green rickard-green added the team:PS Assigned to OTP team PS label Oct 20, 2025
@IngelaAndin IngelaAndin added the team:VM Assigned to OTP team VM label Oct 21, 2025
@Maria-12648430
Copy link
Contributor

@michalmuskala why only gen_servers, ie why not also gen_statems and gen_events?

There is one potential problem with doing it this way. Very contrived, though, so I'm just pointing it out.
What may happen is this:

  1. a gen_server process A gets started with a registered name
  2. the process is unregistered while it is running, either by itself or by another process
  3. another gen_server (or other even) process B gets started registered (or registered while it is running) with the same name
  4. process A terminates and thereby unregisters the name it was started with but no longer "owns", actually unregistering process B

As I said, this is contrived, but we don't know what exists out there in the wild. AFAICT, there is nothing in the documentation saying that sth like this should not be done.

@michalmuskala
Copy link
Contributor Author

I agree this should be done for all standard behaviours. I published this as a sort-of proposal, happy to handle the other behaviours, if the OTP team agrees this is a good idea in principle.

@josevalim
Copy link
Contributor

@michalmuskala should the registry instead check if the process is alive before returning it? That’s what we do for Elixir’s Registry.

@michalmuskala
Copy link
Contributor Author

Does it also check that on register commands and then just clears the registration to avoid returning already_registered error?

@josevalim
Copy link
Contributor

Yes, all operations that lookup in the registry check if it is alive before returning, although it is a local registry. Unsure about the trade-offs in a distributed one.

@michalmuskala
Copy link
Contributor Author

I agree this is a better approach, I'm going to close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team:PS Assigned to OTP team PS team:VM Assigned to OTP team VM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants