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: robust websocket disconnect, nonblocking usage stats #1655

Merged
merged 4 commits into from
Jun 21, 2024

Conversation

akshayka
Copy link
Contributor

@akshayka akshayka commented Jun 20, 2024

  1. When multiple notebooks are open in marimo edit, we sometimes get unexpected WebSocketDisconnects. This change makes the server more robust to those errors — one can at least continue working with notebooks that failed to connect by refreshing —  without solving the root problem.

  2. Make usage endpoint's CPU measurement nonblocking. This was slowing down the server.

Fixes #1653
Fixes #1622

However, when many notebooks are open in marimo edit, some notebooks
still initially connect but then disconnect with code 1006 (abnormal
disconnection). Frontend still thinks it's connected in these cases ...
Copy link

vercel bot commented Jun 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marimo-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 21, 2024 1:17am
marimo-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 21, 2024 1:17am

self.unsubscribe_consumer()
self.session_consumer = None
try:
self.session_consumer.on_stop()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This sometimes fails with errors deep in the guts of websockets. Finally block makes sure the consumer is removed anyway, so users can still work with the notebook by refreshing

Comment on lines +387 to +390
self._on_disconnect(
e,
cleanup_fn=lambda: listen_for_disconnect_task.cancel(),
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Abnormal disconnects can happen when sending too — and it seems like these can race with the listen_for_disconnect task?

Comment on lines +392 to +394
# Starlette can raise a runtime error if a message is sent
# when the socket is closed. In case the disconnection
# error hasn't made its way to listen_for_disconnect, do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not really sure if this case is needed. I occasionally saw RuntimeErrors raised on send_text saying the socket was already closed

@mscolnick
Copy link
Contributor

mscolnick commented Jun 20, 2024

can we wait till #1639 merges? it will simplify this and otherwise cause a few merge conflicts nvm not too overlapping

@akshayka akshayka marked this pull request as ready for review June 20, 2024 21:56
mscolnick
mscolnick previously approved these changes Jun 20, 2024
@@ -35,6 +35,9 @@ export function useWebSocket(options: UseWebSocketOptions) {
maxRetries: 10,
debug: false,
startClosed: true,
// long timeout -- the server can become slow when many notebooks
// are open.
connectionTimeout: 60_000,
Copy link
Contributor

Choose a reason for hiding this comment

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

@akshayka akshayka changed the title make websocket disconnect more robust server side fix: robust websocket disconnect, nonblocking usage stats Jun 21, 2024
@akshayka akshayka merged commit a25c26b into main Jun 21, 2024
30 checks passed
@akshayka akshayka deleted the aka/improve-websocket-disconnect branch June 21, 2024 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants