Skip to content

prefect.server.utilities.subscriptions

still_connected(websocket) async

Checks that a client websocket still seems to be connected during a period where the server is expected to be sending messages.

Source code in src/prefect/server/utilities/subscriptions.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
async def still_connected(websocket: WebSocket) -> bool:
    """Checks that a client websocket still seems to be connected during a period where
    the server is expected to be sending messages."""
    try:
        await asyncio.wait_for(websocket.receive(), timeout=0.1)
        return True  # this should never happen, but if it does, we're still connected
    except asyncio.TimeoutError:
        # The fact that we timed out rather than getting another kind of error
        # here means we're still connected to our client, so we can continue to send
        # events.
        return True
    except RuntimeError:
        # starlette raises this if we test a client that's disconnected
        return False
    except NORMAL_DISCONNECT_EXCEPTIONS:
        return False