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: start remote connection in a Connecting state (#7838)

Myles Scolnickc2eb57e8c825
diff --git a/frontend/src/__tests__/mount.test.ts b/frontend/src/__tests__/mount.test.ts
new file mode 100644
index 0000000000..764ebf9f76
--- /dev/null
+++ b/frontend/src/__tests__/mount.test.ts
@@ -0,0 +1,128 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { connectionAtom } from "@/core/network/connection";
+import { store } from "@/core/state/jotai";
+import { WebSocketState } from "@/core/websocket/types";
+import { mount, visibleForTesting } from "../mount";
+
+// Mock React DOM
+vi.mock("react-dom/client", () => ({
+ createRoot: vi.fn(() => ({
+ render: vi.fn(),
+ })),
+}));
+
+// Mock static state
+vi.mock("@/core/static/static-state", () => ({
+ isStaticNotebook: vi.fn(() => false),
+}));
+
+// Mock other side-effect modules
+vi.mock("@/core/vscode/vscode-bindings", () => ({
+ maybeRegisterVSCodeBindings: vi.fn(),
+}));
+
+vi.mock("@/plugins/plugins", () => ({
+ initializePlugins: vi.fn(),
+}));
+
+vi.mock("@/core/network/auth", () => ({
+ cleanupAuthQueryParams: vi.fn(),
+}));
+
+vi.mock("@/utils/vitals", () => ({
+ reportVitals: vi.fn(),
+}));
+
+// Mock preloadPage
+vi.mock("@/core/MarimoApp", () => ({
+ MarimoApp: () => null,
+ preloadPage: vi.fn(),
+}));
+
+describe("mount", () => {
+ const mockElement = document.createElement("div");
+
+ beforeEach(() => {
+ visibleForTesting.reset();
+ // Reset connection atom to initial state
+ store.set(connectionAtom, { state: WebSocketState.NOT_STARTED });
+ });
+
+ afterEach(() => {
+ vi.clearAllMocks();
+ });
+
+ const baseOptions = {
+ filename: "test.py",
+ code: "",
+ version: "0.0.1",
+ mode: "edit" as const,
+ config: {},
+ configOverrides: {},
+ appConfig: {},
+ view: { showAppCode: true },
+ serverToken: "",
+ };
+
+ describe("connection state initialization", () => {
+ it("should set connection to CONNECTING when runtimeConfig has lazy=false", () => {
+ mount(
+ {
+ ...baseOptions,
+ runtimeConfig: [{ url: "http://localhost:8080", lazy: false }],
+ },
+ mockElement,
+ );
+
+ const connection = store.get(connectionAtom);
+ expect(connection.state).toBe(WebSocketState.CONNECTING);
+ });
+
+ it("should keep connection as NOT_STARTED when runtimeConfig has lazy=true", () => {
+ mount(
+ {
+ ...baseOptions,
+ runtimeConfig: [{ url: "http://localhost:8080", lazy: true }],
+ },
+ mockElement,
+ );
+
+ const connection = store.get(connectionAtom);
+ expect(connection.state).toBe(WebSocketState.NOT_STARTED);
+ });
+
+ it("should keep connection as NOT_STARTED when no runtimeConfig is provided", () => {
+ mount(
+ {
+ ...baseOptions,
+ runtimeConfig: [],
+ },
+ mockElement,
+ );
+
+ const connection = store.get(connectionAtom);
+ expect(connection.state).toBe(WebSocketState.NOT_STARTED);
+ });
+
+ it("should keep connection as NOT_STARTED for static notebooks even with lazy=false", async () => {
+ const { isStaticNotebook } = await import("@/core/static/static-state");
+ vi.mocked(isStaticNotebook).mockReturnValue(true);
+
+ // Reset mount state to allow another mount
+ visibleForTesting.reset();
+
+ mount(
+ {
+ ...baseOptions,
+ runtimeConfig: [{ url: "http://localhost:8080", lazy: false }],
+ },
+ mockElement,
+ );
+
+ const connection = store.get(connectionAtom);
+ expect(connection.state).toBe(WebSocketState.NOT_STARTED);
+ });
+ });
+});
diff --git a/frontend/src/mount.tsx b/frontend/src/mount.tsx
index e974919365..3aadf1ae7a 100644
--- a/frontend/src/mount.tsx
+++ b/frontend/src/mount.tsx
@@ -29,6 +29,7 @@ import {
import { MarimoApp, preloadPage } from "./core/MarimoApp";
import { type AppMode, initialModeAtom, viewStateAtom } from "./core/mode";
import { cleanupAuthQueryParams } from "./core/network/auth";
+import { connectionAtom } from "./core/network/connection";
import { requestClientAtom } from "./core/network/requests";
import { resolveRequestClient } from "./core/network/resolve";
import {
@@ -42,6 +43,7 @@ import { isStaticNotebook } from "./core/static/static-state";
import { maybeRegisterVSCodeBindings } from "./core/vscode/vscode-bindings";
import type { FileStore } from "./core/wasm/store";
import { notebookFileStore } from "./core/wasm/store";
+import { WebSocketState } from "./core/websocket/types";
import { vegaLoader } from "./plugins/impl/vega/loader";
import { initializePlugins } from "./plugins/plugins";
import { ThemeProvider } from "./theme/ThemeProvider";
@@ -304,6 +306,10 @@ function initStore(options: unknown) {
...firstRuntimeConfig,
serverToken: parsedOptions.data.serverToken,
});
+ // If the remote runtime is not lazy, start it in CONNECTING
+ if (!firstRuntimeConfig.lazy && !isStaticNotebook()) {
+ store.set(connectionAtom, { state: WebSocketState.CONNECTING });
+ }
} else {
store.set(runtimeConfigAtom, {
...DEFAULT_RUNTIME_CONFIG,