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.

Apply runtime reports only to the runtime they name (#11206)

Trevor Manz01f66fdba442
diff --git a/marimo/_core/backend.py b/marimo/_core/backend.py
index bb335a9d98..92241ca044 100644
--- a/marimo/_core/backend.py
+++ b/marimo/_core/backend.py
@@ -3,11 +3,11 @@
```python
notebook = host.add_notebook(record, backend)
-notebook.start() # Calls backend.start(...).
+runtime, _ = notebook.start() # Calls backend.start(runtime.id).
-# The backend reports kernel startup through Notebook.observe.
-notebook.observe(Starting("Launching kernel"))
-notebook.observe(Running())
+# The backend reports on that runtime through Notebook.observe.
+notebook.observe(Starting(runtime.id, "Launching kernel"))
+notebook.observe(Running(runtime.id))
```
marimo currently manages sessions and kernels outside the core model.
@@ -29,6 +29,8 @@ class Backend(Protocol):
Commands return without waiting for completion. Implementations report
updates through `Notebook.observe` and assign execution and item IDs.
+ Runtime reports name the runtime they concern, so a report from a
+ kernel the notebook no longer tracks is ignored.
"""
def start(self, runtime_id: protocol.RuntimeId) -> None:
@@ -38,8 +40,8 @@ class Backend(Protocol):
`Exited` with a reason on failure.
"""
- def stop(self) -> None:
- """Stop the kernel and report `Exited` when shutdown completes."""
+ def stop(self, runtime_id: protocol.RuntimeId) -> None:
+ """Stop the runtime's kernel and report `Exited` once it has."""
def run(self, run_id: protocol.RunId, code: str) -> None:
"""Evaluate scratch code and report the run's progress and output."""
@@ -54,13 +56,14 @@ class Backend(Protocol):
"""
-# Runtime lifecycle
+# Runtime reports
@dataclass(frozen=True)
class Starting:
"""Kernel startup progress, with an optional status message."""
+ runtime_id: protocol.RuntimeId
message: str | None = None
@@ -68,6 +71,7 @@ class Starting:
class Running:
"""The kernel is ready to execute code."""
+ runtime_id: protocol.RuntimeId
marimo_version: str | None = None
@@ -75,9 +79,13 @@ class Running:
class Exited:
"""The kernel stopped or exited unexpectedly."""
+ runtime_id: protocol.RuntimeId
reason: str | None = None
+RuntimeReport = Starting | Running | Exited
+
+
# Runs
diff --git a/marimo/_core/notebook.py b/marimo/_core/notebook.py
index c4e4d91a5e..7aec4a4859 100644
--- a/marimo/_core/notebook.py
+++ b/marimo/_core/notebook.py
@@ -60,8 +60,8 @@ class Notebook:
forget=backend.forget,
)
self._replacement: protocol.Runtime | None = None
- # Associate runtime events with the pending start, stop, or restart.
- self._lifecycle_request: str | None = None
+ # Correlate runtime events with the pending start, stop, or restart.
+ self._runtime_request: str | None = None
def expire(self) -> None:
"""Apply the host's result retention policy."""
@@ -95,7 +95,7 @@ class Notebook:
if self._runtime is not None:
return self._runtime, False
runtime = self._new_runtime("starting", None, None)
- self._lifecycle_request = request_id
+ self._runtime_request = request_id
self._set_runtime(runtime)
self._backend.start(runtime.id)
return runtime, True
@@ -113,7 +113,7 @@ class Notebook:
with self._stream.deferred():
self._require_open()
runtime = self._require_runtime()
- self._lifecycle_request = request_id
+ self._runtime_request = request_id
self._replacement = None
if runtime.status == "terminating":
return runtime
@@ -122,7 +122,7 @@ class Notebook:
if runtime.status == "failed":
self._end_runtime()
else:
- self._backend.stop()
+ self._backend.stop(runtime.id)
return terminating
def restart(self, *, request_id: str | None = None) -> protocol.Runtime:
@@ -142,14 +142,43 @@ class Notebook:
raise Conflict(
f"the runtime is {runtime.status}; only a running one restarts"
)
- self._lifecycle_request = request_id
+ self._runtime_request = request_id
replacement = self._new_runtime("starting", None, None)
self._replacement = replacement
terminating = replace(runtime, status="terminating")
self._set_runtime(terminating)
- self._backend.stop()
+ self._backend.stop(runtime.id)
return replacement
+ # TODO: Remove once sessions start their runtime through `start`.
+ # marimo still creates kernels for browser tabs behind the notebook's
+ # back, so the host learns about them afterwards.
+ def adopt(
+ self, attach: Callable[[protocol.RuntimeId], None]
+ ) -> protocol.Runtime:
+ """Track a kernel started outside the host API.
+
+ Returns a starting runtime. Calls `attach` with its ID before
+ delivering events to subscribers. The callback binds the backend
+ synchronously and reports status, including attachment failures,
+ through `observe`.
+
+ Raises:
+ Conflict: If the notebook has a runtime that has not failed.
+ """
+ with self._stream.deferred():
+ self._require_open()
+ if self._runtime is not None:
+ if self._runtime.status != "failed":
+ raise Conflict(
+ f"the notebook has a {self._runtime.status} runtime"
+ )
+ self._end_runtime()
+ runtime = self._new_runtime("starting", None, None)
+ self._set_runtime(runtime)
+ attach(runtime.id)
+ return runtime
+
def _require_runtime(self) -> protocol.Runtime:
if self._runtime is None:
raise NoRuntime(
@@ -159,16 +188,16 @@ class Notebook:
def _set_runtime(self, runtime: protocol.Runtime) -> None:
self._runtime = runtime
- self._publish(runtime, self._lifecycle_request)
+ self._publish(runtime, self._runtime_request)
if runtime.status in ("running", "failed"):
- self._lifecycle_request = None
+ self._runtime_request = None
def _end_runtime(self) -> None:
assert self._runtime is not None
self._state.interrupt()
runtime, self._runtime = self._runtime, None
- self._remove("runtime", runtime.id, self._lifecycle_request)
- self._lifecycle_request = None
+ self._remove("runtime", runtime.id, self._runtime_request)
+ self._runtime_request = None
# ---- runs ----------------------------------------------------------
@@ -246,50 +275,43 @@ class Notebook:
if self._closed:
return
match msg:
- case be.Starting(message=text):
- self._observe_starting(text)
- case be.Running(marimo_version=version):
- self._observe_running(version)
- case be.Exited(reason=reason):
- self._observe_exited(reason)
+ case be.Starting() | be.Running() | be.Exited():
+ runtime = self._runtime
+ if runtime is None or msg.runtime_id != runtime.id:
+ return # A kernel this notebook no longer tracks.
+ self._observe_runtime(runtime, msg)
case _:
self._state.observe(msg, self._runtime)
- def _observe_starting(self, text: str | None) -> None:
- if self._runtime is None:
- # Track kernels started outside the host API.
- self._set_runtime(self._new_runtime("starting", text, None))
- elif self._runtime.status == "failed":
- self._replace_runtime(text)
- elif self._runtime.status == "starting":
- self._set_runtime(replace(self._runtime, message=text))
-
- def _observe_running(self, version: str | None) -> None:
- if self._runtime is None:
- self._set_runtime(self._new_runtime("running", None, version))
- return
- if self._runtime.status == "failed":
- self._replace_runtime()
- if self._runtime.status in ("starting", "running"):
- self._set_runtime(
- replace(
- self._runtime,
- status="running",
- message=None,
- marimo_version=version,
- )
- )
+ def _observe_runtime(
+ self, runtime: protocol.Runtime, report: be.RuntimeReport
+ ) -> None:
+ match report:
+ case be.Starting(message=text):
+ if runtime.status == "starting":
+ self._set_runtime(replace(runtime, message=text))
+ case be.Running(marimo_version=version):
+ if runtime.status in ("starting", "running"):
+ self._set_runtime(
+ replace(
+ runtime,
+ status="running",
+ message=None,
+ marimo_version=version,
+ )
+ )
+ case be.Exited(reason=reason):
+ self._observe_exited(runtime, reason)
- def _observe_exited(self, reason: str | None) -> None:
- runtime = self._runtime
- if runtime is None:
- return
+ def _observe_exited(
+ self, runtime: protocol.Runtime, reason: str | None
+ ) -> None:
if runtime.status == "terminating" and self._replacement is not None:
replacement, self._replacement = self._replacement, None
- request_id = self._lifecycle_request
+ request_id = self._runtime_request
with self._stream.deferred():
self._end_runtime()
- self._lifecycle_request = request_id
+ self._runtime_request = request_id
self._set_runtime(replacement)
self._backend.start(replacement.id)
elif runtime.status == "terminating":
@@ -301,12 +323,6 @@ class Notebook:
replace(runtime, status="failed", message=reason)
)
- def _replace_runtime(self, text: str | None = None) -> None:
- request_id = self._lifecycle_request
- self._end_runtime()
- self._lifecycle_request = request_id
- self._set_runtime(self._new_runtime("starting", text, None))
-
def _new_runtime(
self,
status: protocol.RuntimeStatus,
diff --git a/marimo/_server/host/backend.py b/marimo/_server/host/backend.py
index 0443a475f7..a432a244cc 100644
--- a/marimo/_server/host/backend.py
+++ b/marimo/_server/host/backend.py
@@ -45,6 +45,15 @@ _PHASES = {
Observe = Callable[[be.Message], None]
+@dataclass
+class _Kernel:
+ """The session backing one runtime, once it exists."""
+
+ runtime_id: protocol.RuntimeId
+ session: Session | None = None
+ stop_requested: bool = False
+
+
@dataclass
class _Run:
session: Session
@@ -73,24 +82,28 @@ class SessionBackend(SessionConsumer):
"""Resolve the notebook's current path after file moves."""
self._manager = manager
self._observe = observe
- self.session: Session | None = None
- self._stop_requested = False
+ self._kernel: _Kernel | None = None
self._run: _Run | None = None
self._translators: dict[str, RunTranslator] = {}
+ @property
+ def session(self) -> Session | None:
+ """The session backing the current runtime, once attached."""
+ return None if self._kernel is None else self._kernel.session
+
# Backend commands
def start(self, runtime_id: protocol.RuntimeId) -> None:
- self._stop_requested = False
+ kernel = self._kernel = _Kernel(runtime_id)
asyncio.get_running_loop().create_task(
- self._start(SessionId(f"host-{runtime_id}")),
+ self._start(kernel, SessionId(f"host-{runtime_id}")),
name=f"host.start.{runtime_id}",
)
- async def _start(self, session_id: SessionId) -> None:
+ async def _start(self, kernel: _Kernel, session_id: SessionId) -> None:
file = self._file()
if file is None:
- self._observe(be.Exited("The notebook has no file to run"))
+ self._fail(kernel, "The notebook has no file to run")
return
try:
await self._manager.create_session(
@@ -105,16 +118,24 @@ class SessionBackend(SessionConsumer):
LOGGER.warning(
"Kernel for %s failed to start", file, exc_info=True
)
- self._observe(be.Exited(str(e) or type(e).__name__))
+ self._fail(kernel, str(e) or type(e).__name__)
return
# Apply a stop requested during startup.
- if self._stop_requested:
- self.stop()
+ if kernel.stop_requested:
+ self.stop(kernel.runtime_id)
- def stop(self) -> None:
- session = self.session
+ def _fail(self, kernel: _Kernel, reason: str) -> None:
+ if self._kernel is kernel:
+ self._kernel = None
+ self._observe(be.Exited(kernel.runtime_id, reason))
+
+ def stop(self, runtime_id: protocol.RuntimeId) -> None:
+ kernel = self._kernel
+ if kernel is None or kernel.runtime_id != runtime_id:
+ return
+ session = kernel.session
if session is None:
- self._stop_requested = True
+ kernel.stop_requested = True
return
for session_id, candidate in self._manager.sessions.items():
if candidate is session:
@@ -207,11 +228,20 @@ class SessionBackend(SessionConsumer):
# Existing sessions
- def adopt(self, session: Session) -> None:
- """Observe an existing session."""
+ def adopt(self, runtime_id: protocol.RuntimeId, session: Session) -> None:
+ """Observe an existing session as the given runtime."""
if self.session is not None:
return
- session.connect_consumer(self, main=False)
+ kernel = self._kernel = _Kernel(runtime_id)
+ try:
+ session.connect_consumer(self, main=False)
+ except Exception as e:
+ LOGGER.warning("Failed to adopt session", exc_info=True)
+ # Detachment must not report a second exit or close the session
+ # that another client owns.
+ self._kernel = None
+ session.disconnect_consumer(self)
+ self._fail(kernel, str(e) or type(e).__name__)
# Session consumer
@@ -224,14 +254,18 @@ class SessionBackend(SessionConsumer):
def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:
del event_bus
- self.session = session
- self._observe(be.Running(__version__))
+ kernel = self._kernel
+ if kernel is None:
+ return
+ kernel.session = session
+ self._observe(be.Running(kernel.runtime_id, __version__))
def on_detach(self) -> None:
"""Report session closure and release any waiting run."""
- session, self.session = self.session, None
- if session is None:
+ kernel, self._kernel = self._kernel, None
+ if kernel is None or kernel.session is None:
return
+ session = kernel.session
reason = None
if session.kernel_state() is KernelState.STOPPED:
info = session.kernel_exit_info()
@@ -240,14 +274,14 @@ class SessionBackend(SessionConsumer):
if run is not None and run.task is not None:
run.task.cancel()
self._translators.clear()
- self._observe(be.Exited(reason))
+ self._observe(be.Exited(kernel.runtime_id, reason))
def notify(self, notification: KernelMessage) -> None:
message = deserialize_kernel_message(notification)
if isinstance(message, StartupProgressNotification):
- self._observe(
- be.Starting(_PHASES.get(message.phase, message.phase))
- )
+ if (kernel := self._kernel) is not None:
+ phase = _PHASES.get(message.phase, message.phase)
+ self._observe(be.Starting(kernel.runtime_id, phase))
return
if not isinstance(
message,
diff --git a/marimo/_server/host/bridge.py b/marimo/_server/host/bridge.py
index 2fef9178fe..b8904b1355 100644
--- a/marimo/_server/host/bridge.py
+++ b/marimo/_server/host/bridge.py
@@ -211,7 +211,16 @@ class SessionBridge(SessionEventListener):
if notebook is None:
LOGGER.debug("Not publishing session for %s", path)
return
- self._backends[notebook.id].adopt(session)
+ backend = self._backends[notebook.id]
+ if backend.session is not None:
+ return
+ try:
+ self.host.notebook(notebook.id).adopt(
+ lambda runtime_id: backend.adopt(runtime_id, session)
+ )
+ except Conflict:
+ LOGGER.debug("Not publishing a second session for %s", path)
+ return
# Session events
diff --git a/tests/_core/conftest.py b/tests/_core/conftest.py
index 40c9d3f9e4..29668ad550 100644
--- a/tests/_core/conftest.py
+++ b/tests/_core/conftest.py
@@ -22,8 +22,8 @@ class ScriptedBackend:
def start(self, runtime_id: str) -> None:
self.calls.append(("start", runtime_id))
- def stop(self) -> None:
- self.calls.append(("stop",))
+ def stop(self, runtime_id: str) -> None:
+ self.calls.append(("stop", runtime_id))
def run(self, run_id: str, code: str) -> None:
self.calls.append(("run", run_id, code))
@@ -35,6 +35,12 @@ class ScriptedBackend:
self.calls.append(("forget", run_id))
+def runtime_id_of(notebook: Notebook) -> protocol.RuntimeId:
+ """The current runtime's ID, for reports about it."""
+ assert notebook.runtime is not None
+ return notebook.runtime.id
+
+
def project(project_id: str = "p1") -> protocol.Project:
return protocol.Project(
id=protocol.ProjectId(project_id),
diff --git a/tests/_core/test_execution_state.py b/tests/_core/test_execution_state.py
index 9ff60d12d7..353ce01c34 100644
--- a/tests/_core/test_execution_state.py
+++ b/tests/_core/test_execution_state.py
@@ -11,7 +11,13 @@ from marimo._core.execution_state import RetentionPolicy
from marimo._core.host import Host
from marimo._core.notebook import Notebook
from marimo._core.stream import Event
-from tests._core.conftest import ScriptedBackend, names, project, record
+from tests._core.conftest import (
+ ScriptedBackend,
+ names,
+ project,
+ record,
+ runtime_id_of,
+)
class Clock:
@@ -30,7 +36,8 @@ def host_at(clock: Clock, history: int = 1000) -> tuple[Host, Notebook]:
)
host.update_project(project())
notebook = host.add_notebook(record(), ScriptedBackend())
- notebook.observe(be.Running())
+ notebook.adopt(lambda _: None)
+ notebook.observe(be.Running(runtime_id_of(notebook)))
return host, notebook
@@ -216,7 +223,7 @@ def test_stopping_starts_expiry_for_active_work_without_extending_old_results()
active = notebook.run("sleep", request_id="active")
plan(notebook, active, "active")
notebook.stop()
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime_id_of(notebook)))
clock.now = 10
host.expire()
state = fold(host.events(notebook_id=notebook.id))
diff --git a/tests/_core/test_host.py b/tests/_core/test_host.py
index 37ea2a7bfa..b838eb01cb 100644
--- a/tests/_core/test_host.py
+++ b/tests/_core/test_host.py
@@ -18,6 +18,7 @@ from tests._core.conftest import (
names,
project,
record,
+ runtime_id_of,
)
if TYPE_CHECKING:
@@ -30,7 +31,7 @@ def test_catalog_snapshot_and_replay_exclude_runs(host: Host) -> None:
host.update_project(project())
notebook = host.add_notebook(record(), ScriptedBackend())
notebook.start()
- notebook.observe(be.Running())
+ notebook.observe(be.Running(runtime_id_of(notebook)))
run = notebook.run("1", request_id="r1")
cursor = host.events()[-1].id
assert cursor is not None
@@ -63,7 +64,7 @@ def test_a_notebook_stream_sees_only_its_notebook(host: Host) -> None:
first = host.add_notebook(record("n1", "a.py"), ScriptedBackend())
second = host.add_notebook(record("n2", "b.py"), ScriptedBackend())
first.start()
- first.observe(be.Running())
+ first.observe(be.Running(runtime_id_of(first)))
run = first.run("1", request_id="r1")
first.observe(
be.Planned(
@@ -137,7 +138,7 @@ def test_subscribe_filters_the_live_stream(host: Host) -> None:
unsubscribe = host.subscribe(runtimes.append, types={"runtime"})
notebook.start()
- notebook.observe(be.Running())
+ notebook.observe(be.Running(runtime_id_of(notebook)))
run = notebook.run("1", request_id="r1")
unsubscribe()
host.update_project(project("p2"))
@@ -151,7 +152,7 @@ def test_removing_a_notebook_removes_what_it_owns_first(host: Host) -> None:
host.update_project(project())
notebook = host.add_notebook(record(), ScriptedBackend())
notebook.start()
- notebook.observe(be.Running())
+ notebook.observe(be.Running(runtime_id_of(notebook)))
notebook.attach()
run = notebook.run("1", request_id="r1")
log: list[Event] = []
@@ -178,7 +179,7 @@ def test_updating_a_notebook_publishes_only_changes(host: Host) -> None:
backend = ScriptedBackend()
notebook = host.add_notebook(record(), backend)
notebook.start()
- notebook.observe(be.Running())
+ notebook.observe(be.Running(runtime_id_of(notebook)))
run = notebook.run("1", request_id="run")
runtime = notebook.runtime
backend.calls.clear()
@@ -220,8 +221,8 @@ def test_removed_notebook_handles_cannot_be_reused(
backend: ScriptedBackend,
log: list[Event],
) -> None:
- notebook.start()
- notebook.observe(be.Running())
+ runtime, _ = notebook.start()
+ notebook.observe(be.Running(runtime.id))
run = notebook.run("1", request_id="r1")
attachment = notebook.attach()
retained_id = notebook.id
@@ -231,6 +232,7 @@ def test_removed_notebook_handles_cannot_be_reused(
commands: list[Callable[[], object]] = [
notebook.start,
+ lambda: notebook.adopt(lambda _: None),
notebook.stop,
notebook.restart,
lambda: notebook.run("1", request_id=run.id),
@@ -242,9 +244,9 @@ def test_removed_notebook_handles_cannot_be_reused(
command()
# Late backend updates and disconnect cleanup cannot revive the notebook.
- notebook.observe(be.Starting("Late startup"))
- notebook.observe(be.Running())
- notebook.observe(be.Exited())
+ notebook.observe(be.Starting(runtime.id, "Late startup"))
+ notebook.observe(be.Running(runtime.id))
+ notebook.observe(be.Exited(runtime.id))
notebook.detach(attachment.id)
host.remove_notebook(retained_id)
diff --git a/tests/_core/test_notebook.py b/tests/_core/test_notebook.py
index d91e7fe44b..1b38a0942e 100644
--- a/tests/_core/test_notebook.py
+++ b/tests/_core/test_notebook.py
@@ -30,16 +30,24 @@ from marimo._core.protocol import (
RunMessage,
RunOutcome,
Runtime,
+ RuntimeId,
RuntimeMessage,
ValueOutcome,
)
from marimo._core.stream import Event
-from tests._core.conftest import ScriptedBackend, names, request_ids
+from tests._core.conftest import (
+ ScriptedBackend,
+ names,
+ request_ids,
+ runtime_id_of,
+)
def running(notebook: Notebook, backend: ScriptedBackend) -> Runtime:
notebook.start(request_id="req-start")
- notebook.observe(be.Running(marimo_version="0.25.0"))
+ notebook.observe(
+ be.Running(runtime_id_of(notebook), marimo_version="0.25.0")
+ )
backend.calls.clear()
assert notebook.runtime is not None
return notebook.runtime
@@ -106,8 +114,12 @@ class TestStart:
self, notebook: Notebook, log: list[Event]
) -> None:
notebook.start(request_id="req-1")
- notebook.observe(be.Starting("Preparing the environment"))
- notebook.observe(be.Running(marimo_version="0.25.0"))
+ notebook.observe(
+ be.Starting(runtime_id_of(notebook), "Preparing the environment")
+ )
+ notebook.observe(
+ be.Running(runtime_id_of(notebook), marimo_version="0.25.0")
+ )
notebook.attach()
assert names(log) == ["runtime", "runtime", "runtime", "attachment"]
@@ -121,7 +133,9 @@ class TestStart:
self, notebook: Notebook, log: list[Event]
) -> None:
notebook.start(request_id="req-1")
- notebook.observe(be.Exited("No such environment"))
+ notebook.observe(
+ be.Exited(runtime_id_of(notebook), "No such environment")
+ )
assert notebook.runtime is not None
assert notebook.runtime.status == "failed"
@@ -133,15 +147,15 @@ class TestStop:
def test_stop_cancels_a_pending_restart(
self, notebook: Notebook, backend: ScriptedBackend, log: list[Event]
) -> None:
- running(notebook, backend)
+ runtime = running(notebook, backend)
notebook.restart(request_id="restart")
notebook.stop(request_id="stop")
log.clear()
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime.id))
assert notebook.runtime is None
- assert backend.calls == [("stop",)]
+ assert backend.calls == [("stop", runtime.id)]
assert names(log) == ["runtime.removed"]
assert request_ids(log) == ["stop"]
@@ -157,9 +171,9 @@ class TestStop:
runtime = notebook.stop(request_id="req-stop")
assert runtime.status == "terminating"
- assert backend.calls == [("stop",)]
+ assert backend.calls == [("stop", runtime.id)]
- notebook.observe(be.Exited("Stopped"))
+ notebook.observe(be.Exited(runtime_id_of(notebook), "Stopped"))
assert notebook.runtime is None
assert names(log) == ["runtime", "runtime.removed"]
assert request_ids(log) == ["req-stop", "req-stop"]
@@ -177,7 +191,7 @@ class TestStop:
log.clear()
notebook.stop()
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime_id_of(notebook)))
assert names(log) == [
"runtime",
@@ -201,7 +215,7 @@ class TestStop:
self, notebook: Notebook, backend: ScriptedBackend, log: list[Event]
) -> None:
notebook.start()
- notebook.observe(be.Exited("boom"))
+ notebook.observe(be.Exited(runtime_id_of(notebook), "boom"))
backend.calls.clear()
log.clear()
@@ -228,7 +242,7 @@ class TestStop:
log.clear()
notebook.stop(request_id="stop")
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime_id_of(notebook)))
assert names(log) == [
"runtime",
@@ -266,12 +280,17 @@ class TestRestart:
assert notebook.runtime is not None
assert notebook.runtime.id == before.id
assert notebook.runtime.status == "terminating"
- assert backend.calls == [("stop",)]
- notebook.observe(be.Exited())
+ assert backend.calls == [("stop", before.id)]
+ notebook.observe(be.Exited(before.id))
assert notebook.runtime is not None
assert notebook.runtime.id == response.id
- assert backend.calls == [("stop",), ("start", notebook.runtime.id)]
- notebook.observe(be.Running(marimo_version="0.25.0"))
+ assert backend.calls == [
+ ("stop", before.id),
+ ("start", notebook.runtime.id),
+ ]
+ notebook.observe(
+ be.Running(runtime_id_of(notebook), marimo_version="0.25.0")
+ )
after = notebook.runtime
assert after is not None
@@ -292,24 +311,79 @@ class TestRestart:
assert request_ids(log)[0] == "req-restart"
assert request_ids(log)[-1] == "req-restart"
+ def test_a_late_exit_from_the_old_kernel_leaves_the_replacement(
+ self, notebook: Notebook, backend: ScriptedBackend
+ ) -> None:
+ before = running(notebook, backend)
+ replacement = notebook.restart()
+ notebook.observe(be.Exited(before.id))
+
+ notebook.observe(be.Exited(before.id))
+
+ assert notebook.runtime is not None
+ assert notebook.runtime.id == replacement.id
+ assert notebook.runtime.status == "starting"
+ assert backend.calls == [
+ ("stop", before.id),
+ ("start", replacement.id),
+ ]
+
class TestAdoption:
- @pytest.mark.parametrize("reports_starting", [False, True])
+ def test_a_kernel_someone_else_started_is_published(
+ self, notebook: Notebook, backend: ScriptedBackend, log: list[Event]
+ ) -> None:
+ adopted = notebook.adopt(lambda _: None)
+ notebook.observe(
+ be.Starting(runtime_id_of(notebook), "Starting the kernel")
+ )
+ notebook.observe(
+ be.Running(runtime_id_of(notebook), marimo_version="0.25.0")
+ )
+
+ assert adopted.status == "starting"
+ assert backend.calls == []
+ assert notebook.runtime is not None
+ assert notebook.runtime.id == adopted.id
+ assert notebook.runtime.status == "running"
+ assert names(log) == ["runtime", "runtime", "runtime"]
+ assert request_ids(log) == [UNSET, UNSET, UNSET]
+
+ def test_reports_about_other_kernels_change_nothing(
+ self, notebook: Notebook, backend: ScriptedBackend, log: list[Event]
+ ) -> None:
+ stranger = RuntimeId("rt-stranger")
+ notebook.observe(be.Running(stranger, marimo_version="0.25.0"))
+ assert notebook.runtime is None
+
+ runtime = running(notebook, backend)
+ log.clear()
+ notebook.observe(be.Starting(stranger, "Starting the kernel"))
+ notebook.observe(be.Exited(stranger, "gone"))
+
+ assert notebook.runtime == runtime
+ assert log == []
+
+ def test_only_a_failed_runtime_can_be_replaced(
+ self, notebook: Notebook
+ ) -> None:
+ notebook.start()
+ with pytest.raises(Conflict, match="starting"):
+ notebook.adopt(lambda _: None)
+ notebook.observe(be.Running(runtime_id_of(notebook)))
+ with pytest.raises(Conflict, match="running"):
+ notebook.adopt(lambda _: None)
+
def test_external_relaunch_recovers_a_failed_runtime(
- self,
- notebook: Notebook,
- backend: ScriptedBackend,
- host: Host,
- reports_starting: bool,
+ self, notebook: Notebook, backend: ScriptedBackend, host: Host
) -> None:
before = running(notebook, backend)
- run = scratch(notebook)
- notebook.observe(be.Exited("Kernel exited"))
+ scratch(notebook)
+ notebook.observe(be.Exited(runtime_id_of(notebook), "Kernel exited"))
backend.calls.clear()
- if reports_starting:
- notebook.observe(be.Starting("Relaunching"))
- notebook.observe(be.Running("0.26.0"))
+ notebook.adopt(lambda _: None)
+ notebook.observe(be.Running(runtime_id_of(notebook), "0.26.0"))
assert notebook.runtime is not None
assert notebook.runtime.id != before.id
@@ -325,17 +399,6 @@ class TestAdoption:
run = notebook.run("1", request_id="after-relaunch")
assert backend.calls == [("run", run.id, "1")]
- def test_a_kernel_someone_else_started_is_published(
- self, notebook: Notebook, backend: ScriptedBackend, log: list[Event]
- ) -> None:
- notebook.observe(be.Starting("Starting the kernel"))
- notebook.observe(be.Running(marimo_version="0.25.0"))
-
- assert backend.calls == []
- assert notebook.runtime is not None
- assert notebook.runtime.status == "running"
- assert names(log) == ["runtime", "runtime"]
-
def test_a_kernel_that_dies_on_its_own_fails_and_keeps_results(
self,
host: Host,
@@ -347,7 +410,7 @@ class TestAdoption:
run = scratch(notebook)
log.clear()
- notebook.observe(be.Exited("killed"))
+ notebook.observe(be.Exited(runtime_id_of(notebook), "killed"))
assert notebook.runtime is not None
assert notebook.runtime.status == "failed"
diff --git a/tests/_core/test_stream.py b/tests/_core/test_stream.py
index 25d1c8936b..28e4df677d 100644
--- a/tests/_core/test_stream.py
+++ b/tests/_core/test_stream.py
@@ -111,7 +111,7 @@ def test_subscribers_act_after_the_command_reaches_the_backend(
runtime, _ = notebook.start()
- assert backend.calls == [("start", runtime.id), ("stop",)]
+ assert backend.calls == [("start", runtime.id), ("stop", runtime.id)]
assert notebook.runtime is not None
assert notebook.runtime.status == "terminating"
@@ -119,8 +119,8 @@ def test_subscribers_act_after_the_command_reaches_the_backend(
def test_subscribers_cannot_interrupt_a_restart(
host: Host, notebook: Notebook, backend: ScriptedBackend
) -> None:
- notebook.start()
- notebook.observe(be.Running())
+ runtime, _ = notebook.start()
+ notebook.observe(be.Running(runtime.id))
replacement = notebook.restart()
backend.calls.clear()
started: list[tuple[protocol.Runtime, bool]] = []
@@ -131,7 +131,31 @@ def test_subscribers_cannot_interrupt_a_restart(
host.subscribe(start_on_removal, types={"runtime"})
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime.id))
assert backend.calls == [("start", replacement.id)]
assert started == [(replacement, False)]
+
+
+def test_subscribers_cannot_start_a_runtime_during_adoption(
+ host: Host, notebook: Notebook, backend: ScriptedBackend
+) -> None:
+ runtime, _ = notebook.start()
+ notebook.observe(be.Exited(runtime.id, "Kernel died"))
+ backend.calls.clear()
+ started: list[tuple[protocol.Runtime, bool]] = []
+ attached: list[protocol.RuntimeId] = []
+
+ def start_on_removal(event: Event) -> None:
+ if event.name == "runtime.removed":
+ started.append(notebook.start())
+
+ host.subscribe(start_on_removal, types={"runtime"})
+
+ replacement = notebook.adopt(attached.append)
+
+ assert (attached, started, backend.calls) == (
+ [replacement.id],
+ [(replacement, False)],
+ [],
+ )
diff --git a/tests/_server/host/conftest.py b/tests/_server/host/conftest.py
index b5fd12d4d2..d2d18198d7 100644
--- a/tests/_server/host/conftest.py
+++ b/tests/_server/host/conftest.py
@@ -43,6 +43,13 @@ class FakeSession:
self.consumers.append((consumer, main))
consumer.on_attach(self, self.event_bus) # type: ignore[arg-type]
+ def disconnect_consumer(self, consumer: SessionConsumer) -> None:
+ for entry in self.consumers:
+ if entry[0] is consumer:
+ self.consumers.remove(entry)
+ consumer.on_detach()
+ return
+
def instantiate(self, request: Any, *, http_request: Any) -> None:
del request, http_request
diff --git a/tests/_server/host/test_host_api.py b/tests/_server/host/test_host_api.py
index 80a3526617..5d6db882ca 100644
--- a/tests/_server/host/test_host_api.py
+++ b/tests/_server/host/test_host_api.py
@@ -23,7 +23,7 @@ from marimo._server.host.index import WorkspaceIndex
from marimo._server.main import create_starlette_app
from marimo._server.models.home import MarimoFile
from marimo._server.workspace import FixedFilesWorkspace, SingleFileWorkspace
-from tests._core.conftest import ScriptedBackend
+from tests._core.conftest import ScriptedBackend, runtime_id_of
from tests._server.host.drive import (
PORT,
finished,
@@ -82,7 +82,11 @@ class Harness:
body=b"{}",
)
assert response.status == 202, response.body
- self.host.notebook(self.notebook_id).observe(be.Running("0.25.0"))
+ self.host.notebook(self.notebook_id).observe(
+ be.Running(
+ runtime_id_of(self.host.notebook(self.notebook_id)), "0.25.0"
+ )
+ )
return self.notebook_id
@@ -307,21 +311,29 @@ async def test_runtime_lifecycle_over_http(harness: Harness) -> None:
assert restart.status == 409
assert restart.json()["type"].endswith("/conflict")
- harness.host.notebook(nid).observe(be.Running("0.25.0"))
+ harness.host.notebook(nid).observe(
+ be.Running(runtime_id_of(harness.host.notebook(nid)), "0.25.0")
+ )
restart = await finished(app, "POST", path + "/restart", headers=key("k4"))
assert restart.status == 202
assert restart.json()["status"] == "starting"
assert restart.json()["id"] != started.json()["id"]
- harness.host.notebook(nid).observe(be.Exited())
- harness.host.notebook(nid).observe(be.Running("0.25.0"))
+ harness.host.notebook(nid).observe(
+ be.Exited(runtime_id_of(harness.host.notebook(nid)))
+ )
+ harness.host.notebook(nid).observe(
+ be.Running(runtime_id_of(harness.host.notebook(nid)), "0.25.0")
+ )
stopped = await finished(app, "DELETE", path, headers=key("k5"))
assert stopped.status == 202
assert stopped.json()["status"] == "terminating"
assert stopped.json()["id"] != started.json()["id"]
assert stopped.json()["id"] == restart.json()["id"]
- harness.host.notebook(nid).observe(be.Exited())
+ harness.host.notebook(nid).observe(
+ be.Exited(runtime_id_of(harness.host.notebook(nid)))
+ )
gone = await finished(app, "DELETE", path, headers=key("k6"))
assert gone.status == 409
assert gone.json()["type"].endswith("/no-runtime")
@@ -339,8 +351,8 @@ async def test_slow_restart_keeps_its_receipt(harness: Harness) -> None:
# The receipt's 30-second window must not begin during shutdown/startup.
harness.now = 100
harness.app.state.host_context.expire()
- notebook.observe(be.Exited())
- notebook.observe(be.Running("0.25.0"))
+ notebook.observe(be.Exited(runtime_id_of(notebook)))
+ notebook.observe(be.Running(runtime_id_of(notebook), "0.25.0"))
calls = list(harness.backend().calls)
retried = await finished(harness.app, "POST", path, headers=key("restart"))
@@ -360,7 +372,7 @@ async def test_slow_restart_keeps_its_receipt(harness: Harness) -> None:
fresh = await finished(harness.app, "POST", path, headers=key("restart"))
assert fresh.status == 202
assert fresh.json()["id"] != accepted.json()["id"]
- assert harness.backend().calls == [*calls, ("stop",)]
+ assert harness.backend().calls == [*calls, ("stop", accepted.json()["id"])]
@pytest.mark.parametrize(
@@ -403,11 +415,12 @@ async def test_lifecycle_receipts_expire_after_terminal_events(
calls,
)
if operation == "restart" and now == 100:
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime_id_of(notebook)))
+ rid = runtime_id_of(notebook)
notebook.observe(
- be.Exited("failed" if failed else None)
+ be.Exited(rid, "failed" if failed else None)
if failed or operation == "stop"
- else be.Running()
+ else be.Running(rid)
)
assert harness.host._stream._listeners == listeners
calls = list(harness.backend().calls)
@@ -446,14 +459,14 @@ async def test_stop_settles_superseded_lifecycle_receipts(
harness.app, method, pending_path, headers=key("pending")
)
if operation == "replacement":
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime_id_of(notebook)))
harness.now = 100
stops = [
await finished(harness.app, "DELETE", path, headers=key(request_id))
for request_id in ("stop-1", "stop-2")
]
harness.now = 200
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime_id_of(notebook)))
assert harness.host._stream._listeners == listeners
calls = list(harness.backend().calls)
harness.now = 229
@@ -489,17 +502,17 @@ async def test_synchronous_lifecycle_completion(
await harness.running()
notebook = harness.host.notebook(nid)
if operation == "failed-stop":
- notebook.observe(be.Exited("failed"))
+ notebook.observe(be.Exited(runtime_id_of(notebook), "failed"))
backend = harness.backend()
start, stop = backend.start, backend.stop
- def start_immediately(runtime_id: str) -> None:
- start(runtime_id)
- notebook.observe(be.Running())
+ def start_immediately(rid: str) -> None:
+ start(rid)
+ notebook.observe(be.Running(protocol.RuntimeId(rid)))
- def stop_immediately() -> None:
- stop()
- notebook.observe(be.Exited())
+ def stop_immediately(rid: str) -> None:
+ stop(rid)
+ notebook.observe(be.Exited(protocol.RuntimeId(rid)))
monkeypatch.setattr(backend, "start", start_immediately)
monkeypatch.setattr(backend, "stop", stop_immediately)
diff --git a/tests/_server/host/test_host_backend.py b/tests/_server/host/test_host_backend.py
index 51802b96f8..42ff53a81b 100644
--- a/tests/_server/host/test_host_backend.py
+++ b/tests/_server/host/test_host_backend.py
@@ -25,6 +25,8 @@ from tests._server.host.conftest import (
Observed,
)
+RT1 = protocol.RuntimeId("rt1")
+
def backend(manager: FakeManager, observed: Observed) -> SessionBackend:
return SessionBackend(
@@ -41,7 +43,7 @@ async def test_start_joins_the_session_as_a_secondary_consumer(
b = backend(manager, observed)
manager.release.clear()
- b.start(protocol.RuntimeId("rt1"))
+ b.start(RT1)
await manager.started.wait()
progress = StartupProgressNotification(
phase="starting-kernel", logs="", log_mode="append"
@@ -58,8 +60,8 @@ async def test_start_joins_the_session_as_a_secondary_consumer(
}
]
assert observed.messages == [
- be.Starting("Starting the kernel"),
- be.Running(__version__),
+ be.Starting(RT1, "Starting the kernel"),
+ be.Running(RT1, __version__),
]
@@ -67,15 +69,15 @@ async def test_a_failed_start_reports_the_reason(
manager: FakeManager, observed: Observed
) -> None:
manager.next_session = RuntimeError("no such environment")
- backend(manager, observed).start(protocol.RuntimeId("rt1"))
+ backend(manager, observed).start(RT1)
await observed.wait_for(be.Exited)
- assert observed.messages == [be.Exited("no such environment")]
+ assert observed.messages == [be.Exited(RT1, "no such environment")]
observed.messages.clear()
manager.next_session = KeyError()
- backend(manager, observed).start(protocol.RuntimeId("rt1"))
+ backend(manager, observed).start(RT1)
await observed.wait_for(be.Exited)
- assert observed.messages == [be.Exited("KeyError")]
+ assert observed.messages == [be.Exited(RT1, "KeyError")]
async def test_a_notebook_without_a_file_cannot_start(
@@ -86,9 +88,11 @@ async def test_a_notebook_without_a_file_cannot_start(
lambda: None,
manager, # type: ignore[arg-type]
observed,
- ).start(protocol.RuntimeId("rt1"))
+ ).start(RT1)
await observed.wait_for(be.Exited)
- assert observed.messages == [be.Exited("The notebook has no file to run")]
+ assert observed.messages == [
+ be.Exited(RT1, "The notebook has no file to run")
+ ]
assert manager.starts == []
@@ -96,15 +100,29 @@ async def test_stop_closes_the_session_and_reports_the_exit(
manager: FakeManager, observed: Observed
) -> None:
b = backend(manager, observed)
- b.start(protocol.RuntimeId("rt1"))
+ b.start(RT1)
await observed.wait_for(be.Running)
observed.messages.clear()
- b.stop()
+ b.stop(RT1)
assert manager.sessions == {}
assert manager.next_session.closed
- assert observed.messages == [be.Exited(None)]
+ assert observed.messages == [be.Exited(RT1, None)]
+
+
+async def test_commands_for_another_runtime_are_ignored(
+ manager: FakeManager, observed: Observed
+) -> None:
+ b = backend(manager, observed)
+ b.start(RT1)
+ await observed.wait_for(be.Running)
+ observed.messages.clear()
+
+ b.stop(protocol.RuntimeId("rt2"))
+
+ assert not manager.next_session.closed
+ assert observed.messages == []
async def test_stop_before_the_session_exists_is_carried_out_after(
@@ -112,9 +130,9 @@ async def test_stop_before_the_session_exists_is_carried_out_after(
) -> None:
b = backend(manager, observed)
manager.release.clear()
- b.start(protocol.RuntimeId("rt1"))
+ b.start(RT1)
await manager.started.wait()
- b.stop()
+ b.stop(RT1)
manager.release.set()
await observed.wait_for(be.Exited)
assert observed.kinds() == ["Running", "Exited"]
@@ -125,7 +143,7 @@ async def test_a_kernel_that_dies_reports_why(
manager: FakeManager, observed: Observed
) -> None:
b = backend(manager, observed)
- b.start(protocol.RuntimeId("rt1"))
+ b.start(RT1)
await observed.wait_for(be.Running)
observed.messages.clear()
session = manager.next_session
@@ -136,7 +154,7 @@ async def test_a_kernel_that_dies_reports_why(
session.close() # what the heartbeat does
- assert observed.messages == [be.Exited("Out of memory")]
+ assert observed.messages == [be.Exited(RT1, "Out of memory")]
async def test_adopting_a_session_reports_it_running(
@@ -144,8 +162,8 @@ async def test_adopting_a_session_reports_it_running(
) -> None:
b = backend(manager, observed)
session = FakeSession()
- b.adopt(session) # type: ignore[arg-type]
- b.adopt(session) # type: ignore[arg-type]
+ b.adopt(RT1, session) # type: ignore[arg-type]
+ b.adopt(RT1, session) # type: ignore[arg-type]
assert observed.kinds() == ["Running"]
assert len(session.consumers) == 1
@@ -157,7 +175,7 @@ async def test_interrupt_waits_for_the_kernel_result(
takes_effect: bool,
) -> None:
b = backend(manager, observed)
- b.start(protocol.RuntimeId("rt1"))
+ b.start(RT1)
await observed.wait_for(be.Running)
session = manager.next_session
b.run(protocol.RunId("run-1"), "while True: pass")
@@ -194,7 +212,7 @@ async def test_a_session_closing_mid_run_releases_the_run(
manager: FakeManager, observed: Observed
) -> None:
b = backend(manager, observed)
- b.start(protocol.RuntimeId("rt1"))
+ b.start(RT1)
await observed.wait_for(be.Running)
session = manager.next_session
b.run(protocol.RunId("run-1"), "1")
@@ -215,7 +233,7 @@ async def test_interrupting_a_run_started_by_another_client(
) -> None:
b = backend(manager, observed)
session = manager.next_session
- b.adopt(session)
+ b.adopt(RT1, session)
session.broadcast(
ExecutionNotification(
cell_id=SCRATCH_CELL_ID,
@@ -242,7 +260,7 @@ async def test_external_scratch_is_adopted_before_compilation(
) -> None:
b = backend(manager, observed)
session = manager.next_session
- b.adopt(session)
+ b.adopt(RT1, session)
observed.messages.clear()
session.broadcast(
ExecutionNotification(
@@ -291,7 +309,7 @@ async def test_canceling_a_queued_run_preserves_other_execution(
b = backend(manager, observed)
session = manager.next_session
session.scratchpad_lock = lock = Lock()
- b.adopt(session)
+ b.adopt(RT1, session)
observed.messages.clear()
async with lock:
lock.entered.clear()
@@ -319,7 +337,7 @@ async def test_submission_failure_is_an_error(
) -> None:
b = backend(manager, observed)
session = manager.next_session
- b.adopt(session)
+ b.adopt(RT1, session)
def failed(*_args: object, **_kwargs: object) -> None:
raise RuntimeError("Kernel channel closed")
diff --git a/tests/_server/host/test_host_bridge.py b/tests/_server/host/test_host_bridge.py
index 085ae97dfb..0b6ab67b94 100644
--- a/tests/_server/host/test_host_bridge.py
+++ b/tests/_server/host/test_host_bridge.py
@@ -7,7 +7,9 @@ from typing import TYPE_CHECKING
import pytest
+from marimo._core import protocol
from marimo._core.errors import Conflict
+from marimo._core.stream import Event
from marimo._messaging.notification import (
CompletedRunNotification,
ExecutionNotification,
@@ -23,6 +25,8 @@ from tests._server.host.conftest import FakeManager, FakeSession
if TYPE_CHECKING:
from pathlib import Path
+ from marimo._session.consumer import SessionConsumer
+
def prepare(manager: FakeManager, root: Path, *names: str) -> None:
files = []
@@ -61,6 +65,84 @@ async def test_sessions_present_and_created_are_adopted(
bridge.close()
+async def test_a_subscriber_can_stop_an_adopted_session(
+ manager: FakeManager, tmp_path: Path
+) -> None:
+ prepare(manager, tmp_path, "a.py")
+ bridge = SessionBridge(manager) # type: ignore[arg-type]
+ bridge.open()
+ record = bridge.index.notebook_for(str(tmp_path / "a.py"))
+ assert record is not None
+ notebook = bridge.host.notebook(record.id)
+ session = FakeSession(str(tmp_path / "a.py"))
+ manager.sessions["browser"] = session
+
+ def stop_on_starting(event: Event) -> None:
+ if (
+ isinstance(event.data, protocol.RuntimeMessage)
+ and event.data.runtime.status == "starting"
+ ):
+ notebook.stop()
+
+ bridge.host.subscribe(stop_on_starting, types={"runtime"})
+
+ await manager.event_bus.emit_session_created(session) # type: ignore[arg-type]
+
+ assert (session.closed, notebook.runtime, manager.sessions) == (
+ True,
+ None,
+ {},
+ )
+ bridge.close()
+
+
[email protected]("attached", [False, True])
+async def test_failed_adoption_detaches_without_closing_the_session(
+ manager: FakeManager,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ attached: bool,
+) -> None:
+ prepare(manager, tmp_path, "a.py")
+ bridge = SessionBridge(manager) # type: ignore[arg-type]
+ bridge.open()
+ record = bridge.index.notebook_for(str(tmp_path / "a.py"))
+ assert record is not None
+ notebook = bridge.host.notebook(record.id)
+ session = FakeSession(str(tmp_path / "a.py"))
+ connect = session.connect_consumer
+
+ def fail(consumer: SessionConsumer, *, main: bool) -> None:
+ if attached:
+ connect(consumer, main=main)
+ raise RuntimeError("cannot attach")
+
+ monkeypatch.setattr(session, "connect_consumer", fail)
+
+ await manager.event_bus.emit_session_created(session) # type: ignore[arg-type]
+
+ failed = notebook.runtime
+ assert failed is not None
+ backend = bridge._backends[record.id]
+ assert (
+ failed.status,
+ failed.message,
+ backend.session,
+ session.consumers,
+ session.closed,
+ ) == ("failed", "cannot attach", None, [], False)
+
+ replacement = FakeSession(str(tmp_path / "a.py"))
+ await manager.event_bus.emit_session_created(replacement) # type: ignore[arg-type]
+ session.close()
+
+ runtime = notebook.runtime
+ assert runtime is not None
+ assert runtime.id != failed.id
+ assert (runtime.status, backend.session) == ("running", replacement)
+ bridge.close()
+
+
async def test_a_rename_keeps_the_notebook_id(
manager: FakeManager, tmp_path: Path
) -> None:
diff --git a/tests/_server/host/test_host_index.py b/tests/_server/host/test_host_index.py
index 86c6fdef28..936a19eeba 100644
--- a/tests/_server/host/test_host_index.py
+++ b/tests/_server/host/test_host_index.py
@@ -16,7 +16,7 @@ from marimo._core.stream import Event
from marimo._server.host.index import WorkspaceIndex
from marimo._server.models.home import MarimoFile
from marimo._server.workspace import FixedFilesWorkspace
-from tests._core.conftest import ScriptedBackend, names
+from tests._core.conftest import ScriptedBackend, names, runtime_id_of
def workspace(root: Path, *names: str) -> FixedFilesWorkspace:
@@ -136,7 +136,7 @@ def test_deleted_file_keeps_its_runtime_until_stopped(tmp_path: Path) -> None:
(record,) = host.notebooks.values()
notebook = host.notebook(record.id)
notebook.start()
- notebook.observe(be.Running())
+ notebook.observe(be.Running(runtime_id_of(notebook)))
runtime = notebook.runtime
backend.calls.clear()
(tmp_path / "a.py").unlink()
@@ -152,8 +152,9 @@ def test_deleted_file_keeps_its_runtime_until_stopped(tmp_path: Path) -> None:
assert backend.calls == []
notebook.stop()
- notebook.observe(be.Exited())
+ notebook.observe(be.Exited(runtime_id_of(notebook)))
index.refresh()
assert dict(host.notebooks) == {}
- assert backend.calls == [("stop",)]
+ assert runtime is not None
+ assert backend.calls == [("stop", runtime.id)]
diff --git a/tests/_server/host/test_host_record.py b/tests/_server/host/test_host_record.py
index a62f34d6bf..5e60a84e32 100644
--- a/tests/_server/host/test_host_record.py
+++ b/tests/_server/host/test_host_record.py
@@ -19,7 +19,12 @@ from marimo._server.host.lifespan import host as host_lifespan
from marimo._server.host.record import host_url
from marimo._server.main import create_starlette_app
from marimo._utils.lifespans import Lifespans
-from tests._core.conftest import ScriptedBackend, project, record
+from tests._core.conftest import (
+ ScriptedBackend,
+ project,
+ record,
+ runtime_id_of,
+)
from tests._server.host.drive import finished
from tests._server.mocks import get_starlette_server_state_init
@@ -100,7 +105,8 @@ async def test_idle_host_expires_results_and_stops_maintenance(
host.clock = context.clock = lambda: now
host.update_project(project())
notebook = host.add_notebook(record(), ScriptedBackend())
- notebook.observe(be.Running())
+ notebook.adopt(lambda _: None)
+ notebook.observe(be.Running(runtime_id_of(notebook)))
headers = {
"Authorization": f"Bearer {context.token}",
"Idempotency-Key": "run",