Skip to content

fix: omit members of disconnected nodes from presence_state - #2419

Open
hsusul wants to merge 2 commits into
supabase:mainfrom
hsusul:fix/presence-state-down-node
Open

hsusul wants to merge 2 commits into
supabase:mainfrom
hsusul:fix/presence-state-down-node

Conversation

@hsusul

@hsusul hsusul commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • presence_state now leaves out members tracked by a node that is no longer connected, while keeping the dirty ETS read from fix: dirty_list Presence #558
  • adds a distributed test that tracks a member on a peer node, stops the peer, waits for the tracker's leave, and checks the next presence_state

Why

When a node leaves the cluster, Phoenix.Tracker sends leaves for its members but keeps their rows in each shard's ETS table until permdown_period passes (20 minutes by default). Presence.list/1 filters down replicas out. PresenceHandler builds presence_state from Shard.dirty_list/2, which calls State.tracked_values(shard, topic, []) with no down replicas, so it doesn't.

Clients that were already joined drop the departed members when the leave arrives, about 30s after the node goes away. Clients that join later still get them in presence_state until permdown. With the default random presence key, a user who reconnects to another node shows up twice to new joiners. This happens whenever a node leaves and doesn't come back under the same name, for example a machine replaced in a deploy or a scale-down.

The fix reads the shard table with :ets.select and drops members whose node(pid) isn't in [node() | Node.list()]. The tracked pid is always the channel process on its own node, so the pid's node is the replica's node.

Validation

  • without the fix, the new test fails after the leave: left: %{"remote" => %{"metas" => [...]}}, right: %{}. The assertion before the peer stops, that "remote" is present, passes either way, so the test also guards against filtering out live members
  • with the fix, the new test passes (about 33s, most of it the tracker's down period)
  • test/realtime_web/channels/realtime_channel/presence_handler_test.exs with the new file: 71 passed
  • test/integration/rt_channel/presence_test.exs and test/integration/distributed_realtime_channel_test.exs: 46 passed
  • mix format --check-formatted, MIX_ENV=test mix credo --strict on the changed files, git diff --check
  • mix compile --force --warnings-as-errors, MIX_ENV=test mix compile --warnings-as-errors and mix format --check-formatted with the CI-pinned Elixir 1.19.5 / OTP 28 (elixir:1.19.5-otp-28 container)

I didn't run the full suite or dialyzer locally.

@seangeo

seangeo commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Hi @hsusul, we're actually doing a bunch of work on presence at the moment, so this PR is probably chasing a moving target for a bit. On this issue in particular though, the threshold for a waiting for the perm down is configurable, which I think is a better solution that leaking Phoenix Presence internals out into Realtime. But like I said, lots is changing in presence at the moment, so it might be worth waiting for that to settle in the next couple months.

Thanks!

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants