Skip to content

Commit

Permalink
fix: do not ignore all RuntimeErrors, only for websocket closed.
Browse files Browse the repository at this point in the history
A runtime error also occurs when a send is triggered from the same
event loop thread.
  • Loading branch information
maartenbreddels committed Dec 24, 2024
1 parent e1ae701 commit 1f6d98e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions solara/server/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,21 @@ async def _send_bytes_exc(self, data: bytes):
await self.ws.send_bytes(data)
except (websockets.exceptions.ConnectionClosed, starlette.websockets.WebSocketDisconnect, RuntimeError) as e:
# starlette throws a RuntimeError once you call send after the connection is closed
raise websocket.WebSocketDisconnect() from e
if isinstance(e, RuntimeError) and "close message" in repr(e):
raise websocket.WebSocketDisconnect() from e
else:
raise

async def _send_text_exc(self, data: str):
# make sures we catch the starlette/websockets specific exception
# and re-raise it as a websocket.WebSocketDisconnect
try:
await self.ws.send_text(data)
except (websockets.exceptions.ConnectionClosed, starlette.websockets.WebSocketDisconnect, RuntimeError) as e:
# starlette throws a RuntimeError once you call send after the connection is closed
raise websocket.WebSocketDisconnect() from e
if isinstance(e, RuntimeError) and "close message" in repr(e):
raise websocket.WebSocketDisconnect() from e
else:
raise

def close(self):
if self.portal is None:
Expand Down

0 comments on commit 1f6d98e

Please sign in to comment.