Skip to content

fix(realtime): build a fresh channel while the old one is leaving - #2908

Open
shoemoney wants to merge 1 commit into
supabase:masterfrom
shoemoney:fix/realtime-channel-reuse
Open

shoemoney wants to merge 1 commit into
supabase:masterfrom
shoemoney:fix/realtime-channel-reuse

Conversation

@shoemoney

Copy link
Copy Markdown

Addresses points 1 and the first half of 3 from #2896. Reference counting is left out, since it changes removeChannel semantics and is a maintainer call.

What changes

removeChannel drops the channel right away. It used to stay in channels until the server acknowledged the leave, so a channel(topic) call during that round trip got back the channel being torn down. A React StrictMode remount does exactly this. The leave is now sent first, then the channel is removed from channels, so the next channel(topic) builds a fresh one.

_remove filters by identity, not topic. Without this, the old channel's close hook fires after the new one exists and removes it, because they share a topic.

channel() warns on mismatched params. When it returns an existing channel and the passed config differs from that channel's config, it logs a console.warn instead of dropping the new params silently. Configs are compared with every default filled in, including inside broadcast and presence, so { broadcast: { self: false } } matches the default. Calling with no params, or with { config: {} } (which is what SupabaseClient.channel passes when the caller gives none), never warns.

Leave errors. The existing behaviour is kept: if the server answers the leave with error, the channel goes back into channels. The one exception is when a channel for the same topic was created in the meantime. Then the old one is torn down, so the two don't both receive the topic's messages.

The empty-channels deferred disconnect is unchanged: the close hook still calls _remove, which schedules it when nothing is left.

Tests

Three new tests in RealtimeClient.channels.test.ts cover a fresh channel during the leave, a warning on different params, and no warning for equivalent, omitted or empty params. The realtime-js suite passes (512 passed, 1 skipped), and nx build passes for realtime-js and supabase-js.

The unhandled error that RealtimeChannel.messaging.test.ts sometimes reports ("expected 'closed' to be 'joined'") happens on master too, in 2 of 4 runs here, and has nothing to do with this change.

removeChannel kept the channel in `channels` until the server acknowledged
the leave, so a channel(topic) call during that round trip (a React
StrictMode remount does this) got back the channel being torn down. It is
now dropped as soon as the leave is sent, and _remove filters by identity
so the old channel's close hook cannot remove its same-topic replacement.

channel() also warns when it returns an existing channel whose params
differ from the ones passed, instead of ignoring them silently.

Refs supabase#2896
@shoemoney
shoemoney requested review from a team as code owners November 25, 2026 04:13
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • Channel creation now supports omitted configuration parameters, applying default settings automatically.
  • Bug Fixes
    • Removing a channel no longer risks removing a newer channel created for the same topic while the original is being removed.
    • Requests for an existing topic continue to use its original channel settings. A warning appears when supplied settings differ; equivalent settings do not trigger a warning.

The change exports a helper that applies defaults to channel configuration and uses it in the channel constructor and when comparing requested parameters with an existing channel. When parameters differ, the client retains the existing channel and logs a warning. Channel removal now removes the exact instance before unsubscribe and restores it after an error only if no same-topic channel exists. Cleanup removes channels by instance identity.

Possibly related issues

Possibly related PRs

  • supabase/supabase-js#2803: Both changes remove channels by object identity to prevent stale cleanup from removing a same-topic replacement.

Suggested labels: realtime-js

Change: Bug fix

Merge Risk: Minimal · up to ba09f

This change lets a same-topic channel be created while the old one is leaving, and it warns when a reused channel is requested with different params. No concrete merge-blocking risk was identified.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1671f0b4-4821-4ef9-8c38-076a0fc223be
Commits

Reviewing files that changed from the base of the PR and between 840843d and ba09f81.

Files selected for processing: 3

  • packages/core/realtime-js/src/RealtimeChannel.ts
  • packages/core/realtime-js/src/RealtimeClient.ts
  • packages/core/realtime-js/test/RealtimeClient.channels.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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.

1 participant