Skip to content

WebSocket: RuntimeError “ASGI flow error” on receive #110

@martastain

Description

@martastain

Problem

When using the WebSocket the server sometimes crashes with RuntimeError: ASGI flow error

Traceback points to await client.receive() in ws_endpoint backend/server/websocket.py:57
This usually happens if the client disconnects (or the connection is otherwise closed) while the server is still awaiting receive(). Instead of being handled gracefully, the exception bubbles up and kills the request handler.

Expected behavior

Disconnected clients should raise a clean WebSocketDisconnect (or similar handled exception), not an unhandled RuntimeError.
Server should log the disconnect and exit the loop gracefully without stack traces.

Proposed solution

Wrap WebSocket receive loop in try/except for WebSocketDisconnect and RuntimeError.

Alternatively, update client.receive() wrapper (backend/server/websocket.py:57) to catch RuntimeError and re-raise a WebSocketDisconnect so calling code doesn’t need to know about Starlette internals.

Image

Metadata

Metadata

Assignees

Labels

backendRequires changing the Python code

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions