Skip to content

Commit 7e50ee7

Browse files
fix: catch starlette exceptions when websocket send fails
1 parent cffccca commit 7e50ee7

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

solara/server/starlette.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ async def _send_bytes_exc(self, data: bytes):
131131
# and re-raise it as a websocket.WebSocketDisconnect
132132
try:
133133
await self.ws.send_bytes(data)
134-
except websockets.exceptions.ConnectionClosed as e:
135-
raise websocket.WebSocketDisconnect() from e
136-
except RuntimeError as e:
134+
except (websockets.exceptions.ConnectionClosed, starlette.websockets.WebSocketDisconnect, RuntimeError) as e:
137135
# starlette throws a RuntimeError once you call send after the connection is closed
138136
raise websocket.WebSocketDisconnect() from e
139137

@@ -142,9 +140,7 @@ async def _send_text_exc(self, data: str):
142140
# and re-raise it as a websocket.WebSocketDisconnect
143141
try:
144142
await self.ws.send_text(data)
145-
except websockets.exceptions.ConnectionClosed as e:
146-
raise websocket.WebSocketDisconnect() from e
147-
except RuntimeError as e:
143+
except (websockets.exceptions.ConnectionClosed, starlette.websockets.WebSocketDisconnect, RuntimeError) as e:
148144
# starlette throws a RuntimeError once you call send after the connection is closed
149145
raise websocket.WebSocketDisconnect() from e
150146

0 commit comments

Comments
 (0)