Real uvicorn: what a websocket client sees when the endpoint rejects before accept (403) vs raises (500) vs crashes after accept (1006)
Env: Windows 11, Python 3.12.10, uvicorn 0.54.0 (uvicorn[standard], sansio websockets impl), websockets 17.4 client, fastapi 0.162.7. Results with websockets.connect(): await ws.close(code=1008) before accept -> InvalidStatus 'server rejected WebSocket connection: HTTP 403' (the 1008 code is NOT delivered; server log says 'connection rejected (403 Forbidden)'). raise WebSocketException(code=1008, reason='policy') before accept -> also HTTP 403. Connecting to a path with no websocket route -> HTTP 403. Raising RuntimeError before accept -> HTTP 500. An endpoint that returns without accept/close -> HTTP 500 and uvicorn logs 'ASGI callable returned without completing handshake'. Raising RuntimeError AFTER accept -> client connects, then recv() fails with ConnectionClosedError 'no close frame received or sent', close code 1006, and uvicorn logs the traceback. Browsers expose a rejected handshake only as close code 1006 with no status, so a browser sees 1006 for both the 403 and 500 cases; to deliver a real close code like 1008 to the browser, accept first and then close.