Skip to content

fix(realtime): return the existing channel for a topic from channel() - #1673

Merged
o-santi merged 1 commit into
supabase:mainfrom
breken-ai:fix/realtime-reuse-channel-for-topic
Oct 15, 2026
Merged

o-santi merged 1 commit into
supabase:mainfrom
breken-ai:fix/realtime-reuse-channel-for-topic

Conversation

@breken-ai

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix (realtime).

What is the current behavior?

AsyncRealtimeClient.channel(topic) always creates a new AsyncRealtimeChannel and stores it with self.channels[topic] = chan. If a channel for that topic already exists, the new one replaces it.

_listen routes every incoming message by topic (self.channels.get(message.topic)). So after a second channel("room") call, broadcasts, presence and postgres changes for realtime:room go to the new channel, which is not subscribed and has no callbacks. The first channel is still joined on the server, but its callbacks never fire again, and nothing reports an error. Its later leave reply is routed to the new channel too, so the first channel also stays in LEAVING.

Code ported from supabase-js is where this shows up. There, supabase.channel('room') returns the existing channel for the topic, so calling it again, for example to send_broadcast from a handler, is a common pattern.

Reproduction on main, with the socket's websocket mocked and a real broadcast frame fed through _listen:

channel = socket.channel("room").on_broadcast("ping", lambda p: received.append(p["payload"]))
await channel.subscribe()
socket.channel("room")
# server sends {"topic": "realtime:room", "event": "broadcast", "payload": {"type": "broadcast", "event": "ping", "payload": {"n": 1}}, "ref": null}
await socket._listen()
assert received == [{"n": 1}]   # main: received == []

What is the new behavior?

channel() returns the existing channel for the topic, unless that channel is leaving. This matches realtime-js RealtimeClient.channel(), which returns the channel it finds for the topic. After unsubscribe() (state LEAVING), a new channel is still created, as before, so the unsubscribe-and-recreate flow is unchanged.

Additional context

New tests in tests/test_connection.py (mocked websocket, no server needed):

  • test_channel_returns_existing_channel_for_same_topic: on main, a second channel("room") returns a different object.
  • test_second_channel_call_keeps_first_subscription_receiving: on main, received == []. On this branch, it is [{"n": 1}].
  • test_channel_after_unsubscribe_creates_new_channel: passes before and after. It guards the recreate-after-unsubscribe flow.

Results:

  • On main, the first two tests fail. On this branch, all three pass.
  • Full src/realtime/tests run without a local Supabase: the only failures are the 6 tests that need the server at 127.0.0.1:54321. They fail the same way on main.
  • ruff check, ruff format --check and mypy src/realtime tests are clean.

I searched the open issues and PRs (topic, channel twice, duplicate channel) and found no overlap. #1652 and #1656 touch other parts of client.py.

This PR was prepared by an AI coding agent (Claude Code) working from the breken-ai account. The tests above were run as described.

🤖 Generated with Claude Code

channel() always built a new AsyncRealtimeChannel and stored it in
socket.channels under its topic, replacing any channel already there.
Incoming messages are routed by topic, so a second channel("room") call
silently stopped delivering broadcasts, presence and postgres changes to
the first, already subscribed channel.

Return the existing channel for the topic unless it is being left,
matching realtime-js.
@breken-ai
breken-ai requested review from a team and o-santi as code owners September 26, 2026 03:52
@o-santi
o-santi merged commit df4ed51 into supabase:main Oct 15, 2026
37 of 43 checks passed
spydon pushed a commit to hsusul/supabase-py that referenced this pull request Nov 2, 2026
Since supabase#1673, channel() registers a new channel when the one for that
topic is leaving. The old channel's leave reply or timeout arrives
later and calls _remove_channel(), which deleted whatever channel held
the topic. That was the replacement, so its messages were dropped.

Delete the entry only when it is the channel being removed.
spydon added a commit that referenced this pull request Nov 2, 2026
…1877)

## Why

Realtime users who unsubscribe and then subscribe to the same topic
again can lose every event on the new subscription. The old channel's
late leave reply removes the replacement channel that #1673 registers.

Closes #1876.

## What changed

- `_remove_channel()` deletes the topic entry only when it is the
channel being removed.
- Add a regression test for the late leave, and a test that closing the
current channel still removes it.

## Scope

This covers the client's channel registry. Channel join and leave
behavior is unchanged.

## Blast radius

`_remove_channel()` has two callers, `on_close()` and
`remove_channel()`. Both pass the registered channel except in the
replaced case, so their behavior is unchanged.

## Verification

- The late-leave test fails on main with an empty registry and passes
with the fix. `tests/test_channel_timeouts.py` passes 4 of 4.
- The issue's reproduction prints `False` on main and `True` on this
branch. Mypy and Ruff pass on the package.

AI-assisted contribution. Reproductions and checks were run locally.

---------

Co-authored-by: Lukas Klingsbo <[email protected]>
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