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.

Report a stopped stream to its subscribers (#11425)

Trevor Manz7c10985ae8aa
diff --git a/packages/client/src/__tests__/notebook-events.test.ts b/packages/client/src/__tests__/notebook-events.test.ts
index ec9b26acdf..baa43c4d32 100644
--- a/packages/client/src/__tests__/notebook-events.test.ts
+++ b/packages/client/src/__tests__/notebook-events.test.ts
@@ -380,6 +380,100 @@ describe("NotebookEvents", () => {
expect(closing).not.toHaveBeenCalled();
});
+ it.each([false, true])(
+ "owns each registration of a shared event callback (onError: %s)",
+ async (withError) => {
+ const stream = body();
+ const events = new NotebookEvents(url, {
+ types: ["marimo"],
+ fetch: answering(stream.response).fetch,
+ });
+ const received = vi.fn();
+ const leave = events.subscribe(received, withError ? vi.fn() : undefined);
+ events.subscribe(received);
+ leave();
+ leave();
+ stream.push("id: 1\nevent: marimo\ndata: {}\n\n");
+ await vi.waitFor(() => expect(events.cursor).toBe("1"));
+ events.close();
+ expect(received.mock.calls).toEqual([
+ [{ event: "marimo", data: {}, id: "1" }],
+ ]);
+ },
+ );
+
+ it("owns each registration of a shared error callback", async () => {
+ const events = new NotebookEvents(url, {
+ fetch: answering(new Response(null, { status: 403 })).fetch,
+ });
+ const failed = vi.fn();
+ const leave = events.subscribe(vi.fn(), failed);
+ events.subscribe(vi.fn(), failed);
+ leave();
+ events.subscribe(vi.fn(), failed);
+ leave();
+ const error = await events.closed.catch((error: unknown) => error);
+ expect(failed.mock.calls).toEqual([[error], [error]]);
+ });
+
+ it.each(["status", "error"])(
+ "honors unsubscribe during terminal %s delivery",
+ async (during) => {
+ let leave = () => {};
+ const events = new NotebookEvents(url, {
+ fetch: answering(new Response(null, { status: 403 })).fetch,
+ onStatus: (status) => {
+ if (status === "closed" && during === "status") {
+ leave();
+ }
+ },
+ });
+ events.subscribe(vi.fn(), () => {
+ if (during === "error") {
+ leave();
+ }
+ });
+ const removed = vi.fn();
+ const remaining = vi.fn();
+ leave = events.subscribe(vi.fn(), removed);
+ events.subscribe(vi.fn(), remaining);
+ const error = await events.closed.catch((error: unknown) => error);
+ expect(removed.mock.calls).toEqual([]);
+ expect(remaining.mock.calls).toEqual([[error]]);
+ },
+ );
+
+ it("reports a throwing error callback and still notifies other subscribers", async () => {
+ const queued = vi.fn();
+ vi.stubGlobal("queueMicrotask", queued);
+ const thrown = new Error("subscriber failed");
+ const events = new NotebookEvents(url, {
+ fetch: answering(new Response(null, { status: 403 })).fetch,
+ });
+ events.subscribe(vi.fn(), () => {
+ throw thrown;
+ });
+ const remaining = vi.fn();
+ events.subscribe(vi.fn(), remaining);
+ const error = await events.closed.catch((error: unknown) => error);
+ expect(remaining.mock.calls).toEqual([[error]]);
+ expect(queued.mock.calls).toEqual([[expect.any(Function)]]);
+ expect(queued.mock.calls[0][0]).toThrow(thrown);
+ });
+
+ it("does not notify error subscribers when aborted", async () => {
+ const controller = new AbortController();
+ const events = new NotebookEvents(url, {
+ signal: controller.signal,
+ fetch: unanswered(),
+ });
+ const failed = vi.fn();
+ events.subscribe(vi.fn(), failed);
+ controller.abort();
+ await expect(events.closed).resolves.toBeUndefined();
+ expect(failed.mock.calls).toEqual([]);
+ });
+
it.each([
'event: cell\ndata: {"cell":{"id":"c1"}}\n\n',
"event: reset\ndata: 5\n\n",
diff --git a/packages/client/src/transport/events.ts b/packages/client/src/transport/events.ts
index eb0ace9caf..f98e428e46 100644
--- a/packages/client/src/transport/events.ts
+++ b/packages/client/src/transport/events.ts
@@ -145,13 +145,23 @@ export class Events<
onEvent: (event: StreamEvent<K>) => void,
onError?: (error: unknown) => void,
): () => void {
- const unsubscribe = this.#stream.subscribe(onEvent);
- if (onError === undefined || this.#settle === undefined) {
- return unsubscribe;
+ let active = true;
+ const unsubscribe = this.#stream.subscribe((event) => {
+ if (active) {
+ onEvent(event);
+ }
+ });
+ const failed = (error: unknown) => {
+ if (active) {
+ onError?.(error);
+ }
+ };
+ if (onError !== undefined && this.#settle !== undefined) {
+ this.#onErrors.add(failed);
}
- this.#onErrors.add(onError);
return () => {
- this.#onErrors.delete(onError);
+ active = false;
+ this.#onErrors.delete(failed);
unsubscribe();
};
}