Repository navigation
fix(realtime): forget channels in remove_all_channels #1925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
spydon
merged 6 commits into
main
from
lukasklingsbo/sdk-1674-realtime-remove_all_channels-leaves-selfchannels-populated
Nov 3, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd1ee53
fix(realtime): forget channels in remove_all_channels
spydon 0e09bc1
fix(realtime): remove channels sequentially and always close the socket
spydon 10ff014
fix(realtime): keep removing channels after an unsubscribe failure
spydon 1860330
Merge branch 'main' into lukasklingsbo/sdk-1674-realtime-remove_all_c…
spydon 6eba381
fix(realtime): forget remaining channels when remove_all_channels is …
spydon e04f573
fix(realtime): stop rejoin timers of channels dropped by a cancelled …
spydon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import asyncio | ||
| import json | ||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from realtime import AsyncRealtimeClient | ||
| from realtime.types import ChannelEvents, ChannelStates | ||
|
|
||
| URL = "ws://localhost/realtime/v1" | ||
| TOPICS = ["first", "second", "third"] | ||
|
|
||
|
|
||
| class RecordingConnection: | ||
| def __init__(self, reply_to_leave: bool = False) -> None: | ||
| self.sent: list[dict] = [] | ||
| self.close = AsyncMock() | ||
| self.reply_to_leave = reply_to_leave | ||
| self.incoming: asyncio.Queue[str] = asyncio.Queue() | ||
|
|
||
| async def send(self, message: str) -> None: | ||
| data = json.loads(message) | ||
| self.sent.append(data) | ||
| if self.reply_to_leave and data["event"] == ChannelEvents.leave: | ||
| reply = { | ||
| "topic": data["topic"], | ||
| "event": ChannelEvents.reply, | ||
| "payload": {"status": "ok", "response": {}}, | ||
| "ref": data["ref"], | ||
| } | ||
| await self.incoming.put(json.dumps(reply)) | ||
| for _ in range(5): | ||
| await asyncio.sleep(0) | ||
|
|
||
| def __aiter__(self) -> "RecordingConnection": | ||
| return self | ||
|
|
||
| async def __anext__(self) -> str: | ||
| return await self.incoming.get() | ||
|
|
||
| def leaves(self) -> list[str]: | ||
| return [m["topic"] for m in self.sent if m["event"] == ChannelEvents.leave] | ||
|
|
||
|
|
||
| async def connected_socket(connection: RecordingConnection) -> AsyncRealtimeClient: | ||
| socket = AsyncRealtimeClient(URL, "token", auto_reconnect=False) | ||
| with patch( | ||
| "realtime._async.client.connect", new=AsyncMock(return_value=connection) | ||
| ): | ||
| await socket.connect() | ||
| for topic in TOPICS: | ||
| socket.channel(topic) | ||
| return socket | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_remove_all_channels_forgets_every_channel() -> None: | ||
| connection = RecordingConnection() | ||
| socket = await connected_socket(connection) | ||
|
|
||
| await socket.remove_all_channels() | ||
|
|
||
| assert socket.get_channels() == [] | ||
| assert sorted(connection.leaves()) == sorted( | ||
| f"realtime:{topic}" for topic in TOPICS | ||
| ) | ||
| connection.close.assert_awaited_once() | ||
| assert not socket.is_connected | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_channel_after_remove_all_channels_is_fresh() -> None: | ||
| connection = RecordingConnection() | ||
| socket = await connected_socket(connection) | ||
| removed = socket.get_channels()[0] | ||
|
|
||
| await socket.remove_all_channels() | ||
| fresh = socket.channel("first") | ||
|
|
||
| assert fresh is not removed | ||
| assert fresh.is_closed | ||
| assert socket.get_channels() == [fresh] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_remove_all_channels_survives_leave_replies_during_removal() -> None: | ||
| connection = RecordingConnection(reply_to_leave=True) | ||
| socket = await connected_socket(connection) | ||
|
|
||
| await socket.remove_all_channels() | ||
|
|
||
| assert socket.get_channels() == [] | ||
| assert sorted(connection.leaves()) == sorted( | ||
| f"realtime:{topic}" for topic in TOPICS | ||
| ) | ||
| connection.close.assert_awaited_once() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_remove_all_channels_continues_when_unsubscribe_fails() -> None: | ||
| connection = RecordingConnection() | ||
| socket = await connected_socket(connection) | ||
| failing = socket.get_channels()[0] | ||
|
|
||
| with ( | ||
| patch.object(failing, "unsubscribe", side_effect=ConnectionError), | ||
| pytest.raises(ConnectionError), | ||
| ): | ||
| await socket.remove_all_channels() | ||
|
|
||
| assert socket.get_channels() == [] | ||
| assert sorted(connection.leaves()) == ["realtime:second", "realtime:third"] | ||
| connection.close.assert_awaited_once() | ||
| assert not socket.is_connected | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_remove_all_channels_forgets_every_channel_when_cancelled() -> None: | ||
| connection = RecordingConnection() | ||
| socket = await connected_socket(connection) | ||
| cancelled, errored, _ = socket.get_channels() | ||
| errored.state = ChannelStates.ERRORED | ||
| errored.rejoin_timer.schedule_timeout() | ||
|
|
||
| with ( | ||
| patch.object(cancelled, "unsubscribe", side_effect=asyncio.CancelledError), | ||
| pytest.raises(asyncio.CancelledError), | ||
| ): | ||
| await socket.remove_all_channels() | ||
|
|
||
| assert socket.get_channels() == [] | ||
| assert errored.rejoin_timer.timer is None | ||
| assert errored.is_closed | ||
| connection.close.assert_awaited_once() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.