Repository navigation
fix(realtime): ammend reconnect logic to not unsubscribe - #1346
Merged
Merged
Conversation
grdsdev
approved these changes
Jan 7, 2026
Pull Request Test Coverage Report for Build 20796463304Details
💛 - Coveralls |
edgurgel
approved these changes
Jan 8, 2026
|
Reviewed the reconnect logic change. |
This was referenced Jan 8, 2026
This was referenced Feb 10, 2026
zeekay
pushed a commit
to Dream-AI-4444/supabase-py
that referenced
this pull request
Sep 10, 2026
spydon
added a commit
that referenced
this pull request
Nov 3, 2026
…1675) Fixes #1299 #1299 got closed after #1346, but the "realtime goes quiet and never comes back" part still happens on main. The log in the last comment there is the giveaway: ws state CLOSED, close code 1001, channel still JOINED. What's going on: when the server (or a proxy in front of Realtime) closes the socket with 1000 or 1001, websockets just ends the `async for msg in ws` loop instead of raising. `_listen` only handled `ConnectionClosedError`, so it returned quietly. `_ws_connection` stayed set, `is_connected` kept saying True, and the channel stayed JOINED. Heartbeats then hit `ConnectionClosedOK`, which gets swallowed, so nothing ever reconnected. Close codes like 1012 already worked because they raise. The fix: - if the listen loop ends normally and the socket is still the current one, treat it as a server close and reconnect (only when `auto_reconnect` is on), same as the error path - `close()` now clears `_ws_connection` before closing, so a close we started ourselves doesn't look like a server close and trigger a reconnect I reproduced it against the local Realtime from `infra/` with a tiny relay in front that closes the client with 1001: ``` before: ws.state=CLOSED ws.close_code=1001 | is_connected=True channel.state=JOINED | relay connections so far=1 received total: ['before-close'] -> STUCK after: ws.state=OPEN ws.close_code=None | is_connected=True channel.state=JOINED | relay connections so far=2 received total: ['before-close', 'after-close'] -> RECOVERED ``` I added tests for 1000 and 1001 (reconnect and rejoin), and one that checks `close()` doesn't reconnect. Both close-code tests fail on main, and the `close()` test fails without the reorder. The full realtime suite passes locally (34 passed), and ruff and mypy are clean. --------- Co-authored-by: Lukas Klingsbo <[email protected]>
spydon
added a commit
that referenced
this pull request
Nov 3, 2026
Resolves SDK-2676. ## What Removes `AsyncRealtimeClient._leave_open_topic()`. Nothing called it, so coverage always reported it as uncovered. Also removes the `AsyncMock` stub for it in `tests/test_presence.py`. ## Why delete instead of wiring it up - **realtime-js never called it from `channel()`.** In realtime-js v2 (and in phoenix.js, where it comes from), the only caller of `_leaveOpenTopic` was `RealtimeChannel._rejoin()`. `channel()` returned the existing channel for the topic. realtime-js removed it in supabase/supabase-js#2119 when it switched to phoenix's JS client. - **Python already removed that same caller on purpose.** `git log -S _leave_open_topic` shows that cfbe594 (#1346, "ammend reconnect logic to not unsubscribe") replaced the call in `_rejoin()` with a log line. During a reconnect the helper found the rejoining channel itself and unsubscribed it. The method has been dead since then. Its only other appearance is the test stub added in 6be6c0c. - **It can't find a duplicate.** `self.channels` is a dict keyed by topic, so the helper could only ever match `self.channels[topic]`, which is the channel being joined. When `channel()` replaces a leaving channel, the old instance is dropped from the dict and is already leaving, not joined or joining. `_remove_channel()` only deletes the entry if it is still the same instance, so the replacement is safe without this helper. ## Testing - `git grep leave_open` returns nothing. - `make ruff` is clean. - With realtime infra running, `uv run --frozen --package realtime pytest tests` gives 116 passed, including `test_channel_rejoin.py`, `test_subscription_status.py` and `test_presence.py`. - `uv run --frozen --package realtime mypy src/realtime tests` reports no issues. ## Review I reviewed the diff with `/code-review` (high effort). It found no issues, so nothing changed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Fixes #1311. The core of the problem is explained in the issue thread, but to summarize:
_rejoinfunction that is called on all existing channels, which resubscribes by re-sending the same payload as the first time._rejoinfirst tries tounsubscribeeach channel before re-sending the originaljoin_push, andunsubscriberemoves the channel from thechannelsfield, and only after that does it send thejoin_push.phx_reply) is completly ignored, leaving it in aCLOSEDstate forever.The fix
Given that the websocket connection is new, we can just ignore the
unsubscribecall and just re-send thejoin_pushagain. This way, the external channel references still remain valid, and the state is correctly set.As a bonus, I've added a state change when the reconnect tries to rejoin to signal that an error occured.