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 runtime state registration in error handling (#474)

Myles Scolnick5960bbac370d
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index 494e26de83..e3bf0a747d 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -40,6 +40,7 @@ import useEvent from "react-use-event-hook";
import { CellEditor } from "./cell/code/cell-editor";
import { getEditorCodeAsPython } from "@/core/codemirror/language/utils";
import { outputIsStale } from "@/core/cells/cell";
+import { RuntimeState } from "@/core/kernel/RuntimeState";
/**
* Imperative interface of the cell.
@@ -80,7 +81,6 @@ export interface CellProps
> {
theme: Theme;
showPlaceholder: boolean;
- registerRunStart: () => void;
serializedEditorState: SerializedEditorState | null;
mode: AppMode;
appClosed: boolean;
@@ -110,7 +110,6 @@ const CellComponent = (
interrupted,
errored,
stopped,
- registerRunStart,
serializedEditorState,
mode,
appClosed,
@@ -169,14 +168,18 @@ const CellComponent = (
[editorView, prepareToRunEffects]
);
- const handleRun = useEvent(() => {
+ const handleRun = useEvent(async () => {
if (loading) {
return;
}
const code = prepareToRunEffects();
- registerRunStart();
- sendRun([cellId], [code]);
+
+ RuntimeState.INSTANCE.registerRunStart();
+ await sendRun([cellId], [code]).catch((error) => {
+ Logger.error("Error running cell", error);
+ RuntimeState.INSTANCE.registerRunEnd();
+ });
});
const createBelow = useCallback(
diff --git a/frontend/src/components/editor/cell/useRunCells.ts b/frontend/src/components/editor/cell/useRunCells.ts
index 3a8d9c8b1d..652af3fc5b 100644
--- a/frontend/src/components/editor/cell/useRunCells.ts
+++ b/frontend/src/components/editor/cell/useRunCells.ts
@@ -6,6 +6,7 @@ import { staleCellIds, useNotebook } from "@/core/cells/cells";
import { derefNotNull } from "@/utils/dereference";
import useEvent from "react-use-event-hook";
import { getEditorCodeAsPython } from "@/core/codemirror/language/utils";
+import { Logger } from "@/utils/Logger";
/**
* Creates a function that runs all cells that have been edited or interrupted.
@@ -47,7 +48,10 @@ function useRunCells() {
}
RuntimeState.INSTANCE.registerRunStart();
- await sendRun(cellIds, codes);
+ await sendRun(cellIds, codes).catch((error) => {
+ Logger.error(error);
+ RuntimeState.INSTANCE.registerRunEnd();
+ });
});
return runCells;
diff --git a/frontend/src/components/editor/renderers/CellArray.tsx b/frontend/src/components/editor/renderers/CellArray.tsx
index 1b7daf0a6a..8299baf4dc 100644
--- a/frontend/src/components/editor/renderers/CellArray.tsx
+++ b/frontend/src/components/editor/renderers/CellArray.tsx
@@ -2,7 +2,6 @@
import { useEffect } from "react";
import { sendDeleteCell } from "@/core/network/requests";
import { Cell } from "@/components/editor/Cell";
-import { RuntimeState } from "../../../core/kernel/RuntimeState";
import {
ConnectionStatus,
WebSocketState,
@@ -30,11 +29,6 @@ interface CellArrayProps {
connStatus: ConnectionStatus;
}
-// TODO(akshayka): move running cells state machine to kernel
-function registerRunStart() {
- RuntimeState.INSTANCE.registerRunStart();
-}
-
export const CellArray: React.FC<CellArrayProps> = ({
notebook,
mode,
@@ -106,7 +100,6 @@ export const CellArray: React.FC<CellArrayProps> = ({
stopped={cell.stopped}
runStartTimestamp={cell.runStartTimestamp}
runElapsedTimeMs={cell.runElapsedTimeMs}
- registerRunStart={registerRunStart}
serializedEditorState={cell.serializedEditorState}
showDeleteButton={cells.length > 1}
createNewCell={createNewCell}
diff --git a/frontend/src/core/functions/FunctionRegistry.ts b/frontend/src/core/functions/FunctionRegistry.ts
index d402137324..72ef7b4703 100644
--- a/frontend/src/core/functions/FunctionRegistry.ts
+++ b/frontend/src/core/functions/FunctionRegistry.ts
@@ -15,5 +15,8 @@ export const FUNCTIONS_REGISTRY = new DeferredRequestRegistry<
await sendFunctionRequest({
functionCallId: requestId,
...req,
+ }).catch((error) => {
+ RuntimeState.INSTANCE.registerRunEnd();
+ throw error;
});
});
diff --git a/frontend/src/core/websocket/useMarimoWebSocket.tsx b/frontend/src/core/websocket/useMarimoWebSocket.tsx
index 28bb07c215..dbc5e5e226 100644
--- a/frontend/src/core/websocket/useMarimoWebSocket.tsx
+++ b/frontend/src/core/websocket/useMarimoWebSocket.tsx
@@ -108,6 +108,7 @@ export function useMarimoWebSocket(opts: {
// Start the run
RuntimeState.INSTANCE.registerRunStart();
sendInstantiate({ objectIds, values }).catch((error) => {
+ RuntimeState.INSTANCE.registerRunEnd();
showBoundary(new Error("Failed to instantiate", { cause: error }));
});
}
diff --git a/frontend/src/stories/cell.stories.tsx b/frontend/src/stories/cell.stories.tsx
index c201c3a866..2274ddd603 100644
--- a/frontend/src/stories/cell.stories.tsx
+++ b/frontend/src/stories/cell.stories.tsx
@@ -28,7 +28,6 @@ const props: CellProps = {
stopped: false,
updateCellCode: Logger.log,
prepareForRun: Logger.log,
- registerRunStart: Logger.log,
runStartTimestamp: 0,
runElapsedTimeMs: 10,
serializedEditorState: null,