@marimo-team/islands 0.23.7-dev13 → 0.23.7-dev15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/__vite-browser-external-CAdMKBac.js +1 -0
- package/dist/assets/worker-CpBbwbQo.js +73 -0
- package/dist/{code-visibility-CMSTSWYJ.js → code-visibility-jW9wAqZw.js} +1 -1
- package/dist/main.js +3 -3
- package/dist/{reveal-component-Bof-fIA6.js → reveal-component-DJF9hc8Q.js} +1 -1
- package/package.json +2 -2
- package/src/components/editor/app-container.tsx +7 -1
- package/src/components/editor/header/__tests__/status.test.tsx +108 -0
- package/src/components/editor/header/status.tsx +44 -10
- package/src/core/edit-app.tsx +2 -1
- package/src/core/islands/worker/worker.tsx +3 -2
- package/src/core/run-app.tsx +2 -1
- package/src/core/wasm/__tests__/utils.test.ts +34 -0
- package/src/core/wasm/utils.ts +14 -0
- package/src/core/wasm/worker/bootstrap.ts +3 -2
- package/src/core/wasm/worker/worker.ts +3 -2
- package/src/core/websocket/__tests__/useMarimoKernelConnection.hook.test.tsx +155 -0
- package/src/core/websocket/__tests__/useMarimoKernelConnection.test.ts +137 -0
- package/src/core/websocket/transports/basic.ts +2 -0
- package/src/core/websocket/transports/transport.ts +1 -0
- package/src/core/websocket/useMarimoKernelConnection.tsx +130 -55
- package/src/core/websocket/useWebSocket.tsx +5 -2
- package/dist/assets/__vite-browser-external-rrUYDKRl.js +0 -1
- package/dist/assets/worker-Bfy15ViQ.js +0 -73
package/src/core/run-app.tsx
CHANGED
|
@@ -38,7 +38,7 @@ export const RunApp: React.FC<AppProps> = ({ appConfig }) => {
|
|
|
38
38
|
};
|
|
39
39
|
}, []);
|
|
40
40
|
|
|
41
|
-
const { connection } = useMarimoKernelConnection({
|
|
41
|
+
const { connection, reconnect } = useMarimoKernelConnection({
|
|
42
42
|
autoInstantiate: true,
|
|
43
43
|
setCells: setCells,
|
|
44
44
|
sessionId: getSessionId(),
|
|
@@ -84,6 +84,7 @@ export const RunApp: React.FC<AppProps> = ({ appConfig }) => {
|
|
|
84
84
|
connection={connection}
|
|
85
85
|
isRunning={isRunning}
|
|
86
86
|
width={appConfig.width}
|
|
87
|
+
onReconnect={reconnect}
|
|
87
88
|
>
|
|
88
89
|
<AppHeader connection={connection} className="sm:pt-8">
|
|
89
90
|
{galleryHref && (
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { shouldLoadDuckDBPackages } from "../utils";
|
|
5
|
+
|
|
6
|
+
describe("shouldLoadDuckDBPackages", () => {
|
|
7
|
+
it("loads for mo.sql", () => {
|
|
8
|
+
expect(shouldLoadDuckDBPackages('df = mo.sql("SELECT 1")')).toBe(true);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("loads for duckdb imports and usage", () => {
|
|
12
|
+
expect(shouldLoadDuckDBPackages("import duckdb")).toBe(true);
|
|
13
|
+
expect(shouldLoadDuckDBPackages("from duckdb import sql")).toBe(true);
|
|
14
|
+
expect(shouldLoadDuckDBPackages("import pandas, duckdb")).toBe(true);
|
|
15
|
+
expect(shouldLoadDuckDBPackages("rows = duckdb.sql('SELECT 1')")).toBe(
|
|
16
|
+
true,
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("loads when package discovery found duckdb", () => {
|
|
21
|
+
expect(
|
|
22
|
+
shouldLoadDuckDBPackages("print('hello')", new Set(["duckdb"])),
|
|
23
|
+
).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("does not load for incidental duckdb text", () => {
|
|
27
|
+
expect(shouldLoadDuckDBPackages("name = 'duckdb'")).toBe(false);
|
|
28
|
+
expect(shouldLoadDuckDBPackages("# import duckdb")).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("does not load without mo.sql, duckdb usage, or discovery", () => {
|
|
32
|
+
expect(shouldLoadDuckDBPackages("print('hello')")).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
});
|
package/src/core/wasm/utils.ts
CHANGED
|
@@ -10,3 +10,17 @@ export function isWasm(): boolean {
|
|
|
10
10
|
document.querySelector("marimo-wasm") !== null
|
|
11
11
|
);
|
|
12
12
|
}
|
|
13
|
+
|
|
14
|
+
const DUCKDB_USAGE_PATTERN =
|
|
15
|
+
/(^|\n)\s*(?:import\s+[^\n#]*\bduckdb\b|from\s+duckdb\b|[^\n#]*\bduckdb\s*\.)/;
|
|
16
|
+
|
|
17
|
+
export function shouldLoadDuckDBPackages(
|
|
18
|
+
code: string,
|
|
19
|
+
foundPackages?: ReadonlySet<string>,
|
|
20
|
+
): boolean {
|
|
21
|
+
return (
|
|
22
|
+
code.includes("mo.sql") ||
|
|
23
|
+
DUCKDB_USAGE_PATTERN.test(code) ||
|
|
24
|
+
foundPackages?.has("duckdb") === true
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -9,6 +9,7 @@ import { WasmFileSystem } from "./fs";
|
|
|
9
9
|
import { getMarimoWheel } from "./getMarimoWheel";
|
|
10
10
|
import { t } from "./tracer";
|
|
11
11
|
import type { SerializedBridge, WasmController } from "./types";
|
|
12
|
+
import { shouldLoadDuckDBPackages } from "../utils";
|
|
12
13
|
|
|
13
14
|
const MAKE_SNAPSHOT = false;
|
|
14
15
|
|
|
@@ -163,8 +164,8 @@ export class DefaultWasmController implements WasmController {
|
|
|
163
164
|
private async loadNotebookDeps(code: string, foundPackages: Set<string>) {
|
|
164
165
|
const pyodide = this.requirePyodide;
|
|
165
166
|
|
|
166
|
-
if (code
|
|
167
|
-
// We need pandas and duckdb for mo.sql
|
|
167
|
+
if (shouldLoadDuckDBPackages(code, foundPackages)) {
|
|
168
|
+
// We need pandas and duckdb for mo.sql and for remote duckdb sources
|
|
168
169
|
code = `import pandas\n${code}`;
|
|
169
170
|
code = `import duckdb\n${code}`;
|
|
170
171
|
code = `import sqlglot\n${code}`;
|
|
@@ -34,6 +34,7 @@ import type {
|
|
|
34
34
|
SerializedBridge,
|
|
35
35
|
WasmController,
|
|
36
36
|
} from "./types";
|
|
37
|
+
import { shouldLoadDuckDBPackages } from "../utils";
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
40
|
* Web worker responsible for running the notebook.
|
|
@@ -141,8 +142,8 @@ const requestHandler = createRPCRequestHandler({
|
|
|
141
142
|
const span = t.startSpan("loadPackages");
|
|
142
143
|
await pyodideReadyPromise; // Make sure loading is done
|
|
143
144
|
|
|
144
|
-
if (code
|
|
145
|
-
// Add pandas and duckdb to the code
|
|
145
|
+
if (shouldLoadDuckDBPackages(code)) {
|
|
146
|
+
// Add pandas and duckdb to the code for mo.sql and for remote duckdb sources
|
|
146
147
|
code = `import pandas\n${code}`;
|
|
147
148
|
code = `import duckdb\n${code}`;
|
|
148
149
|
code = `import sqlglot\n${code}`;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
// @vitest-environment jsdom
|
|
3
|
+
|
|
4
|
+
import { act, renderHook } from "@testing-library/react";
|
|
5
|
+
import { createStore, Provider as JotaiProvider } from "jotai";
|
|
6
|
+
import type React from "react";
|
|
7
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
8
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
9
|
+
|
|
10
|
+
vi.mock("@/core/websocket/useWebSocket", async () => {
|
|
11
|
+
const actual =
|
|
12
|
+
await vi.importActual<typeof import("../useWebSocket")>("../useWebSocket");
|
|
13
|
+
return {
|
|
14
|
+
...actual,
|
|
15
|
+
useConnectionTransport: vi.fn(),
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
vi.mock("@/core/runtime/config", async () => {
|
|
20
|
+
const actual = await vi.importActual<typeof import("@/core/runtime/config")>(
|
|
21
|
+
"@/core/runtime/config",
|
|
22
|
+
);
|
|
23
|
+
return {
|
|
24
|
+
...actual,
|
|
25
|
+
useRuntimeManager: vi.fn(),
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
import { useRuntimeManager } from "@/core/runtime/config";
|
|
30
|
+
import { connectionAtom } from "../../network/connection";
|
|
31
|
+
import type { SessionId } from "../../kernel/session";
|
|
32
|
+
import { WebSocketClosedReason, WebSocketState } from "../types";
|
|
33
|
+
import { useMarimoKernelConnection } from "../useMarimoKernelConnection";
|
|
34
|
+
import { useConnectionTransport } from "../useWebSocket";
|
|
35
|
+
|
|
36
|
+
interface MockTransport {
|
|
37
|
+
readyState: 0 | 1 | 2 | 3;
|
|
38
|
+
retryCount: number;
|
|
39
|
+
reconnect: ReturnType<typeof vi.fn>;
|
|
40
|
+
close: ReturnType<typeof vi.fn>;
|
|
41
|
+
send: ReturnType<typeof vi.fn>;
|
|
42
|
+
addEventListener: ReturnType<typeof vi.fn>;
|
|
43
|
+
removeEventListener: ReturnType<typeof vi.fn>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function makeTransport(
|
|
47
|
+
readyState: 0 | 1 | 2 | 3 = WebSocket.CLOSED,
|
|
48
|
+
): MockTransport {
|
|
49
|
+
return {
|
|
50
|
+
readyState,
|
|
51
|
+
retryCount: 0,
|
|
52
|
+
reconnect: vi.fn(),
|
|
53
|
+
close: vi.fn(),
|
|
54
|
+
send: vi.fn(),
|
|
55
|
+
addEventListener: vi.fn(),
|
|
56
|
+
removeEventListener: vi.fn(),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function makeRuntimeManager(isHealthy = vi.fn().mockResolvedValue(true)) {
|
|
61
|
+
return {
|
|
62
|
+
isHealthy,
|
|
63
|
+
getWsURL: () => new URL("ws://localhost/ws"),
|
|
64
|
+
waitForHealthy: vi.fn().mockResolvedValue(undefined),
|
|
65
|
+
isSameOrigin: true,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
describe("useMarimoKernelConnection.reconnect()", () => {
|
|
70
|
+
let transport: MockTransport;
|
|
71
|
+
let isHealthy: ReturnType<typeof vi.fn>;
|
|
72
|
+
let store: ReturnType<typeof createStore>;
|
|
73
|
+
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
transport = makeTransport(WebSocket.CLOSED);
|
|
76
|
+
isHealthy = vi.fn().mockResolvedValue(true);
|
|
77
|
+
store = createStore();
|
|
78
|
+
store.set(connectionAtom, {
|
|
79
|
+
state: WebSocketState.CLOSED,
|
|
80
|
+
code: WebSocketClosedReason.KERNEL_DISCONNECTED,
|
|
81
|
+
reason: "kernel not found",
|
|
82
|
+
});
|
|
83
|
+
vi.mocked(useConnectionTransport).mockReturnValue(transport);
|
|
84
|
+
vi.mocked(useRuntimeManager).mockReturnValue(
|
|
85
|
+
makeRuntimeManager(isHealthy) as unknown as ReturnType<
|
|
86
|
+
typeof useRuntimeManager
|
|
87
|
+
>,
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
function renderUseHook() {
|
|
92
|
+
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
|
|
93
|
+
<JotaiProvider store={store}>
|
|
94
|
+
<ErrorBoundary fallback={null}>{children}</ErrorBoundary>
|
|
95
|
+
</JotaiProvider>
|
|
96
|
+
);
|
|
97
|
+
return renderHook(
|
|
98
|
+
() =>
|
|
99
|
+
useMarimoKernelConnection({
|
|
100
|
+
sessionId: "test-session" as SessionId,
|
|
101
|
+
autoInstantiate: false,
|
|
102
|
+
setCells: () => {},
|
|
103
|
+
}),
|
|
104
|
+
{ wrapper },
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
it("is a no-op when the transport is already OPEN", async () => {
|
|
109
|
+
transport.readyState = WebSocket.OPEN;
|
|
110
|
+
const { result } = renderUseHook();
|
|
111
|
+
await act(async () => {
|
|
112
|
+
await result.current.reconnect();
|
|
113
|
+
});
|
|
114
|
+
expect(isHealthy).not.toHaveBeenCalled();
|
|
115
|
+
expect(transport.reconnect).not.toHaveBeenCalled();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("is a no-op when the transport is already CONNECTING", async () => {
|
|
119
|
+
transport.readyState = WebSocket.CONNECTING;
|
|
120
|
+
const { result } = renderUseHook();
|
|
121
|
+
await act(async () => {
|
|
122
|
+
await result.current.reconnect();
|
|
123
|
+
});
|
|
124
|
+
expect(isHealthy).not.toHaveBeenCalled();
|
|
125
|
+
expect(transport.reconnect).not.toHaveBeenCalled();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("probes /health and reconnects when the runtime is healthy", async () => {
|
|
129
|
+
isHealthy.mockResolvedValue(true);
|
|
130
|
+
const { result } = renderUseHook();
|
|
131
|
+
await act(async () => {
|
|
132
|
+
await result.current.reconnect();
|
|
133
|
+
});
|
|
134
|
+
expect(isHealthy).toHaveBeenCalledOnce();
|
|
135
|
+
expect(transport.reconnect).toHaveBeenCalledOnce();
|
|
136
|
+
expect(store.get(connectionAtom)).toEqual({
|
|
137
|
+
state: WebSocketState.CONNECTING,
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("transitions to CLOSED and does not call ws.reconnect when the probe fails", async () => {
|
|
142
|
+
isHealthy.mockResolvedValue(false);
|
|
143
|
+
const { result } = renderUseHook();
|
|
144
|
+
await act(async () => {
|
|
145
|
+
await result.current.reconnect();
|
|
146
|
+
});
|
|
147
|
+
expect(isHealthy).toHaveBeenCalledOnce();
|
|
148
|
+
expect(transport.reconnect).not.toHaveBeenCalled();
|
|
149
|
+
expect(store.get(connectionAtom)).toEqual({
|
|
150
|
+
state: WebSocketState.CLOSED,
|
|
151
|
+
code: WebSocketClosedReason.KERNEL_DISCONNECTED,
|
|
152
|
+
reason: "kernel not found",
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { Logger } from "@/utils/Logger";
|
|
5
|
+
import { WebSocketClosedReason, WebSocketState } from "../types";
|
|
6
|
+
import { classifyCloseEvent } from "../useMarimoKernelConnection";
|
|
7
|
+
import { MAX_RETRIES } from "../useWebSocket";
|
|
8
|
+
|
|
9
|
+
function classify(
|
|
10
|
+
reason: string | undefined,
|
|
11
|
+
retryCount = 0,
|
|
12
|
+
maxRetries = MAX_RETRIES,
|
|
13
|
+
) {
|
|
14
|
+
return classifyCloseEvent({ reason }, { retryCount, maxRetries });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("classifyCloseEvent", () => {
|
|
18
|
+
describe("transient closes (default branch)", () => {
|
|
19
|
+
it("retries when retryCount < maxRetries", () => {
|
|
20
|
+
const decision = classify(undefined, 0);
|
|
21
|
+
expect(decision.kind).toBe("retry");
|
|
22
|
+
expect(decision.status).toEqual({ state: WebSocketState.CONNECTING });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("retries on each intermediate close event during a retry storm", () => {
|
|
26
|
+
for (let n = 0; n < MAX_RETRIES; n++) {
|
|
27
|
+
const decision = classify(undefined, n);
|
|
28
|
+
expect(decision.kind).toBe("retry");
|
|
29
|
+
expect(decision.status).toEqual({ state: WebSocketState.CONNECTING });
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("transitions to CLOSED when retryCount reaches maxRetries", () => {
|
|
34
|
+
const decision = classify(undefined, MAX_RETRIES);
|
|
35
|
+
expect(decision.kind).toBe("gave-up");
|
|
36
|
+
expect(decision.status).toEqual({
|
|
37
|
+
state: WebSocketState.CLOSED,
|
|
38
|
+
code: WebSocketClosedReason.KERNEL_DISCONNECTED,
|
|
39
|
+
reason: "kernel not found",
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("transitions to CLOSED when retryCount exceeds maxRetries", () => {
|
|
44
|
+
const decision = classify(undefined, MAX_RETRIES + 5);
|
|
45
|
+
expect(decision.kind).toBe("gave-up");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("treats unknown reason strings as transient and logs a warning", () => {
|
|
49
|
+
const logger = vi.spyOn(Logger, "warn").mockImplementation(() => {});
|
|
50
|
+
const decision = classify("something-else", 3);
|
|
51
|
+
expect(decision.kind).toBe("retry");
|
|
52
|
+
expect(logger).toHaveBeenCalled();
|
|
53
|
+
logger.mockRestore();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
vi.restoreAllMocks();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("terminal closes (server-initiated)", () => {
|
|
62
|
+
it("MARIMO_ALREADY_CONNECTED → terminal + closeTransport, with takeover", () => {
|
|
63
|
+
const decision = classify("MARIMO_ALREADY_CONNECTED", 0);
|
|
64
|
+
expect(decision.kind).toBe("terminal");
|
|
65
|
+
expect(decision.status).toMatchObject({
|
|
66
|
+
state: WebSocketState.CLOSED,
|
|
67
|
+
code: WebSocketClosedReason.ALREADY_RUNNING,
|
|
68
|
+
canTakeover: true,
|
|
69
|
+
});
|
|
70
|
+
if (decision.kind === "terminal") {
|
|
71
|
+
expect(decision.closeTransport).toBe(true);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it.each([
|
|
76
|
+
"MARIMO_WRONG_KERNEL_ID",
|
|
77
|
+
"MARIMO_NO_FILE_KEY",
|
|
78
|
+
"MARIMO_NO_SESSION_ID",
|
|
79
|
+
"MARIMO_NO_SESSION",
|
|
80
|
+
"MARIMO_SHUTDOWN",
|
|
81
|
+
])("%s → terminal with KERNEL_DISCONNECTED, closes transport", (reason) => {
|
|
82
|
+
const decision = classify(reason, 0);
|
|
83
|
+
expect(decision.kind).toBe("terminal");
|
|
84
|
+
expect(decision.status).toMatchObject({
|
|
85
|
+
state: WebSocketState.CLOSED,
|
|
86
|
+
code: WebSocketClosedReason.KERNEL_DISCONNECTED,
|
|
87
|
+
});
|
|
88
|
+
if (decision.kind === "terminal") {
|
|
89
|
+
expect(decision.closeTransport).toBe(true);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("MARIMO_MALFORMED_QUERY → terminal but does NOT close transport", () => {
|
|
94
|
+
const decision = classify("MARIMO_MALFORMED_QUERY", 0);
|
|
95
|
+
expect(decision.kind).toBe("terminal");
|
|
96
|
+
expect(decision.status).toMatchObject({
|
|
97
|
+
state: WebSocketState.CLOSED,
|
|
98
|
+
code: WebSocketClosedReason.MALFORMED_QUERY,
|
|
99
|
+
});
|
|
100
|
+
if (decision.kind === "terminal") {
|
|
101
|
+
expect(decision.closeTransport).toBe(false);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("MARIMO_KERNEL_STARTUP_ERROR → terminal + closeTransport", () => {
|
|
106
|
+
const decision = classify("MARIMO_KERNEL_STARTUP_ERROR", 0);
|
|
107
|
+
expect(decision.kind).toBe("terminal");
|
|
108
|
+
expect(decision.status).toMatchObject({
|
|
109
|
+
state: WebSocketState.CLOSED,
|
|
110
|
+
code: WebSocketClosedReason.KERNEL_STARTUP_ERROR,
|
|
111
|
+
});
|
|
112
|
+
if (decision.kind === "terminal") {
|
|
113
|
+
expect(decision.closeTransport).toBe(true);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("terminal closes ignore retryCount entirely", () => {
|
|
118
|
+
const decision = classify("MARIMO_SHUTDOWN", 99);
|
|
119
|
+
expect(decision.kind).toBe("terminal");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("retry budget exhaustion", () => {
|
|
124
|
+
it("yields retry on attempts 1..maxRetries-1 and gave-up on the final close", () => {
|
|
125
|
+
const states: string[] = [];
|
|
126
|
+
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
|
|
127
|
+
states.push(classify(undefined, attempt - 1).kind);
|
|
128
|
+
}
|
|
129
|
+
states.push(classify(undefined, MAX_RETRIES).kind);
|
|
130
|
+
|
|
131
|
+
expect(states).toEqual([
|
|
132
|
+
...Array.from({ length: MAX_RETRIES }, () => "retry"),
|
|
133
|
+
"gave-up",
|
|
134
|
+
]);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
});
|
|
@@ -11,7 +11,10 @@ import type {
|
|
|
11
11
|
NotificationMessageData,
|
|
12
12
|
NotificationPayload,
|
|
13
13
|
} from "@/core/kernel/messages";
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
MAX_RETRIES,
|
|
16
|
+
useConnectionTransport,
|
|
17
|
+
} from "@/core/websocket/useWebSocket";
|
|
15
18
|
import { renderHTML } from "@/plugins/core/RenderHTML";
|
|
16
19
|
import {
|
|
17
20
|
handleWidgetMessage,
|
|
@@ -69,10 +72,96 @@ import { useStorageActions } from "../storage/state";
|
|
|
69
72
|
import { useVariablesActions } from "../variables/state";
|
|
70
73
|
import type { VariableName } from "../variables/types";
|
|
71
74
|
import { isWasm } from "../wasm/utils";
|
|
72
|
-
import {
|
|
75
|
+
import {
|
|
76
|
+
type ConnectionStatus,
|
|
77
|
+
WebSocketClosedReason,
|
|
78
|
+
WebSocketState,
|
|
79
|
+
} from "./types";
|
|
73
80
|
|
|
74
81
|
const SUPPORTS_LAZY_KERNELS = true;
|
|
75
82
|
|
|
83
|
+
export type CloseDecision =
|
|
84
|
+
| { kind: "terminal"; status: ConnectionStatus; closeTransport: boolean }
|
|
85
|
+
| { kind: "gave-up"; status: ConnectionStatus }
|
|
86
|
+
| { kind: "retry"; status: ConnectionStatus };
|
|
87
|
+
|
|
88
|
+
export function classifyCloseEvent(
|
|
89
|
+
event: { reason?: string },
|
|
90
|
+
context: { retryCount: number; maxRetries: number },
|
|
91
|
+
): CloseDecision {
|
|
92
|
+
switch (event.reason) {
|
|
93
|
+
case "MARIMO_ALREADY_CONNECTED":
|
|
94
|
+
return {
|
|
95
|
+
kind: "terminal",
|
|
96
|
+
status: {
|
|
97
|
+
state: WebSocketState.CLOSED,
|
|
98
|
+
code: WebSocketClosedReason.ALREADY_RUNNING,
|
|
99
|
+
reason: "another browser tab is already connected to the kernel",
|
|
100
|
+
canTakeover: true,
|
|
101
|
+
},
|
|
102
|
+
closeTransport: true,
|
|
103
|
+
};
|
|
104
|
+
case "MARIMO_WRONG_KERNEL_ID":
|
|
105
|
+
case "MARIMO_NO_FILE_KEY":
|
|
106
|
+
case "MARIMO_NO_SESSION_ID":
|
|
107
|
+
case "MARIMO_NO_SESSION":
|
|
108
|
+
case "MARIMO_SHUTDOWN":
|
|
109
|
+
return {
|
|
110
|
+
kind: "terminal",
|
|
111
|
+
status: {
|
|
112
|
+
state: WebSocketState.CLOSED,
|
|
113
|
+
code: WebSocketClosedReason.KERNEL_DISCONNECTED,
|
|
114
|
+
reason: "kernel not found",
|
|
115
|
+
},
|
|
116
|
+
closeTransport: true,
|
|
117
|
+
};
|
|
118
|
+
case "MARIMO_MALFORMED_QUERY":
|
|
119
|
+
return {
|
|
120
|
+
kind: "terminal",
|
|
121
|
+
status: {
|
|
122
|
+
state: WebSocketState.CLOSED,
|
|
123
|
+
code: WebSocketClosedReason.MALFORMED_QUERY,
|
|
124
|
+
reason:
|
|
125
|
+
"the kernel did not recognize a request; please file a bug with marimo",
|
|
126
|
+
},
|
|
127
|
+
closeTransport: false,
|
|
128
|
+
};
|
|
129
|
+
case "MARIMO_KERNEL_STARTUP_ERROR":
|
|
130
|
+
return {
|
|
131
|
+
kind: "terminal",
|
|
132
|
+
status: {
|
|
133
|
+
state: WebSocketState.CLOSED,
|
|
134
|
+
code: WebSocketClosedReason.KERNEL_STARTUP_ERROR,
|
|
135
|
+
reason: "Failed to start kernel sandbox",
|
|
136
|
+
},
|
|
137
|
+
closeTransport: true,
|
|
138
|
+
};
|
|
139
|
+
default:
|
|
140
|
+
// Empty/undefined reasons are normal transient closes. Anything else is
|
|
141
|
+
// an unknown server reason; warn so a new MARIMO_* reason doesn't fall
|
|
142
|
+
// silently into the retry path.
|
|
143
|
+
if (event.reason) {
|
|
144
|
+
logNever(event.reason as never);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// partysocket stops retrying silently once `maxRetries` is hit; surface
|
|
148
|
+
// CLOSED so callers can detect the give-up.
|
|
149
|
+
if (context.retryCount >= context.maxRetries) {
|
|
150
|
+
return {
|
|
151
|
+
kind: "gave-up",
|
|
152
|
+
status: {
|
|
153
|
+
state: WebSocketState.CLOSED,
|
|
154
|
+
code: WebSocketClosedReason.KERNEL_DISCONNECTED,
|
|
155
|
+
reason: "kernel not found",
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
kind: "retry",
|
|
161
|
+
status: { state: WebSocketState.CONNECTING },
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
76
165
|
function getExistingCells(): CellData[] | undefined {
|
|
77
166
|
if (!SUPPORTS_LAZY_KERNELS) {
|
|
78
167
|
return undefined;
|
|
@@ -340,6 +429,30 @@ export function useMarimoKernelConnection(opts: {
|
|
|
340
429
|
}
|
|
341
430
|
};
|
|
342
431
|
|
|
432
|
+
// Manual reconnect. Probes /health first to fail fast when the runtime
|
|
433
|
+
// is unreachable, instead of waiting on partysocket's retry budget.
|
|
434
|
+
const reconnect = async () => {
|
|
435
|
+
if (
|
|
436
|
+
ws.readyState === WebSocket.OPEN ||
|
|
437
|
+
ws.readyState === WebSocket.CONNECTING
|
|
438
|
+
) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
shouldTryReconnecting.current = true;
|
|
442
|
+
setConnection({ state: WebSocketState.CONNECTING });
|
|
443
|
+
const healthy = await runtimeManager.isHealthy();
|
|
444
|
+
if (!healthy) {
|
|
445
|
+
shouldTryReconnecting.current = false;
|
|
446
|
+
setConnection({
|
|
447
|
+
state: WebSocketState.CLOSED,
|
|
448
|
+
code: WebSocketClosedReason.KERNEL_DISCONNECTED,
|
|
449
|
+
reason: "kernel not found",
|
|
450
|
+
});
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
ws.reconnect();
|
|
454
|
+
};
|
|
455
|
+
|
|
343
456
|
const ws = useConnectionTransport({
|
|
344
457
|
static: isStaticNotebook(),
|
|
345
458
|
/**
|
|
@@ -399,58 +512,20 @@ export function useMarimoKernelConnection(opts: {
|
|
|
399
512
|
*/
|
|
400
513
|
onClose: (e) => {
|
|
401
514
|
Logger.warn("WebSocket closed", e.code, e.reason);
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
case "MARIMO_NO_SESSION":
|
|
417
|
-
case "MARIMO_SHUTDOWN":
|
|
418
|
-
setConnection({
|
|
419
|
-
state: WebSocketState.CLOSED,
|
|
420
|
-
code: WebSocketClosedReason.KERNEL_DISCONNECTED,
|
|
421
|
-
reason: "kernel not found",
|
|
422
|
-
});
|
|
423
|
-
ws.close(); // close to prevent reconnecting
|
|
424
|
-
return;
|
|
425
|
-
|
|
426
|
-
case "MARIMO_MALFORMED_QUERY":
|
|
427
|
-
setConnection({
|
|
428
|
-
state: WebSocketState.CLOSED,
|
|
429
|
-
code: WebSocketClosedReason.MALFORMED_QUERY,
|
|
430
|
-
reason:
|
|
431
|
-
"the kernel did not recognize a request; please file a bug with marimo",
|
|
432
|
-
});
|
|
433
|
-
return;
|
|
434
|
-
|
|
435
|
-
default:
|
|
436
|
-
// Check for kernel startup error (full error already received via message)
|
|
437
|
-
if (e.reason === "MARIMO_KERNEL_STARTUP_ERROR") {
|
|
438
|
-
setConnection({
|
|
439
|
-
state: WebSocketState.CLOSED,
|
|
440
|
-
code: WebSocketClosedReason.KERNEL_STARTUP_ERROR,
|
|
441
|
-
reason: "Failed to start kernel sandbox",
|
|
442
|
-
});
|
|
443
|
-
ws.close(); // prevent reconnecting
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
// Session should be valid
|
|
448
|
-
// - browser tab might have been closed or re-opened
|
|
449
|
-
// - computer might have just woken from sleep
|
|
450
|
-
//
|
|
451
|
-
// so try reconnecting.
|
|
452
|
-
setConnection({ state: WebSocketState.CONNECTING });
|
|
453
|
-
tryReconnecting(e.code, e.reason);
|
|
515
|
+
const decision = classifyCloseEvent(e, {
|
|
516
|
+
retryCount: ws.retryCount,
|
|
517
|
+
maxRetries: MAX_RETRIES,
|
|
518
|
+
});
|
|
519
|
+
setConnection(decision.status);
|
|
520
|
+
if (decision.kind === "terminal" && decision.closeTransport) {
|
|
521
|
+
ws.close(); // close to prevent reconnecting
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (decision.kind === "retry") {
|
|
525
|
+
// Session should be valid
|
|
526
|
+
// - browser tab might have been closed or re-opened
|
|
527
|
+
// - computer might have just woken from sleep
|
|
528
|
+
tryReconnecting(e.code, e.reason);
|
|
454
529
|
}
|
|
455
530
|
},
|
|
456
531
|
|
|
@@ -468,5 +543,5 @@ export function useMarimoKernelConnection(opts: {
|
|
|
468
543
|
},
|
|
469
544
|
});
|
|
470
545
|
|
|
471
|
-
return { connection };
|
|
546
|
+
return { connection, reconnect };
|
|
472
547
|
}
|
|
@@ -18,6 +18,10 @@ interface UseConnectionTransportOptions {
|
|
|
18
18
|
onError: (event: WebSocketEventMap["error"]) => void;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
// Per-`reconnect()` retry budget for partysocket. After exhaustion, partysocket
|
|
22
|
+
// stops silently; treat `retryCount >= MAX_RETRIES` as the give-up signal.
|
|
23
|
+
export const MAX_RETRIES = 10;
|
|
24
|
+
|
|
21
25
|
function createConnectionTransport(
|
|
22
26
|
options: Pick<UseConnectionTransportOptions, "url" | "static">,
|
|
23
27
|
): IConnectionTransport {
|
|
@@ -33,8 +37,7 @@ function createConnectionTransport(
|
|
|
33
37
|
// Cast needed: ReconnectingWebSocket types readyState as `number`
|
|
34
38
|
// but IConnectionTransport expects `0 | 1 | 2 | 3`
|
|
35
39
|
return new ReconnectingWebSocket(urlProvider, undefined, {
|
|
36
|
-
|
|
37
|
-
maxRetries: 10,
|
|
40
|
+
maxRetries: MAX_RETRIES,
|
|
38
41
|
debug: false,
|
|
39
42
|
startClosed: true,
|
|
40
43
|
// long timeout -- the server can become slow when many notebooks
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{t as e}from"./worker-Bfy15ViQ.js";var t=e(((e,t)=>{t.exports={}}));export default t();
|