Skip to content

Commit 169239d

Browse files
author
Alan Christie
committed
fix: Attempt to handle WebSocketDisconnect
1 parent 5d4844c commit 169239d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

app/app.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import aio_pika
1111
import shortuuid
12-
from fastapi import FastAPI, HTTPException, WebSocket, status
12+
from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect, status
1313
from pydantic import BaseModel
1414

1515
# Configure logging
@@ -176,7 +176,11 @@ async def event_stream(websocket: WebSocket, uuid: str):
176176
_LOGGER.info("Taking POISON for %s (%s) (closing)...", es_id, uuid)
177177
_running = False
178178
else:
179-
await websocket.send_text(str(message_body))
179+
try:
180+
await websocket.send_text(str(message_body))
181+
except WebSocketDisconnect:
182+
_LOGGER.warning("Got WebSocketDisconnect for %s (%s) (closing)...")
183+
_running = False
180184

181185
_LOGGER.info("Closing %s (uuid=%s)...", es_id, uuid)
182186
await websocket.close(

0 commit comments

Comments
 (0)