-
First Check
Commit to Help
Example Codeimport asyncio
import logging
import fastapi
import uvicorn
APP = fastapi.FastAPI()
@APP.websocket("/ws")
async def ws_telemetry(websocket: fastapi.WebSocket) -> None:
await websocket.accept()
try:
while True:
print("Still Looping...")
await asyncio.sleep(1)
except fastapi.WebSocketDisconnect:
logging.info("Websocket disconnected")
if __name__ == "__main__":
uvicorn.run("websocket_duplicator:APP", host="127.0.0.1", port=8080, reload=True)DescriptionWhen running FastAPI with a websocket, the WebSocketDisconnect error isn't raised per the documentation. Using the given reproducer code and Postman's websocket function, I get the following output: It seems to me that this functionality has broken as similar code worked with FastAPI 0.95.2 however, I found a Starlette discussion which makes me think the behavior is, for some reason, intentional. My question is: is the WebSocketDisconnect exception supposed to be raised when a client disconnects or is the feature broken? Operating SystemLinux Operating System DetailsNo response FastAPI Version0.109.0 Pydantic Version2.5.3 Python Version3.10 Additional Contextstarlette==0.35.1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Your snippet is unrealistic. The Note: the next release of Uvicorn/Starlette will raise |
Beta Was this translation helpful? Give feedback.
Your snippet is unrealistic. The
WebSocketDisconnectonly raises onwebsocket.receive().Note: the next release of Uvicorn/Starlette will raise
WebSocketDisconnectonsend()as well.