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: state resolution (#7110)

Akshay Agrawalb9fa7f475e66
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 8b376d4cdc..578bd77530 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -1456,36 +1456,41 @@ class Kernel:
self.state_updates
)
self.state_updates.clear()
- self.state_updates.clear()
return cells_with_stale_state
def register_state_update(self, state: State[Any]) -> None:
"""Register a state object as having been updated.
- Should be called when a state's setter is called.
+ Should be called when a state's setter is called
"""
- # store the state and the currently executing cell
+ from marimo._runtime.threads import is_marimo_thread
+
ctx = get_context()
assert ctx.execution_context is not None
- cell_id = ctx.execution_context.cell_id
- with self._state_lock:
- self.state_updates[state] = cell_id
- # TODO(akshayka): State should not be updated eagerly like this,
- # the runner already handles state execution. This is a hack
- # until we figure out how to clean this up.
- names = ctx.state_registry.bound_names(state)
- if len(names) == 1 and not (name := list(names)[0]).isidentifier():
- LOGGER.warning(
- f"Found 'name' for state object is not an identifier; got name {name}"
- )
+ setter_cell_id = ctx.execution_context.cell_id
+
+ # When running on the main thread of execution, state updates
+ # are just logged in a data structure; it is the runner's
+ # job to process these later.
+ if not is_marimo_thread():
+ with self._state_lock:
+ self.state_updates[state] = setter_cell_id
return
- to_update = self.update_stateful_values(
- ctx.state_registry.bound_names(state), state._value
- )
- if self.reactive_execution_mode == "autorun":
- # If autorun, run the cells that depend on the state
- self.graph.set_stale(to_update)
+ # Otherwise, when running in a mo.Thread, we eagerly process
+ # state updates.
+ cells_with_stale_state = set()
+ for cid, cell in self.graph.cells.items():
+ # No self-loops
+ if cid == setter_cell_id and not state.allow_self_loops:
+ continue
+ for ref in cell.refs:
+ # run this cell if any of its refs match the state object
+ # by object ID (via is operator)
+ if ref in self.globals and self.globals[ref] is state:
+ cells_with_stale_state.add(cid)
+ self.graph.set_stale(cells_with_stale_state, prune_imports=True)
+ if not self.lazy():
self._execute_stale_cells_callback()
@kernel_tracer.start_as_current_span("delete_cell")
@@ -1886,9 +1891,38 @@ class Kernel:
for name in ctx.ui_element_registry.bound_names(object_id)
if not is_local(name)
}
- referring_cells.update(
- self.update_stateful_values(bound_names, value)
- )
+ variable_values: list[VariableValue] = []
+ for name in bound_names:
+ # TODO update variable values even for namespaces? lenses? etc
+ variable_values.append(
+ VariableValue.create(name=name, value=value)
+ )
+ try:
+ # subtracting self.graph.definitions[name]: never rerun the
+ # cell that created the name
+ referring_cells |= self.graph.get_referring_cells(
+ name, language="python"
+ ) - self.graph.get_defining_cells(name)
+ except Exception:
+ # This is a serious bug that should never be triggered;
+ # it means that we couldn't find a UIElement object
+ # that should exist.
+ sys.stderr.write(
+ "An exception was raised when finding cells that "
+ f"refer to a UIElement value, for bound name {name}. "
+ "This is a bug in marimo. "
+ "Please copy the below traceback and paste it in an "
+ "issue: https://github.com/marimo-team/marimo/issues\n"
+ )
+ tmpio = io.StringIO()
+ traceback.print_exc(file=tmpio)
+ tmpio.seek(0)
+ write_traceback(tmpio.read())
+ # Entering undefined behavior territory ...
+ continue
+
+ if variable_values:
+ VariableValues(variables=variable_values).broadcast()
if self.reactive_execution_mode == "autorun":
await self._run_cells(referring_cells)
@@ -1937,43 +1971,6 @@ class Kernel:
def reset_ui_initializers(self) -> None:
self.ui_initializers = {}
- def update_stateful_values(
- self, bound_names: set[str], value: Any
- ) -> set[CellId_t]:
- variable_values: list[VariableValue] = []
- referring_cells: set[CellId_t] = set()
- for name in bound_names:
- # TODO update variable values even for namespaces? lenses? etc
- variable_values.append(
- VariableValue.create(name=name, value=value)
- )
- try:
- # subtracting self.graph.definitions[name]: never rerun the
- # cell that created the name
- referring_cells |= self.graph.get_referring_cells(
- name, language="python"
- ) - self.graph.get_defining_cells(name)
- except Exception:
- # Internal marimo error
- sys.stderr.write(
- "An exception was raised when finding cells that "
- f"refer to a UIElement value, for bound name {name}. "
- "This is a bug in marimo. "
- "Please copy the below traceback and paste it in an "
- "issue: https://github.com/marimo-team/marimo/issues\n"
- )
- tmpio = io.StringIO()
- traceback.print_exc(file=tmpio)
- tmpio.seek(0)
- write_traceback(tmpio.read())
- # Entering undefined behavior territory ...
- continue
-
- if variable_values:
- VariableValues(variables=variable_values).broadcast()
-
- return referring_cells
-
@kernel_tracer.start_as_current_span("function_call_request")
async def function_call_request(
self, request: FunctionCallRequest
diff --git a/marimo/_runtime/side_effect.py b/marimo/_runtime/side_effect.py
index 94d36a6f8b..914cf22717 100644
--- a/marimo/_runtime/side_effect.py
+++ b/marimo/_runtime/side_effect.py
@@ -31,11 +31,14 @@ class SideEffect(CellLifecycleItem):
Typically hook to expose the object to the context, but the existence of
the object is enough for side effect tracking.
"""
+ del context
def dispose(self, context: RuntimeContext, deletion: bool) -> bool: # noqa: ARG002
"""Clean up and mark the object for deletion"""
# Side effects can always be disposed since they are just a cell level
# marker that some event has occurred.
+ del context
+ del deletion
return True
diff --git a/marimo/_runtime/threads.py b/marimo/_runtime/threads.py
index f47a8e9d89..5a8d41534c 100644
--- a/marimo/_runtime/threads.py
+++ b/marimo/_runtime/threads.py
@@ -5,6 +5,7 @@ import threading
from typing import Any
from marimo._messaging.streams import ThreadSafeStream
+from marimo._output.rich_help import mddoc
from marimo._runtime.cell_lifecycle_item import CellLifecycleItem
from marimo._runtime.context.kernel_context import KernelRuntimeContext
from marimo._runtime.context.script_context import ScriptRuntimeContext
@@ -21,6 +22,7 @@ from marimo._runtime.context.types import (
THREADS: set[int] = set()
+@mddoc
class Thread(threading.Thread):
"""A Thread subclass that can communicate with the frontend.
@@ -40,24 +42,23 @@ class Thread(threading.Thread):
`should_exit` property will evaluate to `True`, at which point it
is the developer's responsibility to clean up their thread.
- **Example.**
+ Examples:
+ ```python
+ def target():
+ import time
+ import marimo as mo
+
+ thread = mo.current_thread()
+ while not thread.should_exit:
+ time.sleep(1)
+ print("hello")
+ ```
- ```python
- def target():
- import time
+ ```python
import marimo as mo
- thread = mo.current_thread()
- while not thread.should_exit:
- time.sleep(1)
- print("hello")
- ```
-
- ```python
- import marimo as mo
-
- mo.Thread(target=target).start()
- ```
+ mo.Thread(target=target).start()
+ ```
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
@@ -174,3 +175,11 @@ def current_thread() -> Thread:
"thread created with mo.Thread."
)
return thread
+
+
+def is_marimo_thread() -> bool:
+ try:
+ current_thread()
+ return True
+ except RuntimeError:
+ return False
diff --git a/marimo/_smoke_tests/state_threading.py b/marimo/_smoke_tests/state_threading.py
new file mode 100644
index 0000000000..3765041d13
--- /dev/null
+++ b/marimo/_smoke_tests/state_threading.py
@@ -0,0 +1,66 @@
+import marimo
+
+__generated_with = "0.17.7"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+ import random
+ return mo, random
+
+
+def _(mo):
+ g, s = mo.state(0)
+
+ g(), s(1)
+ return g, s
+
+
+def _(g, random):
+ g(), (x := random.randint(0, 10))
+ return (x,)
+
+
+def _(x):
+ x
+ return
+
+
+def _(s):
+ s(6)
+ return
+
+
+def _(mo):
+ def f(s):
+ import random
+ import time
+
+ thread = mo.current_thread()
+ while not thread.should_exit:
+ s(random.randint(0, 100000))
+ time.sleep(1)
+ return (f,)
+
+
+def _(f, mo, s):
+ mo.Thread(target=f, args=(s,)).start()
+ return
+
+
+def _(g):
+ g()
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_save/test_hash.py b/tests/_save/test_hash.py
index 5fc822c19b..ca22bd1530 100644
--- a/tests/_save/test_hash.py
+++ b/tests/_save/test_hash.py
@@ -9,6 +9,7 @@ import pytest
from marimo._ast.app import App
from marimo._dependencies.dependencies import DependencyManager
+from marimo._runtime.requests import ExecuteStaleRequest
from marimo._runtime.runtime import Kernel
from tests.conftest import ExecReqProvider
@@ -1389,6 +1390,8 @@ class TestSideEffects:
async def test_side_effect_file(
k: Kernel, exec_req: ExecReqProvider, tmp_path
) -> None:
+ control_requests = []
+ k.enqueue_control_request = lambda req: control_requests.append(req) # type: ignore
await k.run(
[
exec_req.get(
@@ -1402,7 +1405,7 @@ class TestSideEffects:
hashes = []
"""),
- exec_req.get("""
+ r := exec_req.get("""
f = mo.watch.file(tmp_path_fixture / "test.txt")
"""),
]
@@ -1434,7 +1437,11 @@ class TestSideEffects:
)
(tmp_path / "test.txt").touch()
await asyncio.sleep(0.25)
- await k.run([])
+ assert len(control_requests) == 1
+ assert isinstance(control_requests[0], ExecuteStaleRequest)
+ assert k.graph.cells[r.cell_id].stale
+ await k.run_stale_cells()
+
assert not k.stdout.messages, k.stdout
assert not k.stderr.messages, k.stderr
v = k.globals["v"]
@@ -1448,6 +1455,8 @@ class TestSideEffects:
async def test_side_effect_directory(
k: Kernel, exec_req: ExecReqProvider, tmp_path
) -> None:
+ control_requests = []
+ k.enqueue_control_request = lambda req: control_requests.append(req) # type: ignore
await k.run(
[
exec_req.get(
@@ -1493,6 +1502,11 @@ class TestSideEffects:
)
(tmp_path / "test_dir" / "test.txt").write_text("test")
await asyncio.sleep(0.25)
+ assert len(control_requests) == 1
+ assert isinstance(control_requests[0], ExecuteStaleRequest)
+ assert k.graph.cells[r.cell_id].stale
+ await k.run_stale_cells()
+
await k.run([])
assert not k.stdout.messages, k.stdout
assert not k.stderr.messages, k.stderr