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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: add defensive null checks for output channel access (#7883) (#9065)
Myles Scolnickb89c2f73428e
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsxindex 55a4ad661e..34786e6a2f 100644--- a/frontend/src/components/editor/Output.tsx+++ b/frontend/src/components/editor/Output.tsx@@ -367,7 +367,7 @@ export const OutputArea = React.memo(forceExpand,className,}: OutputAreaProps) => {- if (output === null) {+ if (output == null) {return null;}if (output.channel === "output" && output.data === "") {diff --git a/frontend/src/components/editor/__tests__/Output.test.tsx b/frontend/src/components/editor/__tests__/Output.test.tsxindex deae467f15..cb36f7ed89 100644--- a/frontend/src/components/editor/__tests__/Output.test.tsx+++ b/frontend/src/components/editor/__tests__/Output.test.tsx@@ -1,7 +1,9 @@/* Copyright 2026 Marimo. All rights reserved. */import { render, screen } from "@testing-library/react";import { describe, expect, it } from "vitest";-import { OutputRenderer } from "../Output";+import { cellId } from "@/__tests__/branded";+import { TooltipProvider } from "@/components/ui/tooltip";+import { OutputArea, OutputRenderer } from "../Output";describe("OutputRenderer renderFallback prop", () => {it("should use renderFallback for unsupported mimetypes", () => {@@ -65,6 +67,39 @@ describe("OutputRenderer renderFallback prop", () => {});});+describe("OutputArea null/undefined handling", () => {+ it("should render null when output is null", () => {+ const { container } = render(+ <TooltipProvider>+ <OutputArea+ output={null}+ cellId={cellId("test")}+ stale={false}+ loading={false}+ allowExpand={true}+ />+ </TooltipProvider>,+ );+ expect(container.innerHTML).toBe("");+ });++ it("should render null when output is undefined", () => {+ const { container } = render(+ <TooltipProvider>+ <OutputArea+ // @ts-expect-error -- testing runtime safety for undefined output+ output={undefined}+ cellId={cellId("test")}+ stale={false}+ loading={false}+ allowExpand={true}+ />+ </TooltipProvider>,+ );+ expect(container.innerHTML).toBe("");+ });+});+describe("OutputRenderer image and SVG rendering", () => {const plainSvgString ='<svg><rect x="0" y="0" width="10" height="10"></rect></svg>';diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.tsindex 5a11e44e56..2fde4e8c82 100644--- a/frontend/src/core/cells/__tests__/cells.test.ts+++ b/frontend/src/core/cells/__tests__/cells.test.ts@@ -1209,6 +1209,47 @@ describe("cell reducer", () => {]);});+ it("does not crash when setStdinResponse has out-of-bounds outputIndex", () => {+ const STDOUT: OutputMessage = {+ channel: "stdout",+ mimetype: "text/plain",+ data: "hello!",+ timestamp: 1,+ };++ // Set the cell to running with a console output+ actions.prepareForRun({ cellId: firstCellId });+ actions.handleCellMessage({+ cell_id: firstCellId,+ output: undefined,+ console: null,+ status: "running",+ stale_inputs: null,+ timestamp: new Date(20).getTime() as Seconds,+ });+ actions.handleCellMessage({+ cell_id: firstCellId,+ output: undefined,+ console: STDOUT,+ status: undefined,+ stale_inputs: null,+ timestamp: new Date(22).getTime() as Seconds,+ });++ // Try to set stdin response with an out-of-bounds index+ // This should not crash - it should return state unchanged+ actions.setStdinResponse({+ response: "test",+ cellId: firstCellId,+ outputIndex: 999,+ });++ // Cell state should be unchanged+ const cell = cells[0];+ expect(cell.consoleOutputs).toHaveLength(1);+ expect(cell.consoleOutputs[0]).toMatchObject(STDOUT);+ });+it("can receive console when the cell is idle and will clear when starts again", () => {const OLD_STDOUT: OutputMessage = {channel: "stdout",diff --git a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.tsindex 1e8e771ec3..2f4ae21a1a 100644--- a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts+++ b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts@@ -241,6 +241,44 @@ describe("collapseConsoleOutputs", () => {expect(result[2].data).toBe("<pre>E\nF\nG\nH\n</pre>");});+ it("should not crash when truncating with a single output at the limit boundary", () => {+ // Create outputs that push truncation to the exact boundary+ const consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/html",+ channel: "output",+ data: "<div>html1</div>",+ timestamp: 0,+ },+ {+ mimetype: "text/html",+ channel: "output",+ data: "<div>html2</div>",+ timestamp: 0,+ },+ ];+ // With limit=1, truncation must handle edge cases gracefully+ const result = collapseConsoleOutputs(consoleOutputs, 1);+ expect(result[0].data).toContain("Streaming output truncated");+ });++ it("should handle truncation when cutoff indexes past the end of the array", () => {+ // With maxLines=0, the truncation loop never runs, causing cutoff+ // to index past the array. This exercises the `output == null`+ // defensive branch in truncateHead().+ const consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/html",+ channel: "output",+ data: "<div>content</div>",+ timestamp: 0,+ },+ ];+ const result = collapseConsoleOutputs(consoleOutputs, 0);+ expect(result).toHaveLength(1);+ expect(result[0].data).toContain("Streaming output truncated");+ });+describe("ANSI escape sequences", () => {it("should handle cursor movement with collapse", () => {const consoleOutputs: OutputMessage[] = [diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.tsindex 059515185d..f7ec8483af 100644--- a/frontend/src/core/cells/cells.ts+++ b/frontend/src/core/cells/cells.ts@@ -947,7 +947,7 @@ const {cellReducer: (cell) => {const consoleOutputs = [...cell.consoleOutputs];const stdinOutput = consoleOutputs[outputIndex];- if (stdinOutput.channel !== "stdin") {+ if (stdinOutput == null || stdinOutput.channel !== "stdin") {Logger.warn("Expected stdin output");return cell;}diff --git a/frontend/src/core/cells/collapseConsoleOutputs.tsx b/frontend/src/core/cells/collapseConsoleOutputs.tsxindex 32639950bd..b1107e9606 100644--- a/frontend/src/core/cells/collapseConsoleOutputs.tsx+++ b/frontend/src/core/cells/collapseConsoleOutputs.tsx@@ -108,6 +108,9 @@ function truncateHead(consoleOutputs: OutputMessage[], limit: number) {timestamp: -1,};const output = consoleOutputs[cutoff];+ if (output == null) {+ return [warningOutput, ...consoleOutputs.slice(cutoff + 1)];+ }if (output.mimetype === "text/plain") {invariant(typeof output.data === "string", "expected string");const outputLines = output.data.split("\n");