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: race-condition when connecting to websocket sometimes (#1377)

Myles Scolnickb3d4e126e290
diff --git a/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx b/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx
index 2ccb76b370..01b62d0d09 100644
--- a/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx
@@ -1,4 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
+import { Spinner } from "@/components/icons/spinner";
import { Tooltip } from "@/components/ui/tooltip";
import { connectionAtom } from "@/core/network/connection";
import { getUsageStats } from "@/core/network/requests";
@@ -8,7 +9,13 @@ import { WebSocketState } from "@/core/websocket/types";
import { useAsyncData } from "@/hooks/useAsyncData";
import { useInterval } from "@/hooks/useInterval";
import { useAtomValue } from "jotai";
-import { CpuIcon, MemoryStickIcon } from "lucide-react";
+import { startCase } from "lodash-es";
+import {
+ CheckCircle2Icon,
+ CpuIcon,
+ MemoryStickIcon,
+ PowerOffIcon,
+} from "lucide-react";
import React, { useState } from "react";
export const MachineStats: React.FC = (props) => {
@@ -30,18 +37,36 @@ export const MachineStats: React.FC = (props) => {
return getUsageStats();
}, [nonce, connection.state]);
- if (!data) {
- return;
- }
-
return (
<div className="flex gap-3">
- <MemoryUsageBar memory={data.memory} />
- <CPUBar cpu={data.cpu} />
+ {data && <MemoryUsageBar memory={data.memory} />}
+ {data && <CPUBar cpu={data.cpu} />}
+ <BackendConnection connection={connection.state} />
</div>
);
};
+const BackendConnection: React.FC<{ connection: WebSocketState }> = ({
+ connection,
+}) => {
+ return (
+ <Tooltip delayDuration={200} content={startCase(connection.toLowerCase())}>
+ <div>
+ {connection === WebSocketState.OPEN && (
+ <CheckCircle2Icon className="w-4 h-4" />
+ )}
+ {connection === WebSocketState.CLOSED && (
+ <PowerOffIcon className="w-4 h-4" />
+ )}
+ {connection === WebSocketState.CONNECTING && <Spinner size="small" />}
+ {connection === WebSocketState.CLOSING && (
+ <Spinner className="text-destructive" size="small" />
+ )}
+ </div>
+ </Tooltip>
+ );
+};
+
const MemoryUsageBar: React.FC<{ memory: UsageResponse["memory"] }> = ({
memory,
}) => {
diff --git a/frontend/src/core/websocket/types.ts b/frontend/src/core/websocket/types.ts
index eafead236f..c9f87ae60c 100644
--- a/frontend/src/core/websocket/types.ts
+++ b/frontend/src/core/websocket/types.ts
@@ -3,10 +3,10 @@
import ReconnectingWebSocket from "partysocket/ws";
export enum WebSocketState {
- CONNECTING = 0,
- OPEN = 1,
- CLOSING = 2,
- CLOSED = 3,
+ CONNECTING = "CONNECTING",
+ OPEN = "CONNECTED",
+ CLOSING = "CLOSING",
+ CLOSED = "CLOSED",
}
export enum WebSocketClosedReason {
diff --git a/frontend/src/core/websocket/useWebSocket.tsx b/frontend/src/core/websocket/useWebSocket.tsx
index 6bae0e1246..b743ac319c 100644
--- a/frontend/src/core/websocket/useWebSocket.tsx
+++ b/frontend/src/core/websocket/useWebSocket.tsx
@@ -35,15 +35,15 @@ export function useWebSocket(options: UseWebSocketOptions) {
debug: false,
});
+ onOpen && socket.addEventListener("open", onOpen);
+ onClose && socket.addEventListener("close", onClose);
+ onError && socket.addEventListener("error", onError);
+ onMessage && socket.addEventListener("message", onMessage);
+
return socket;
});
useEffect(() => {
- onOpen && ws.addEventListener("open", onOpen);
- onClose && ws.addEventListener("close", onClose);
- onError && ws.addEventListener("error", onError);
- onMessage && ws.addEventListener("message", onMessage);
-
return () => {
onOpen && ws.removeEventListener("open", onOpen);
onClose && ws.removeEventListener("close", onClose);