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: websocket connection from connecting twice (#1456)

Myles Scolnick87ba029aea40
diff --git a/frontend/src/components/pages/edit-page.tsx b/frontend/src/components/pages/edit-page.tsx
index 46673b804f..0f51ca7a35 100644
--- a/frontend/src/components/pages/edit-page.tsx
+++ b/frontend/src/components/pages/edit-page.tsx
@@ -2,7 +2,7 @@
import { EditApp } from "@/core/edit-app";
import { AppChrome } from "../editor/chrome/wrapper/app-chrome";
import { CommandPalette } from "../editor/controls/command-palette";
-import { AppConfig, UserConfig } from "@/core/config/config-schema";
+import type { AppConfig, UserConfig } from "@/core/config/config-schema";
interface Props {
userConfig: UserConfig;
diff --git a/frontend/src/core/edit-app.tsx b/frontend/src/core/edit-app.tsx
index 3f49499424..c9c5a9a31d 100644
--- a/frontend/src/core/edit-app.tsx
+++ b/frontend/src/core/edit-app.tsx
@@ -14,7 +14,7 @@ import { FilenameForm } from "@/components/editor/header/filename-form";
import { WebSocketState } from "./websocket/types";
import { useMarimoWebSocket } from "./websocket/useMarimoWebSocket";
import {
- LastSavedNotebook,
+ type LastSavedNotebook,
notebookIsRunningAtom,
useCellActions,
useNotebook,
@@ -25,7 +25,7 @@ import {
notebookNeedsRun,
notebookNeedsSave,
} from "./cells/utils";
-import { AppConfig, UserConfig } from "./config/config-schema";
+import type { AppConfig, UserConfig } from "./config/config-schema";
import { toggleAppMode, viewStateAtom } from "./mode";
import { useHotkey } from "../hooks/useHotkey";
import { useImperativeModal } from "../components/modal/ImperativeModal";
@@ -42,7 +42,7 @@ import { useAutoSave } from "./saving/useAutoSave";
import { useEventListener } from "../hooks/useEventListener";
import { toast } from "../components/ui/use-toast";
import { SortableCellsProvider } from "../components/sort/SortableCellsProvider";
-import { CellId, HTMLCellId } from "./cells/ids";
+import { type CellId, HTMLCellId } from "./cells/ids";
import { CellArray } from "../components/editor/renderers/CellArray";
import { RuntimeState } from "./kernel/RuntimeState";
import { CellsRenderer } from "../components/editor/renderers/cells-renderer";
diff --git a/frontend/src/core/websocket/useMarimoWebSocket.tsx b/frontend/src/core/websocket/useMarimoWebSocket.tsx
index 1c7d862429..38542ad546 100644
--- a/frontend/src/core/websocket/useMarimoWebSocket.tsx
+++ b/frontend/src/core/websocket/useMarimoWebSocket.tsx
@@ -6,11 +6,11 @@ import { useWebSocket } from "@/core/websocket/useWebSocket";
import { logNever } from "@/utils/assertNever";
import { useCellActions } from "@/core/cells/cells";
import { AUTOCOMPLETER } from "@/core/codemirror/completion/Autocompleter";
-import { OperationMessage } from "@/core/kernel/messages";
-import { CellData } from "../cells/types";
+import type { OperationMessage } from "@/core/kernel/messages";
+import type { CellData } from "../cells/types";
import { useErrorBoundary } from "react-error-boundary";
import { Logger } from "@/utils/Logger";
-import { LayoutState, useLayoutActions } from "../layout/layout";
+import { type LayoutState, useLayoutActions } from "../layout/layout";
import { useVariablesActions } from "../variables/state";
import { toast } from "@/components/ui/use-toast";
import { renderHTML } from "@/plugins/core/RenderHTML";
@@ -19,7 +19,7 @@ import { prettyError } from "@/utils/errors";
import { isStaticNotebook } from "../static/static-state";
import { useRef } from "react";
import { jsonParseWithSpecialChar } from "@/utils/json/json-parser";
-import { SessionId } from "../kernel/session";
+import type { SessionId } from "../kernel/session";
import { useBannersActions } from "../errors/state";
import { useAlertActions } from "../alerts/state";
import { generateUUID } from "@/utils/uuid";
@@ -31,7 +31,7 @@ import {
handleRemoveUIElements,
} from "../kernel/handlers";
import { queryParamHandlers } from "../kernel/queryParamHandlers";
-import { JsonString } from "@/utils/json/base64";
+import type { JsonString } from "@/utils/json/base64";
/**
* WebSocket that connects to the Marimo kernel and handles incoming messages.
diff --git a/frontend/src/core/websocket/useWebSocket.tsx b/frontend/src/core/websocket/useWebSocket.tsx
index d9bc9299b1..fa529ef56f 100644
--- a/frontend/src/core/websocket/useWebSocket.tsx
+++ b/frontend/src/core/websocket/useWebSocket.tsx
@@ -16,8 +16,6 @@ interface UseWebSocketOptions {
onError?: (event: WebSocketEventMap["error"]) => void;
}
-let hasMounted = false;
-
/**
* A hook for creating a WebSocket connection with React.
*
@@ -28,10 +26,6 @@ export function useWebSocket(options: UseWebSocketOptions) {
// eslint-disable-next-line react/hook-use-state
const [ws] = useState<IReconnectingWebSocket>(() => {
- if (hasMounted) {
- Logger.warn("useWebSocket should only be called once.");
- }
- hasMounted = true;
const socket: IReconnectingWebSocket = isPyodide()
? new PyodideWebsocket(PyodideBridge.INSTANCE)
: options.static
@@ -40,6 +34,7 @@ export function useWebSocket(options: UseWebSocketOptions) {
// We don't want Infinity retries
maxRetries: 10,
debug: false,
+ startClosed: true,
});
onOpen && socket.addEventListener("open", onOpen);
@@ -51,6 +46,12 @@ export function useWebSocket(options: UseWebSocketOptions) {
});
useEffect(() => {
+ // If it's closed, reconnect
+ // This starts closed, so we need to connect for the first time
+ if (ws.readyState === WebSocket.CLOSED) {
+ ws.reconnect();
+ }
+
return () => {
Logger.warn(
"useWebSocket is unmounting. This likely means there is a bug.",
diff --git a/frontend/src/hooks/debug.ts b/frontend/src/hooks/debug.ts
index f7425d9d37..e21e66419a 100644
--- a/frontend/src/hooks/debug.ts
+++ b/frontend/src/hooks/debug.ts
@@ -16,9 +16,9 @@ export function useDebugMounting(name: string) {
const renders = useRef(0);
useEffect(() => {
- Logger.debug(`[${name}] mounted`);
+ Logger.debug(`🐛 [${name}] mounted`);
return () => {
- Logger.debug(`[${name}] unmounted. Renders: ${renders.current}`);
+ Logger.debug(`🐛 [${name}] unmounted. Renders: ${renders.current}`);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);