Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: a failing websocket.send would drop the connection silently #919

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

maartenbreddels
Copy link
Contributor

Instead of assuming all exceptions are due to a closed connection, we only ignored the exception when it is due to a closed connection.

This caused issues in #841 where we called from the same thread as the websocket's portal, which caused the connection to be ignored without any error message.

Instead of assuming all exceptions are due to a closed connection,
we only ignored the exception when it is due to a closed connection.

This caused issues in #841 where we called from the same thread as the
websocket's portal, which caused the connection to be ignored without
any error message.
@maartenbreddels maartenbreddels temporarily deployed to fix_silent_error_on_websocket_send_fail - solara-stable PR #919 December 12, 2024 11:37 — with Render Destroyed
@maartenbreddels maartenbreddels marked this pull request as ready for review December 12, 2024 11:37
Copy link
Collaborator

@iisakkirotko iisakkirotko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Only one comment about the implementation of the wrapper methods, but if it let's us get this in faster, we can merge as is 👍

Comment on lines +129 to +138
async def _send_bytes_exc(self, data: bytes):
# make sures we catch the starlette/websockets specific exception
# and re-raise it as a websocket.WebSocketDisconnect
try:
await self.ws.send_bytes(data)
except websockets.exceptions.ConnectionClosed as e:
raise websocket.WebSocketDisconnect() from e
except RuntimeError as e:
# starlette throws a RuntimeError once you call send after the connection is closed
raise websocket.WebSocketDisconnect() from e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be implemented as a decorator / context manager to not define two almost identical functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in private, it's DRY (don't repeat yourself) vs YAGNI (you ain't gonna need it). It would make it more difficult to read, vs it makes it more difficult to maintain.
In this particular case, we probably should have overwritten the send(..) method and should have pushed the branching down.

@maartenbreddels maartenbreddels merged commit 51cbfa9 into master Dec 12, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants