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: Restart cell in pdb (#4855)

Dylan Madisetti36576ae61e7b
diff --git a/marimo/_runtime/marimo_pdb.py b/marimo/_runtime/marimo_pdb.py
index 7ab95f6af9..e680c1b165 100644
--- a/marimo/_runtime/marimo_pdb.py
+++ b/marimo/_runtime/marimo_pdb.py
@@ -3,7 +3,7 @@ from __future__ import annotations
import inspect
import sys
-from pdb import Pdb
+from pdb import Pdb, Restart as pdbRestart
from typing import TYPE_CHECKING, Any, Optional
from marimo import _loggers
@@ -17,6 +17,38 @@ if TYPE_CHECKING:
LOGGER = _loggers.marimo_logger()
+def try_restart() -> bool:
+ from marimo._runtime.context import (
+ ContextNotInitializedError,
+ get_context,
+ )
+ from marimo._runtime.context.kernel_context import (
+ KernelRuntimeContext,
+ )
+ from marimo._runtime.requests import ExecuteMultipleRequest
+
+ try:
+ ctx = get_context()
+ if ctx is None or not isinstance(ctx, KernelRuntimeContext):
+ return False
+
+ graph = ctx.graph
+ if ctx.cell_id is None or ctx.cell_id not in graph.cells:
+ return False
+
+ # This runs the request and also runs UpdateCellCodes
+ ctx._kernel.enqueue_control_request(
+ ExecuteMultipleRequest(
+ cell_ids=[ctx.cell_id],
+ codes=[graph.cells[ctx.cell_id].code],
+ )
+ )
+ except ContextNotInitializedError:
+ return False
+
+ return True
+
+
class MarimoPdb(Pdb):
# Because we are patching Pdb, we need copy the exact constructor signature
def __init__(
@@ -52,6 +84,21 @@ class MarimoPdb(Pdb):
sys.stdout.write(header)
return super().set_trace(frame)
+ def cmdloop(self, intro: Optional[str] = None) -> None:
+ """Override to gracefully handle restarts."""
+ try:
+ super().cmdloop(intro)
+ except pdbRestart:
+ if not try_restart():
+ LOGGER.warning("Unable to restart cell.")
+
+ def do_run(self, arg: str) -> bool | None:
+ """super.do_run raises an error AND manipulates sys.argv"""
+ del arg # unused
+ raise pdbRestart
+
+ do_restart = do_run
+
def post_mortem_by_cell_id(self, cell_id: CellId_t) -> None:
return self.post_mortem(t=self._last_tracebacks.get(cell_id))
diff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.py
index b3f9d81899..81c62d474b 100644
--- a/marimo/_server/sessions.py
+++ b/marimo/_server/sessions.py
@@ -920,7 +920,7 @@ class SessionManager:
UpdateCellCodes(
cell_ids=cell_ids,
codes=codes,
- code_is_stale=not should_autorun,
+ code_is_stale=True,
),
from_consumer_id=None,
)