Skip to content

Realtime subscription callbacks omit later channel errors and closure #1844

Closed

Description

@hsusul

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

  1. added
    realtime
    bug
    python
    on Oct 26, 2026
  2. added a commit that references this issue on Oct 28, 2026
    bcecfc3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpythonrealtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions