Repository navigation
Description
Describe the bug
After a successful subscription, subscribe(callback) does not report subsequent channel errors or closure. A valid server phx_error changes the channel to ERRORED, and phx_close changes it to CLOSED, but the callback only receives the earlier SUBSCRIBED event.
An application using the documented subscription-state callback cannot update its UI or react to those later lifecycle events. The public RealtimeSubscribeStates already exposes CHANNEL_ERROR and CLOSED.
Reproduction
This requires no Supabase service. The local server acknowledges the join, sends phx_close, then an ordered barrier broadcast on another channel.
import asyncio
import json
from websockets.asyncio.server import serve
from realtime import AsyncRealtimeClient
from realtime.types import RealtimeSubscribeStates
async def main():
advance, processed = asyncio.Event(), asyncio.Event()
statuses = []
async def handler(ws):
async for raw in ws:
msg = json.loads(raw)
if msg["event"] != "phx_join":
continue
await ws.send(json.dumps({"event": "phx_reply", "topic": msg["topic"],
"ref": msg["ref"], "payload": {"status": "ok", "response": {"postgres_changes": []}}}))
await advance.wait()
await ws.send(json.dumps({"event": "phx_close", "topic": msg["topic"],
"ref": msg["ref"], "join_ref": msg["join_ref"], "payload": {}}))
await ws.send(json.dumps({"event": "broadcast", "topic": "realtime:barrier",
"ref": None, "payload": {"event": "processed", "payload": {}}}))
async with serve(handler, "127.0.0.1", 0) as server:
port = server.sockets[0].getsockname()[1]
client = AsyncRealtimeClient(f"ws://127.0.0.1:{port}", auto_reconnect=False)
client.channel("barrier").on_broadcast("processed", lambda _: processed.set())
channel = client.channel("room")
def on_status(status, error):
statuses.append(status)
advance.set()
try:
await channel.subscribe(on_status)
await asyncio.wait_for(processed.wait(), 2)
print("callbacks:", [status.value for status in statuses])
assert statuses == [RealtimeSubscribeStates.SUBSCRIBED, RealtimeSubscribeStates.CLOSED]
finally:
await client.close()
asyncio.run(main())Actual result is channel state CLOSED and callback statuses [SUBSCRIBED]. Expected result is [SUBSCRIBED, CLOSED]. For phx_error, expected result is [SUBSCRIBED, CHANNEL_ERROR]. Explicit unsubscribe should also notify CLOSED once.
I reproduced both server-event cases against a real localhost WebSocket server, with an initial SUBSCRIBED positive control and an ordered broadcast barrier on a separate channel. The handlers run and update state, which distinguishes this from legacy realtime-py #344's handler-registration bug. The same reproduction passes after retaining the existing subscription callback and notifying it from the existing handlers.
The callback is currently captured only by join reply closures. The existing subscribe_callback field is never assigned. Initial acknowledgement timeout work in #1386/#1394 addresses a separate path.
Library affected
realtime 2.33.0 with websockets 15.0.1, upstream main 901f00d8bb908410c9a4707c30cde6f834008329.
Python version
Python 3.13.5. Focused wire regressions also pass on Python 3.9.18.
Activity
test(realtime): reproduce missing subscription statuses
fix(realtime): notify subscribers of channel errors and closure
fix(realtime): notify subscribers of channel errors and closure (#1846)