Skip to content

fix(realtime): update channel join payloads in set_auth - #1656

Merged
o-santi merged 1 commit into
supabase:mainfrom
hsusul:fix/realtime-set-auth-join-payload
Oct 15, 2026
Merged

o-santi merged 1 commit into
supabase:mainfrom
hsusul:fix/realtime-set-auth-join-payload

Conversation

@hsusul

@hsusul hsusul commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix.

What is the current behavior?

Fixes #1655

AsyncRealtimeClient.set_auth(token) pushes access_token to joined channels but leaves each channel's join payload alone:

for _, channel in self.channels.items():
    if channel._joined_once and channel.is_joined:
        await channel.push(ChannelEvents.access_token, {"access_token": token})

The join payload keeps the token captured in subscribe(). When a channel rejoins (_reconnect() → channel._rejoin() → join_push.resend()), it sends that original token.

supabase calls realtime.set_auth(...) on every TOKEN_REFRESHED, and user JWTs expire after an hour by default. Once a connection drops after that, the rejoin carries an expired JWT. The realtime server rejects the join (InvalidJWTToken: Token has expired), and the channel keeps retrying with the same stale token, so private channels and RLS-filtered postgres_changes stop delivering events. Channels that are still joining or errored when set_auth runs never get the new token at all.

What is the new behavior?

set_auth also stores the token in every channel's join payload (join_push.update_payload({"access_token": token})), so rejoins use the current token. Joined channels still get the access_token push, as before.

This matches realtime-js, which calls channel.updateJoinPayload({ access_token }) for every channel in setAuth in addition to the push. A None token is stored as-is, like realtime-js does; the server treats a missing join token as the tenant key.

The sync realtime client is a NotImplementedError stub, so there's no sync counterpart.

Tests

test_set_auth_updates_join_payload_used_on_rejoin in src/realtime/tests/test_connection.py follows the existing mocked-WebSocket tests in that file. It:

  1. subscribes a channel and marks it joined;
  2. calls set_auth("refreshed-token") and checks the access_token push (existing behavior);
  3. calls channel._rejoin() the way _reconnect() does, and asserts the phx_join payload carries refreshed-token.

It fails on main (the rejoin sends the original key) and passes here. It doesn't need the realtime server.

Validation

  • New test: 1 passed (1 failed with the upstream client.py).
  • uv run --package realtime mypy src/realtime/src/realtime src/realtime/tests: no issues in 21 source files.
  • uv run ruff check --fix / uv run ruff format: clean.
  • git diff --check: clean.

The rest of src/realtime/tests needs the local Supabase stack (make realtime.start-infra: Docker + Supabase CLI), which wasn't running. With this change the full suite gives 26 passed and 6 failed. The 6 failures are test_broadcast_events, test_postgrest_changes, test_postgrest_changes_on_different_tables, test_multiple_connect_attempts, test_send_message_reconnection and test_presence, and all of them are connection-refused errors. Without the fix, those same 6 fail plus the new test.

set_auth pushed access_token to joined channels but left each channel's
join payload holding the token captured at subscribe() time. Rejoins
resend that payload, so after a dropped connection a channel rejoined
with the original, possibly expired, JWT and the server rejected it.
Channels that were still joining or errored never got the new token.

Store the token in every channel's join payload, as realtime-js does in
setAuth, while keeping the access_token push for joined channels.

Fixes supabase#1655
@o-santi
o-santi force-pushed the fix/realtime-set-auth-join-payload branch from c02fdf7 to f45693d Compare October 15, 2026 17:52
@o-santi
o-santi merged commit a2a142e into supabase:main Oct 15, 2026
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.

Realtime set_auth doesn't update the join payload, so rejoins send the old token

2 participants