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: dont close an already closed websocket with mo.mpl.interactive (#1861)

Myles Scolnickbbe642e96a06
diff --git a/marimo/_plugins/stateless/mpl/_mpl.py b/marimo/_plugins/stateless/mpl/_mpl.py
index 0ab2e538c1..b4bb0b2676 100644
--- a/marimo/_plugins/stateless/mpl/_mpl.py
+++ b/marimo/_plugins/stateless/mpl/_mpl.py
@@ -17,6 +17,8 @@ import time
from pathlib import Path
from typing import TYPE_CHECKING, Any, Optional, Tuple
+from starlette.websockets import WebSocketState
+
from marimo._output.builder import h
from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
@@ -142,7 +144,8 @@ def create_application(
except Exception:
pass
finally:
- await websocket.close()
+ if websocket.application_state != WebSocketState.DISCONNECTED:
+ await websocket.close()
async def send() -> None:
try:
@@ -155,7 +158,8 @@ def create_application(
except Exception:
pass
finally:
- await websocket.close()
+ if websocket.application_state != WebSocketState.DISCONNECTED:
+ await websocket.close()
await asyncio.gather(receive(), send())