Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: disconnect from consumer if there is a stale connection (#1301)

Myles Scolnick2a4bd92ff38c
diff --git a/marimo/_server/api/endpoints/ws.py b/marimo/_server/api/endpoints/ws.py
index 9f54031a52..fd0eab45d1 100644
--- a/marimo/_server/api/endpoints/ws.py
+++ b/marimo/_server/api/endpoints/ws.py
@@ -257,6 +257,8 @@ class WebsocketHandler(SessionConsumer):
existing_session = mgr.get_session(session_id)
if existing_session is not None:
LOGGER.debug("Reconnecting session %s", session_id)
+ # In case there is a lingering connection, close it
+ existing_session.maybe_disconnect_consumer()
await self._reconnect_session(existing_session, replay=False)
return existing_session
diff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.py
index 5c31f992df..84765a1500 100644
--- a/marimo/_server/sessions.py
+++ b/marimo/_server/sessions.py
@@ -364,6 +364,10 @@ class Session:
self.unsubscribe_consumer()
self.session_consumer = None
+ def maybe_disconnect_consumer(self) -> None:
+ if self.session_consumer is not None:
+ self.disconnect_consumer()
+
def connect_consumer(self, session_consumer: SessionConsumer) -> None:
"""Connect or resume the session with a new consumer"""
assert (