fix: correctly call websocket_endpoint from _WSInterceptMiddleware

The middleware was calling the handler as a raw ASGI callable
(scope, receive, send), but websocket_endpoint expects (WebSocket, token).
This caused an AttributeError on every connection attempt, which made the
frontend hit its 30s exponential backoff cap and spam reconnects.

Fix: construct a WebSocket object from scope/receive/send and extract the
?token= query param before calling the handler, matching what FastAPI's
routing machinery would do.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Khoa (Revenovich) Tran Gia
2026-03-03 11:47:47 +07:00
parent e83703aff0
commit 15721dd1c9

View File

@@ -88,7 +88,11 @@ class _WSInterceptMiddleware:
async def __call__(self, scope, receive, send) -> None: async def __call__(self, scope, receive, send) -> None:
if scope["type"] == "websocket" and scope.get("path") == "/ws": if scope["type"] == "websocket" and scope.get("path") == "/ws":
await self._ws(scope, receive, send) from urllib.parse import parse_qs
from starlette.websockets import WebSocket as _WS
qs = parse_qs(scope.get("query_string", b"").decode())
token = qs.get("token", [""])[0]
await self._ws(_WS(scope, receive=receive, send=send), token)
else: else:
await self._app(scope, receive, send) await self._app(scope, receive, send)