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.

Follow the WASM notebook through its host stream (#11517)

Trevor Manz30ad03b0a1dc
diff --git a/frontend/src/core/islands/worker/controller.ts b/frontend/src/core/islands/worker/controller.ts
index 7d0e52b3ce..acbdd0c73c 100644
--- a/frontend/src/core/islands/worker/controller.ts
+++ b/frontend/src/core/islands/worker/controller.ts
@@ -40,7 +40,7 @@ export class ReadonlyWasmController extends DefaultWasmController {
public override async startSession(opts: {
code: string;
filename: string | null;
- onMessage: (message: JsonString<NotificationPayload>) => void;
+ onMessage?: (message: JsonString<NotificationPayload>) => void;
}): Promise<SerializedBridge> {
const bridge = super.startSession({
queryParameters: {},
diff --git a/frontend/src/core/wasm/__tests__/bridge.test.ts b/frontend/src/core/wasm/__tests__/bridge.test.ts
index 9d1af4b844..0351b31dad 100644
--- a/frontend/src/core/wasm/__tests__/bridge.test.ts
+++ b/frontend/src/core/wasm/__tests__/bridge.test.ts
@@ -51,7 +51,6 @@ vi.mock("@/core/wasm/rpc", () => ({
readNotebook: mockReadNotebook,
saveNotebook: mockSaveNotebook,
},
- send: { consumerReady: vi.fn() },
},
addMessageListener: (event: string, cb: () => void) => {
rpcListeners[event] = cb;
diff --git a/frontend/src/core/wasm/bridge.ts b/frontend/src/core/wasm/bridge.ts
index 941518032c..2ea908def0 100644
--- a/frontend/src/core/wasm/bridge.ts
+++ b/frontend/src/core/wasm/bridge.ts
@@ -70,9 +70,6 @@ export class PyodideBridge implements RunRequests, EditRequests {
private pendingSessionSave: Promise<unknown> = Promise.resolve();
private notebookConsumers = new Map<string, (event: unknown) => void>();
private interruptBuffer?: Uint8Array;
- private messageConsumer:
- | ((message: MessageEvent<string>) => void)
- | undefined;
public initialized = new Deferred<void>();
@@ -158,9 +155,6 @@ export class PyodideBridge implements RunRequests, EditRequests {
store.set(wasmInitStateAtom, { kind: "error", message: error });
this.initialized.reject(new Error(error));
});
- this.rpc.addMessageListener("kernelMessage", ({ message }) => {
- this.messageConsumer?.(new MessageEvent("message", { data: message }));
- });
}
private async startSession() {
@@ -212,13 +206,6 @@ export class PyodideBridge implements RunRequests, EditRequests {
}
}
- public attachMessageConsumer(
- consumer: (message: MessageEvent<string>) => void,
- ) {
- this.messageConsumer = consumer;
- this.rpc.proxy.send.consumerReady({});
- }
-
public async createNotebookBackend() {
await this.initialized.promise;
const { id } = await this.rpc.proxy.request.notebookInfo();
diff --git a/frontend/src/core/wasm/worker/bootstrap.ts b/frontend/src/core/wasm/worker/bootstrap.ts
index 448a8424aa..357293e7d8 100644
--- a/frontend/src/core/wasm/worker/bootstrap.ts
+++ b/frontend/src/core/wasm/worker/bootstrap.ts
@@ -142,7 +142,8 @@ export class DefaultWasmController implements WasmController {
queryParameters: Record<string, string | string[]>;
code: string;
filename: string | null;
- onMessage: (message: JsonString<NotificationPayload>) => void;
+ /** Receives every notification, for callers that do not follow the notebook's events. */
+ onMessage?: (message: JsonString<NotificationPayload>) => void;
userConfig: UserConfig;
}): Promise<SerializedBridge> {
const sessionGeneration = this.sessionGeneration;
diff --git a/frontend/src/core/wasm/worker/types.ts b/frontend/src/core/wasm/worker/types.ts
index 4a260bc516..fa731e87bd 100644
--- a/frontend/src/core/wasm/worker/types.ts
+++ b/frontend/src/core/wasm/worker/types.ts
@@ -69,7 +69,8 @@ export interface WasmController {
code: string;
filename: string | null;
userConfig: UserConfig;
- onMessage: (message: JsonString<NotificationPayload>) => void;
+ /** Receives every notification, for callers that do not follow the notebook's events. */
+ onMessage?: (message: JsonString<NotificationPayload>) => void;
}): Promise<SerializedBridge>;
}
diff --git a/frontend/src/core/wasm/worker/worker.ts b/frontend/src/core/wasm/worker/worker.ts
index 257a5f5310..8177a5fd49 100644
--- a/frontend/src/core/wasm/worker/worker.ts
+++ b/frontend/src/core/wasm/worker/worker.ts
@@ -9,14 +9,11 @@ import {
type RPCSchema,
} from "rpc-anywhere";
import type { UserConfig } from "@/core/config/config-schema";
-import type { NotificationPayload } from "@/core/kernel/messages";
import type {
ListPackagesResponse,
PackageOperationResponse,
SaveNotebookRequest,
} from "@/core/network/types";
-import type { JsonString } from "@/utils/json/base64";
-import { once } from "@/utils/once";
import { decodeUtf8 } from "@/utils/strings";
import { Deferred } from "../../../utils/Deferred";
import { prettyError } from "../../../utils/errors";
@@ -28,7 +25,6 @@ import { TRANSPORT_ID } from "./constants";
import { WasmFileSystem } from "./fs";
import { getController } from "./getController";
import { getPyodideVersion } from "./getPyodideVersion";
-import { MessageBuffer } from "./message-buffer";
import { t } from "./tracer";
import type {
BridgePayload,
@@ -73,11 +69,6 @@ async function loadPyodideAndPackages() {
}
}
-const messageBuffer = new MessageBuffer(
- (message: JsonString<NotificationPayload>) => {
- rpc.send.kernelMessage({ message });
- },
-);
const bridgeReady = new Deferred<SerializedBridge>();
let started = false;
@@ -155,12 +146,6 @@ const requestHandler = createRPCRequestHandler({
const startSession = t.wrapAsync(
self.controller.startSession.bind(self.controller),
);
- const initializeOnce = once(() => {
- rpc.send.initializingMessage({
- message: "Initializing notebook...",
- });
- rpc.send.initialized({});
- });
rpc.send.initializingMessage({
message: "Loading notebook and dependencies...",
});
@@ -169,12 +154,12 @@ const requestHandler = createRPCRequestHandler({
filename: opts.filename,
queryParameters: opts.queryParameters,
userConfig: opts.userConfig,
- onMessage: (msg) => {
- initializeOnce();
- messageBuffer.push(msg);
- },
});
bridgeReady.resolve(bridge);
+ rpc.send.initializingMessage({
+ message: "Initializing notebook...",
+ });
+ rpc.send.initialized({});
workerInitSpan.end("ok");
} catch (error) {
rpc.send.initializedError({
@@ -385,8 +370,6 @@ export type WorkerSchema = RPCSchema<
notebookEvent: { id: string; event: string };
// Emitted when the worker is ready
ready: {};
- // Emitted when the kernel sends a message
- kernelMessage: { message: JsonString<NotificationPayload> };
// Emitted when the Pyodide is initialized
initialized: {};
// Emitted when the Pyodide is initializing, with new messages
@@ -409,13 +392,6 @@ rpc.addMessageListener("bootstrap", (config) => runtimeConfig.resolve(config));
rpc.send("ready", {});
-/// Listeners
-// When the consumer is ready, start the message buffer
-rpc.addMessageListener("consumerReady", async () => {
- await pyodideReadyPromise; // Make sure loading is done
- messageBuffer.start();
-});
-
const namesThatRequireSync = new Set<keyof RawBridge>([
"save",
"save_app_config",
diff --git a/frontend/src/core/websocket/__tests__/backend.test.ts b/frontend/src/core/websocket/__tests__/backend.test.ts
index bbabc5670b..3add70bbd1 100644
--- a/frontend/src/core/websocket/__tests__/backend.test.ts
+++ b/frontend/src/core/websocket/__tests__/backend.test.ts
@@ -1,22 +1,23 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { NotebookEvents } from "@marimo-team/client";
+import { NotebookEvents, StreamStopped } from "@marimo-team/client";
import { createStore } from "jotai/vanilla";
import { afterEach, describe, expect, it, onTestFinished, vi } from "vitest";
import { Deferred } from "@/utils/Deferred";
import { ConnectionState } from "../../host/connection";
+import { type FakeEdit, fakeBackend } from "../../host/__tests__/fixtures";
import { createNotebookReplica } from "../../host/replica";
import { createEditorBackend } from "../backend";
-const wasm = vi.hoisted(() => ({ enabled: false, consumer: vi.fn() }));
+const wasm = vi.hoisted(() => ({ enabled: false, create: vi.fn() }));
vi.mock("../../wasm/utils", () => ({ isWasm: () => wasm.enabled }));
vi.mock("../../wasm/bridge", () => ({
- PyodideBridge: { INSTANCE: { attachMessageConsumer: wasm.consumer } },
+ PyodideBridge: { INSTANCE: { createNotebookBackend: wasm.create } },
}));
afterEach(() => {
wasm.enabled = false;
- wasm.consumer.mockReset();
+ wasm.create.mockReset();
vi.unstubAllGlobals();
});
@@ -55,6 +56,8 @@ describe("createEditorBackend", () => {
["the default", options, "socket"],
] as const)("follows %s", async (_name, chosen, expected) => {
wasm.enabled = expected === "wasm";
+ const worker = vi.fn(() => new Promise<void>(() => undefined));
+ wasm.create.mockResolvedValue(fakeBackend({ transport: worker }));
const fetch = vi.fn(() => new Promise<Response>(() => undefined));
const opened = sockets();
vi.stubGlobal("fetch", fetch);
@@ -62,9 +65,8 @@ describe("createEditorBackend", () => {
await vi.waitFor(() =>
expect(
{
- idle:
- events.status === "connected" && !wasm.consumer.mock.calls.length,
- wasm: wasm.consumer.mock.calls.length === 1,
+ idle: events.status === "connected" && !wasm.create.mock.calls.length,
+ wasm: worker.mock.calls.length === 1,
fetch: fetch.mock.calls.length === 1,
socket: opened.length === 1,
}[expected],
@@ -107,19 +109,45 @@ describe("createEditorBackend", () => {
expect(headers.get("Idempotency-Key")).toBe("edit-1");
});
- it.each([
- ["a static notebook", { ...options, static: true }, false],
- ["WASM", options, true],
- ] as const)("does not edit %s", async (_name, chosen, isWasm) => {
- wasm.enabled = isWasm;
+ it("does not edit a static notebook", async () => {
const fetch = vi.fn();
vi.stubGlobal("fetch", fetch);
- const result = await createEditorBackend(chosen)
+ const result = await createEditorBackend({ ...options, static: true })
.notebook("n1")
.edit([], { idempotencyKey: "edit-1" });
expect(result).toMatchObject({ ok: false, status: 501 });
expect(fetch).not.toHaveBeenCalled();
});
+
+ it("edits the WASM notebook through the worker once its stream starts", async () => {
+ wasm.enabled = true;
+ const edit = vi.fn<FakeEdit>();
+ const transport = vi.fn(async () => undefined);
+ wasm.create.mockResolvedValue(fakeBackend({ transport, edit }));
+ const backend = createEditorBackend(options);
+ expect(wasm.create).not.toHaveBeenCalled();
+ expect(() => backend.notebook("n1")).toThrow("has not started");
+
+ const attempt = { cursor: null, signal: new AbortController().signal };
+ const handlers = { open: vi.fn(), message: vi.fn() };
+ await backend.transport(attempt, handlers);
+ await backend.transport(attempt, handlers);
+ expect(wasm.create).toHaveBeenCalledOnce();
+ expect(transport).toHaveBeenCalledWith(attempt, handlers);
+ await backend.notebook("n1").edit([], { idempotencyKey: "edit-1" });
+ expect(edit).toHaveBeenCalledWith("n1", [], { idempotencyKey: "edit-1" });
+ });
+
+ it("stops following WASM when the worker does not start", async () => {
+ wasm.enabled = true;
+ wasm.create.mockRejectedValue(new Error("Initialization failed"));
+ await expect(
+ createEditorBackend(options).transport(
+ { cursor: null, signal: new AbortController().signal },
+ { open: vi.fn(), message: vi.fn() },
+ ),
+ ).rejects.toBeInstanceOf(StreamStopped);
+ });
});
describe("the session backend's startup", () => {
diff --git a/frontend/src/core/websocket/backend.ts b/frontend/src/core/websocket/backend.ts
index 479339039a..226631c31e 100644
--- a/frontend/src/core/websocket/backend.ts
+++ b/frontend/src/core/websocket/backend.ts
@@ -9,7 +9,6 @@ import { PyodideBridge } from "../wasm/bridge";
import { isWasm } from "../wasm/utils";
import {
idle,
- pyodide,
sessionSse,
websocket,
withHeaders,
@@ -61,6 +60,36 @@ function afterStartup({
};
}
+/**
+ * Follow the notebook running in the WASM worker.
+ *
+ * The worker's backend exists once the worker has started, so the first
+ * connection creates it. Commands are only for a notebook its stream reported.
+ */
+function wasm(): NotebookBackend {
+ let backend: Promise<NotebookBackend> | undefined;
+ let started: NotebookBackend | undefined;
+ return {
+ transport: async (attempt, handlers) => {
+ backend ??= PyodideBridge.INSTANCE.createNotebookBackend();
+ try {
+ started = await backend;
+ } catch (error) {
+ throw new StreamStopped("The WASM notebook did not start", {
+ cause: error,
+ });
+ }
+ return started.transport(attempt, handlers);
+ },
+ notebook: (id) => {
+ if (started === undefined) {
+ throw new Error("The WASM notebook has not started");
+ }
+ return started.notebook(id);
+ },
+ };
+}
+
/** Commands for a notebook the editor cannot change through its backend. */
function uneditable(): ReturnType<NotebookBackend["notebook"]> {
return {
@@ -84,12 +113,7 @@ export function createEditorBackend(
return { transport: idle, notebook: uneditable };
}
if (isWasm()) {
- return {
- transport: pyodide((consumer) =>
- PyodideBridge.INSTANCE.attachMessageConsumer(consumer),
- ),
- notebook: uneditable,
- };
+ return wasm();
}
const url = afterStartup(options);
return {
diff --git a/frontend/src/core/websocket/transports/__tests__/notebook.test.ts b/frontend/src/core/websocket/transports/__tests__/notebook.test.ts
index 5ff05ea6ba..665f22ab2e 100644
--- a/frontend/src/core/websocket/transports/__tests__/notebook.test.ts
+++ b/frontend/src/core/websocket/transports/__tests__/notebook.test.ts
@@ -11,7 +11,7 @@ import { createNotebookReplica } from "../../../host/replica";
import { fakeBackend, fixtureMessages } from "../../../host/__tests__/fixtures";
import { ConnectionState } from "../../../host/connection";
import { Deferred } from "@/utils/Deferred";
-import { pyodide, sessionSse, websocket } from "../notebook";
+import { sessionSse, websocket } from "../notebook";
const encoder = new TextEncoder();
@@ -403,43 +403,3 @@ describe.each(["websocket", "sse"] as const)(
});
},
);
-
-describe("pyodide", () => {
- it("delivers worker notifications as marimo events and host events as they are", async () => {
- let consumer: ((message: MessageEvent<string>) => void) | undefined;
- const events = new NotebookEvents(
- pyodide((next) => {
- consumer = next;
- }),
- { types: ["notebook", "marimo"] },
- );
- const received = collect(events);
- await vi.waitFor(() => expect(consumer).toBeDefined());
- const send = (data: string) =>
- consumer?.(new MessageEvent("message", { data }));
- send('{"data":{"op":"kernel-ready"}}');
- send('{"event":"marimo","id":"3","data":{"marimo":{"op":"reconnected"}}}');
- expect(received).toMatchInlineSnapshot(`
- [
- {
- "data": {
- "marimo": {
- "op": "kernel-ready",
- },
- },
- "event": "marimo",
- },
- {
- "data": {
- "marimo": {
- "op": "reconnected",
- },
- },
- "event": "marimo",
- "id": "3",
- },
- ]
- `);
- events.close();
- });
-});
diff --git a/frontend/src/core/websocket/transports/notebook.ts b/frontend/src/core/websocket/transports/notebook.ts
index aa728a3fca..ca4cfdd658 100644
--- a/frontend/src/core/websocket/transports/notebook.ts
+++ b/frontend/src/core/websocket/transports/notebook.ts
@@ -157,34 +157,3 @@ export const idle: NotebookTransport = ({ signal }, { open }) =>
open();
signal.addEventListener("abort", () => settle(), { once: true });
});
-
-/**
- * Follow the notebook running in the WASM worker.
- *
- * The worker sends marimo notifications as `{ data: notification }`, which
- * arrive as `marimo` events.
- */
-export function pyodide(
- attach: (consumer: (message: MessageEvent<string>) => void) => void,
-): NotebookTransport {
- return ({ signal }, { open, message }) =>
- new Promise((settle) => {
- open();
- attach(({ data }) => {
- if (signal.aborted) {
- return;
- }
- const value = jsonParseWithSpecialChar(data);
- // The worker sends notifications without the host's event envelope.
- message(
- typeof value === "object" &&
- value !== null &&
- !("event" in value) &&
- "data" in value
- ? { event: "marimo", data: { marimo: value.data } }
- : value,
- );
- });
- signal.addEventListener("abort", () => settle(), { once: true });
- });
-}
diff --git a/marimo/_pyodide/bootstrap.py b/marimo/_pyodide/bootstrap.py
index 00c9c74051..4ab2acab03 100644
--- a/marimo/_pyodide/bootstrap.py
+++ b/marimo/_pyodide/bootstrap.py
@@ -10,15 +10,11 @@ from marimo._runtime._wasm import ensure_wasm_runtime_bootstrapped
ensure_wasm_runtime_bootstrapped()
from marimo._config.config import merge_config
-from marimo._messaging.notification import (
- ConsumerCapabilities,
- KernelCapabilitiesNotification,
- KernelReadyNotification,
-)
from marimo._messaging.serde import (
deserialize_kernel_message,
serialize_kernel_message,
)
+from marimo._pyodide.kernel_ready import kernel_ready
from marimo._runtime.commands import (
AppMetadata,
CreateNotebookCommand,
@@ -76,7 +72,7 @@ def instantiate(
def create_session(
filename: str,
query_params: SerializedQueryParams,
- message_callback: Callable[[str], None],
+ message_callback: Callable[[str], None] | None,
user_config: MarimoConfig,
) -> tuple[PyodideSession, PyodideBridge]:
"""
@@ -85,8 +81,9 @@ def create_session(
Args:
filename: The filename of the app.
query_params: The query parameters from the URL.
- message_callback: A callback that can be used to send messages to the
- frontend.
+ message_callback: Receives every notification as JSON, starting with
+ kernel-ready, or None when the caller follows the notebook's
+ event stream instead.
user_config: The user configuration.
Returns:
@@ -97,6 +94,7 @@ def create_session(
ensure_wasm_runtime_bootstrapped()
def write_kernel_message(notification: KernelMessage) -> None:
+ assert message_callback is not None
data_json_str = notification.decode("utf-8")
name = deserialize_kernel_message(notification).name
text = f'{{"op": "{name}", "data": {data_json_str}}}'
@@ -121,34 +119,18 @@ def create_session(
)
app = app_file_manager.app
- # We want this message to be performant, so any expensive operations
- # should be after this message is sent
- write_kernel_message(
- serialize_kernel_message(
- KernelReadyNotification(
- codes=tuple(app.cell_manager.codes()),
- names=tuple(app.cell_manager.names()),
- configs=tuple(app.cell_manager.configs()),
- cell_ids=tuple(app.cell_manager.cell_ids()),
- layout=app_file_manager.read_layout_config(),
- resumed=False,
- ui_values={},
- last_executed_code={},
- last_execution_time={},
- app_config=app.config,
- kiosk=False,
- capabilities=KernelCapabilitiesNotification(),
- consumer_capabilities=ConsumerCapabilities.EDITOR,
- )
- ),
- )
+ if message_callback is not None:
+ # We want this message to be performant, so any expensive operations
+ # should be after this message is sent
+ write_kernel_message(
+ serialize_kernel_message(kernel_ready(app_file_manager, None))
+ )
from marimo._pyodide.pyodide_session import PyodideBridge, PyodideSession
session = PyodideSession(
app_file_manager,
SessionMode.EDIT,
- write_kernel_message,
AppMetadata(
query_params=query_params,
filename=filename,
@@ -162,6 +144,8 @@ def create_session(
user_config,
)
+ if message_callback is not None:
+ session.consumers.append(write_kernel_message)
bridge = PyodideBridge(session)
return session, bridge
diff --git a/marimo/_pyodide/kernel_ready.py b/marimo/_pyodide/kernel_ready.py
new file mode 100644
index 0000000000..04dc3d1c85
--- /dev/null
+++ b/marimo/_pyodide/kernel_ready.py
@@ -0,0 +1,38 @@
+# Copyright 2026 Marimo. All rights reserved.
+"""The kernel-ready notification for a worker session."""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from marimo._messaging.notification import (
+ ConsumerCapabilities,
+ KernelCapabilitiesNotification,
+ KernelReadyNotification,
+)
+
+if TYPE_CHECKING:
+ from marimo._session.notebook import AppFileManager
+ from marimo._session.state.session_view import SessionView
+
+
+def kernel_ready(
+ app_manager: AppFileManager, view: SessionView | None
+) -> KernelReadyNotification:
+ """Describe the app's cells, resumed from `view` when the session has run."""
+ app = app_manager.app
+ return KernelReadyNotification(
+ codes=tuple(app.cell_manager.codes()),
+ names=tuple(app.cell_manager.names()),
+ configs=tuple(app.cell_manager.configs()),
+ cell_ids=tuple(app.cell_manager.cell_ids()),
+ layout=app_manager.read_layout_config(),
+ resumed=view is not None,
+ ui_values=dict(view.ui_values) if view else {},
+ last_executed_code=dict(view.last_executed_code) if view else {},
+ last_execution_time=dict(view.last_execution_time) if view else {},
+ app_config=app.config,
+ kiosk=False,
+ capabilities=KernelCapabilitiesNotification(),
+ consumer_capabilities=ConsumerCapabilities.EDITOR,
+ )
diff --git a/marimo/_pyodide/notebook.py b/marimo/_pyodide/notebook.py
index 4d5ac83a7e..893518339b 100644
--- a/marimo/_pyodide/notebook.py
+++ b/marimo/_pyodide/notebook.py
@@ -19,15 +19,21 @@ from marimo._core.operations import Mutations, problem
from marimo._messaging.notification import (
NotebookDocumentTransactionNotification,
)
-from marimo._messaging.serde import serialize_kernel_message
+from marimo._messaging.serde import (
+ deserialize_kernel_message,
+ serialize_kernel_message,
+)
+from marimo._pyodide.kernel_ready import kernel_ready
from marimo._server.host import document as documents
-from marimo._server.host.events import serialize_event
+from marimo._server.host.events import notification_event, serialize_event
from marimo._session.model import SessionMode
if TYPE_CHECKING:
from collections.abc import Callable, Mapping, Sequence
from marimo._core.stream import Sink
+ from marimo._messaging.notification import NotificationMessage
+ from marimo._messaging.types import KernelMessage
from marimo._pyodide.pyodide_session import PyodideSession
@@ -131,15 +137,40 @@ class PyodideNotebook:
raise RuntimeError("The notebook is closed")
self.refresh()
self.unsubscribe(subscription_id)
- # Snapshot and subscription are established in the same event-loop turn.
+
+ def notify(notification: NotificationMessage) -> None:
+ emit(serialize_event(notification_event(self.id, notification)))
+
+ def consume(message: KernelMessage) -> None:
+ notify(deserialize_kernel_message(message))
+
+ # Snapshot and subscriptions are established in the same event-loop turn.
initial = self.host.events(cursor, notebook_id=self.id)
- self._subscriptions[subscription_id] = self.host.subscribe(
+ unsubscribe = self.host.subscribe(
lambda event: emit(serialize_event(event)),
notebook_id=self.id,
)
+ session = self._session
+ session.consumers.append(consume)
+
+ def close() -> None:
+ unsubscribe()
+ session.consumers.remove(consume)
+
+ self._subscriptions[subscription_id] = close
try:
for event in initial:
emit(serialize_event(event))
+ # As on a server connection, kernel-ready and the session's
+ # retained notifications follow the snapshot.
+ view = session.session_view
+ notify(
+ kernel_ready(
+ session.app_manager, view if session.instantiated else None
+ )
+ )
+ for notification in view.snapshot_notifications():
+ notify(notification)
except BaseException:
self.unsubscribe(subscription_id)
raise
diff --git a/marimo/_pyodide/pyodide_session.py b/marimo/_pyodide/pyodide_session.py
index 15b9c79ddd..ae64fa75cf 100644
--- a/marimo/_pyodide/pyodide_session.py
+++ b/marimo/_pyodide/pyodide_session.py
@@ -128,7 +128,6 @@ class PyodideSession:
self,
app: AppFileManager,
mode: SessionMode,
- on_write: Callable[[KernelMessage], None],
app_metadata: AppMetadata,
user_config: MarimoConfig,
) -> None:
@@ -139,7 +138,6 @@ class PyodideSession:
self.mode = mode
self.app_metadata = app_metadata
self._queue_manager = AsyncQueueManager()
- self.session_consumer = on_write
self.session_view = SessionView()
self.instantiated = False
self._initial_user_config = user_config
@@ -148,14 +146,19 @@ class PyodideSession:
self._queue_manager.set_ui_element_queue,
)
+ # Notebook subscriptions add themselves to receive notifications.
self.consumers: list[Callable[[KernelMessage], None]] = [
- lambda msg: self.session_consumer(msg),
- lambda msg: self.session_view.add_raw_notification(msg),
+ self.session_view.add_raw_notification,
]
def _on_message(self, msg: KernelMessage) -> None:
- for consumer in self.consumers:
- consumer(msg)
+ # A consumer may unsubscribe during delivery, and one that fails
+ # must not keep the notification from the others.
+ for consumer in list(self.consumers):
+ try:
+ consumer(msg)
+ except Exception:
+ LOGGER.exception("A notification consumer failed")
async def start(self) -> None:
self.kernel_task = _launch_pyodide_kernel(
diff --git a/tests/_pyodide/test_bootstrap.py b/tests/_pyodide/test_bootstrap.py
index d547691d90..7eaaa26e1f 100644
--- a/tests/_pyodide/test_bootstrap.py
+++ b/tests/_pyodide/test_bootstrap.py
@@ -8,6 +8,8 @@ import pytest
from marimo._config.config import DEFAULT_CONFIG
from marimo._messaging.msgspec_encoder import encode_json_str
+from marimo._messaging.notification import VariablesNotification
+from marimo._messaging.serde import serialize_kernel_message
from marimo._pyodide.bootstrap import create_session, save_file
from marimo._pyodide.pyodide_session import PyodideSession
from marimo._server.models.models import SaveNotebookRequest
@@ -197,3 +199,34 @@ async def test_message_callback_format(
parsed = json.loads(received_messages[0])
assert parsed["op"] == "kernel-ready"
assert isinstance(parsed["data"], dict)
+
+
+async def test_message_callback_receives_later_notifications(
+ mock_app_file: Path,
+) -> None:
+ received: list[str] = []
+ session, _ = create_session(
+ filename=str(mock_app_file),
+ query_params={},
+ message_callback=received.append,
+ user_config=DEFAULT_CONFIG,
+ )
+ session._on_message(
+ serialize_kernel_message(VariablesNotification(variables=[]))
+ )
+ assert [json.loads(message)["op"] for message in received] == [
+ "kernel-ready",
+ "variables",
+ ]
+
+
+async def test_create_session_without_message_callback(
+ mock_app_file: Path,
+) -> None:
+ session, _ = create_session(
+ filename=str(mock_app_file),
+ query_params={},
+ message_callback=None,
+ user_config=DEFAULT_CONFIG,
+ )
+ assert session.consumers == [session.session_view.add_raw_notification]
diff --git a/tests/_pyodide/test_pyodide_acceptance.mjs b/tests/_pyodide/test_pyodide_acceptance.mjs
index cae796d04d..13be8b12da 100644
--- a/tests/_pyodide/test_pyodide_acceptance.mjs
+++ b/tests/_pyodide/test_pyodide_acceptance.mjs
@@ -300,7 +300,13 @@ json.dumps(result)
const notebook = pyodide.runPython("bridge.notebook");
const frames = [];
try {
- await notebook.subscribe("acceptance", null, (raw) => frames.push(JSON.parse(raw)));
+ // Host events only. Notifications follow them on the same stream.
+ await notebook.subscribe("acceptance", null, (raw) => {
+ const frame = JSON.parse(raw);
+ if (frame.event !== "marimo") {
+ frames.push(frame);
+ }
+ });
assert.equal(frames.at(-1).event, "ready");
const cell = frames.find((frame) => frame.event === "cell").data.cell;
const call = JSON.stringify({
diff --git a/tests/_pyodide/test_pyodide_notebook.py b/tests/_pyodide/test_pyodide_notebook.py
index 4a1644586b..84abb77c95 100644
--- a/tests/_pyodide/test_pyodide_notebook.py
+++ b/tests/_pyodide/test_pyodide_notebook.py
@@ -13,6 +13,11 @@ if TYPE_CHECKING:
from marimo._ast.app_config import _AppConfig
from marimo._config.config import DEFAULT_CONFIG
from marimo._core.calls import EditCall, dispatch
+from marimo._messaging.notification import (
+ VariableDeclarationNotification,
+ VariablesNotification,
+)
+from marimo._messaging.serde import serialize_kernel_message
from marimo._pyodide.bootstrap import instantiate
from marimo._pyodide.pyodide_session import PyodideBridge, PyodideSession
from marimo._runtime.commands import (
@@ -37,7 +42,6 @@ def _():
session = PyodideSession(
AppFileManager(str(filename)),
SessionMode.EDIT,
- lambda _: None,
AppMetadata(
query_params={},
cli_args={},
@@ -51,14 +55,27 @@ def _():
bridge.close()
-async def snapshot(bridge, subscription="reader", cursor=None):
+async def snapshot(
+ bridge, subscription="reader", cursor=None, notifications=None
+):
+ """Follow host events, and marimo notifications into `notifications`."""
events = []
- await bridge.notebook.subscribe(
- subscription, cursor, lambda raw: events.append(json.loads(raw))
- )
+
+ def receive(raw):
+ event = json.loads(raw)
+ if event["event"] != "marimo":
+ events.append(event)
+ elif notifications is not None:
+ notifications.append(event["data"]["marimo"])
+
+ await bridge.notebook.subscribe(subscription, cursor, receive)
return events
+def ops(notifications):
+ return [notification["op"] for notification in notifications]
+
+
async def send(bridge, call):
return json.loads(await bridge.notebook.call(json.dumps(call)))
@@ -225,3 +242,57 @@ async def test_invalid_calls(bridge):
assert json.loads(await bridge.notebook.call("{"))["status"] == 400
other = {**request, "params": {"notebook_id": "elsewhere"}}
assert (await send(bridge, other))["status"] == 404
+
+
+async def test_kernel_ready_follows_the_snapshot(bridge):
+ frames = []
+ await bridge.notebook.subscribe(
+ "reader", None, lambda raw: frames.append(json.loads(raw))
+ )
+ kinds = [frame["event"] for frame in frames]
+ assert kinds[:4] == ["notebook", "cell", "document", "ready"]
+ assert set(kinds[4:]) == {"marimo"}
+ kernel_ready = frames[4]["data"]["marimo"]
+ assert kernel_ready["op"] == "kernel-ready"
+ assert kernel_ready["codes"] == ["x = 1"]
+ assert kernel_ready["resumed"] is False
+
+
+async def test_a_later_subscription_resumes_with_retained_notifications(
+ bridge,
+):
+ session = bridge.session
+ session.instantiated = True
+ variables = VariablesNotification(
+ variables=[
+ VariableDeclarationNotification(
+ name="x", declared_by=[], used_by=[]
+ )
+ ]
+ )
+ session._on_message(serialize_kernel_message(variables))
+ notifications = []
+ await snapshot(bridge, notifications=notifications)
+ assert ops(notifications)[0] == "kernel-ready"
+ assert notifications[0]["resumed"] is True
+ assert "variables" in ops(notifications)
+
+
+async def test_live_notifications_reach_each_subscription_until_it_closes(
+ bridge,
+):
+ first, second = [], []
+ await snapshot(bridge, "first", notifications=first)
+ await snapshot(bridge, "second", notifications=second)
+ variables = VariablesNotification(variables=[])
+ bridge.session._on_message(serialize_kernel_message(variables))
+ assert ops(first)[-1] == ops(second)[-1] == "variables"
+ bridge.notebook.unsubscribe("first")
+ count = len(first)
+ bridge.session._on_message(serialize_kernel_message(variables))
+ assert len(first) == count
+ assert ops(second)[-2:] == ["variables", "variables"]
+ bridge.notebook.unsubscribe("second")
+ assert bridge.session.consumers == [
+ bridge.session.session_view.add_raw_notification
+ ]
diff --git a/tests/_pyodide/test_pyodide_session.py b/tests/_pyodide/test_pyodide_session.py
index 370a121cca..260ccba2a7 100644
--- a/tests/_pyodide/test_pyodide_session.py
+++ b/tests/_pyodide/test_pyodide_session.py
@@ -122,13 +122,9 @@ def mock_pyodide() -> Generator[MagicMock, None, None]:
async def pyodide_session(
pyodide_app_file: Path,
) -> AsyncGenerator[PyodideSession, None]:
- def _on_write(msg: KernelMessage) -> None:
- pass
-
session = PyodideSession(
app=AppFileManager(filename=str(pyodide_app_file)),
mode=SessionMode.EDIT,
- on_write=_on_write,
app_metadata=AppMetadata(
query_params={},
cli_args={},
@@ -761,6 +757,28 @@ async def test_pyodide_session_on_message(
mock_consumer2.assert_called_once_with(test_msg)
+async def test_pyodide_session_on_message_isolates_consumers(
+ pyodide_session: PyodideSession,
+) -> None:
+ """A failing or departing consumer does not keep the message from others."""
+ received: list[str] = []
+
+ def failing(_msg: KernelMessage) -> None:
+ raise RuntimeError("receiver is gone")
+
+ def departing(_msg: KernelMessage) -> None:
+ received.append("departing")
+ pyodide_session.consumers.remove(departing)
+
+ pyodide_session.consumers = [
+ failing,
+ departing,
+ lambda _msg: received.append("last"),
+ ]
+ pyodide_session._on_message(b"{}")
+ assert received == ["departing", "last"]
+
+
async def test_pyodide_session_put_completion_request(
pyodide_session: PyodideSession,
) -> None:
diff --git a/tests/_pyodide/test_wasm_concurrency_matrix.mjs b/tests/_pyodide/test_wasm_concurrency_matrix.mjs
index 8589f13ee2..412ff064d6 100644
--- a/tests/_pyodide/test_wasm_concurrency_matrix.mjs
+++ b/tests/_pyodide/test_wasm_concurrency_matrix.mjs
@@ -340,16 +340,15 @@ if __name__ == "__main__":
app.run()
"""
-def create_wasm_concurrency_session(notebook_path, callback_name):
+async def create_wasm_concurrency_session(notebook_path, callback_name):
os.makedirs(os.path.dirname(notebook_path), exist_ok=True)
with open(notebook_path, "w") as f:
f.write(WASM_CONCURRENCY_NOTEBOOK)
- session, bridge = create_session(
- notebook_path,
- {},
- getattr(js, callback_name),
- {},
+ session, bridge = create_session(notebook_path, {}, None, {})
+ # Notifications arrive on the notebook stream, after its snapshot.
+ await bridge.notebook.subscribe(
+ "concurrency-matrix", None, getattr(js, callback_name)
)
session_task = None
@@ -369,8 +368,8 @@ def create_wasm_concurrency_session(notebook_path, callback_name):
}
function createSession(pyodide, notebookPath, callbackName) {
- return pyodide.runPython(`
-create_wasm_concurrency_session(
+ return pyodide.runPythonAsync(`
+await create_wasm_concurrency_session(
${JSON.stringify(notebookPath)},
${JSON.stringify(callbackName)},
)
@@ -388,9 +387,9 @@ async function waitUntil(condition, label, timeoutName, fallback) {
}
function sessionNotification(message) {
- // Pyodide session callbacks receive an envelope with the notification payload
- // in `data`.
- return JSON.parse(message).data;
+ // The notebook stream carries notifications as marimo events.
+ const frame = JSON.parse(message);
+ return frame.event === "marimo" ? frame.data.marimo : {};
}
async function waitForKernelReady(messages, label) {
@@ -483,7 +482,7 @@ async function main() {
assertMatrixSupportFilesCompile(pyodide, matrixSupportFiles);
installSessionFactory(pyodide);
- const [bridge, init, stopSession] = createSession(
+ const [bridge, init, stopSession] = await createSession(
pyodide,
"/home/pyodide/wasm_concurrency_matrix_notebook.py",
"marimoWasmConcurrencyMessageCallback",
@@ -601,7 +600,7 @@ with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
const liveStopExitedPath =
"/home/pyodide/wasm_concurrency_live_stop_exited.json";
removePythonFiles(pyodide, [liveStopStartedPath, liveStopExitedPath]);
- const [liveStopBridge, liveStopInit, liveStopSessionStop] = createSession(
+ const [liveStopBridge, liveStopInit, liveStopSessionStop] = await createSession(
pyodide,
"/home/pyodide/wasm_concurrency_live_stop_notebook.py",
"marimoWasmConcurrencyLiveStopMessageCallback",
@@ -679,7 +678,7 @@ live_thread.start()
const postStopResultPath =
"/home/pyodide/wasm_concurrency_post_stop_context.json";
removePythonFiles(pyodide, [postStopResultPath]);
- const [postStopBridge, postStopInit, postStopSessionStop] = createSession(
+ const [postStopBridge, postStopInit, postStopSessionStop] = await createSession(
pyodide,
"/home/pyodide/wasm_concurrency_post_stop_notebook.py",
"marimoWasmConcurrencyPostStopMessageCallback",