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: make threads share their accumulated output (#8418)

Akshay Agrawal71e8b8e1057c
diff --git a/marimo/_plugins/stateless/status/_progress.py b/marimo/_plugins/stateless/status/_progress.py
index e16608677e..2e09b9c0ab 100644
--- a/marimo/_plugins/stateless/status/_progress.py
+++ b/marimo/_plugins/stateless/status/_progress.py
@@ -1,6 +1,7 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
+import threading
import time
from collections.abc import AsyncIterable, Iterable, Sized
from typing import (
@@ -55,6 +56,7 @@ class _Progress(Html):
self.show_rate = show_rate
self.show_eta = show_eta
self.start_time = time.time()
+ self._lock = threading.Lock()
super().__init__(self._get_text())
def update_progress(
@@ -63,7 +65,7 @@ class _Progress(Html):
title: Optional[str] = None,
subtitle: Optional[str] = None,
) -> None:
- """Update the progress indicator.
+ """Update the progress indicator. Thread-safe.
Examples:
Increment by 1:
@@ -83,18 +85,19 @@ class _Progress(Html):
title (str, optional): New title. Defaults to None.
subtitle (str, optional): New subtitle. Defaults to None.
"""
- if self.closed:
- raise RuntimeError(
- "Progress indicators cannot be updated after exiting "
- "the context manager that created them. "
- )
- self.current += increment
- if title is not None:
- self.title = title
- if subtitle is not None:
- self.subtitle = subtitle
-
- self._text = self._get_text()
+ with self._lock:
+ if self.closed:
+ raise RuntimeError(
+ "Progress indicators cannot be updated after exiting "
+ "the context manager that created them. "
+ )
+ self.current += increment
+ if title is not None:
+ self.title = title
+ if subtitle is not None:
+ self.subtitle = subtitle
+
+ self._text = self._get_text()
self.debounced_flush()
@debounce(0.15)
diff --git a/marimo/_runtime/app/kernel_runner.py b/marimo/_runtime/app/kernel_runner.py
index 4d8d9e920f..90ee7a98d7 100644
--- a/marimo/_runtime/app/kernel_runner.py
+++ b/marimo/_runtime/app/kernel_runner.py
@@ -90,15 +90,10 @@ class AppKernelRunner:
run_result: cell_runner.RunResult,
) -> None:
"""Update the app's cached outputs."""
- from marimo._plugins.stateless.flex import vstack
-
del ctx
- if (
- run_result.output is None
- and run_result.accumulated_output is not None
- ):
- self.outputs[cell.cell_id] = vstack(
- run_result.accumulated_output
+ if run_result.output is None and run_result.accumulated_output:
+ self.outputs[cell.cell_id] = (
+ run_result.accumulated_output.stack()
)
else:
self.outputs[cell.cell_id] = run_result.output
diff --git a/marimo/_runtime/cell_output_list.py b/marimo/_runtime/cell_output_list.py
new file mode 100644
index 0000000000..383c8c7fbb
--- /dev/null
+++ b/marimo/_runtime/cell_output_list.py
@@ -0,0 +1,62 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import threading
+from typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+ from marimo._output.hypertext import Html
+
+
+class CellOutputList:
+ """Thread-safe container for a cell's imperatively constructed output."""
+
+ def __init__(self) -> None:
+ self._items: list[Html] = []
+ self._lock = threading.RLock()
+
+ def append(self, item: Html) -> None:
+ with self._lock:
+ self._items.append(item)
+
+ def clear(self) -> None:
+ with self._lock:
+ self._items.clear()
+
+ def replace_at_index(self, item: Html, idx: int) -> None:
+ with self._lock:
+ if idx > len(self._items):
+ raise IndexError(
+ f"idx is {idx}, must be <= {len(self._items)}"
+ )
+ if idx == len(self._items):
+ self._items.append(item)
+ else:
+ self._items[idx] = item
+
+ def remove(self, value: object) -> None:
+ with self._lock:
+ self._items[:] = [
+ item for item in self._items if item is not value
+ ]
+
+ def stack(self) -> Html | None:
+ """Return `vstack` of the items, or `None` if empty."""
+ with self._lock:
+ if self._items:
+ from marimo._plugins.stateless.flex import vstack
+
+ return vstack(self._items)
+ return None
+
+ def __bool__(self) -> bool:
+ with self._lock:
+ return bool(self._items)
+
+ def __len__(self) -> int:
+ with self._lock:
+ return len(self._items)
+
+ def __repr__(self) -> str:
+ with self._lock:
+ return f"CellOutputList({self._items!r})"
diff --git a/marimo/_runtime/context/types.py b/marimo/_runtime/context/types.py
index 51b879f8b9..385c270d4d 100644
--- a/marimo/_runtime/context/types.py
+++ b/marimo/_runtime/context/types.py
@@ -9,11 +9,12 @@ from __future__ import annotations
import abc
import threading
from contextlib import contextmanager
-from dataclasses import dataclass
+from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Optional
from marimo._ast.app_config import _AppConfig
from marimo._messaging.context import HTTP_REQUEST_CTX
+from marimo._runtime.cell_output_list import CellOutputList
if TYPE_CHECKING:
from collections.abc import Iterator
@@ -26,7 +27,6 @@ if TYPE_CHECKING:
)
from marimo._config.config import MarimoConfig
from marimo._messaging.types import Stderr, Stdout, Stream
- from marimo._output.hypertext import Html
from marimo._plugins.ui._core.registry import UIElementRegistry
from marimo._runtime import dataflow
from marimo._runtime.cell_lifecycle_registry import CellLifecycleRegistry
@@ -67,8 +67,8 @@ class ExecutionContext:
# Cell ID corresponding to local graph object, and not prefixed in script
# context.
local_cell_id: Optional[CellId_t] = None
- # output object set imperatively
- output: Optional[list[Html]] = None
+ # outputs set imperatively via mo.output.append and associated functions
+ output: CellOutputList = field(default_factory=CellOutputList)
duckdb_connection: duckdb.DuckDBPyConnection | None = None
@contextmanager
diff --git a/marimo/_runtime/output/_output.py b/marimo/_runtime/output/_output.py
index fd6744c7c6..e9ad75b93b 100644
--- a/marimo/_runtime/output/_output.py
+++ b/marimo/_runtime/output/_output.py
@@ -6,7 +6,6 @@ from marimo._messaging.notification_utils import CellNotificationUtils
from marimo._messaging.tracebacks import write_traceback
from marimo._output import formatting
from marimo._output.rich_help import mddoc
-from marimo._plugins.stateless.flex import vstack
from marimo._runtime.context import get_context
from marimo._runtime.context.types import ContextNotInitializedError
from marimo._types.ids import CellId_t
@@ -42,10 +41,11 @@ def replace(value: object) -> None:
if ctx.execution_context is None:
return
- elif value is None:
- ctx.execution_context.output = None
- else:
- ctx.execution_context.output = [formatting.as_html(value)]
+
+ output = ctx.execution_context.output
+ output.clear()
+ if value is not None:
+ output.append(formatting.as_html(value))
write_internal(cell_id=ctx.execution_context.cell_id, value=value)
@@ -66,19 +66,14 @@ def replace_at_index(value: object, idx: int) -> None:
except ContextNotInitializedError:
return
- if ctx.execution_context is None or ctx.execution_context.output is None:
+ if ctx.execution_context is None:
return
- elif idx > len(ctx.execution_context.output):
- raise IndexError(
- f"idx is {idx}, must be <= {len(ctx.execution_context.output)}"
- )
- elif idx == len(ctx.execution_context.output):
- ctx.execution_context.output.append(formatting.as_html(value))
- else:
- ctx.execution_context.output[idx] = formatting.as_html(value)
+
+ output = ctx.execution_context.output
+ output.replace_at_index(formatting.as_html(value), idx)
write_internal(
cell_id=ctx.execution_context.cell_id,
- value=vstack(ctx.execution_context.output),
+ value=output.stack(),
)
@@ -100,13 +95,11 @@ def append(value: object) -> None:
if ctx.execution_context is None:
return
- if ctx.execution_context.output is None:
- ctx.execution_context.output = [formatting.as_html(value)]
- else:
- ctx.execution_context.output.append(formatting.as_html(value))
+ output = ctx.execution_context.output
+ output.append(formatting.as_html(value))
write_internal(
cell_id=ctx.execution_context.cell_id,
- value=vstack(ctx.execution_context.output),
+ value=output.stack(),
)
@@ -126,11 +119,8 @@ def flush() -> None:
if ctx.execution_context is None:
return
- if ctx.execution_context.output is not None:
- value = vstack(ctx.execution_context.output)
- else:
- value = None
- write_internal(cell_id=ctx.execution_context.cell_id, value=value)
+ output = ctx.execution_context.output
+ write_internal(cell_id=ctx.execution_context.cell_id, value=output.stack())
def remove(value: object) -> None:
@@ -140,10 +130,8 @@ def remove(value: object) -> None:
except ContextNotInitializedError:
return
- if ctx.execution_context is None or ctx.execution_context.output is None:
+ if ctx.execution_context is None or not ctx.execution_context.output:
return
- output = [
- item for item in ctx.execution_context.output if item is not value
- ]
- ctx.execution_context.output = output if output else None
+ output = ctx.execution_context.output
+ output.remove(value)
flush()
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index 6b2c5c423b..c825aa694e 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -328,10 +328,10 @@ def _broadcast_outputs(
# Don't rebroadcast an output that was already sent
#
# 1. if run_result.output is not None, need to send it
- # 2. otherwise if exc_ctx.output is None, then need to send
+ # 2. otherwise if accumulated_output is empty, then need to send
# the (empty) output (to clear it)
should_send_output = (
- run_result.output is not None or run_result.accumulated_output is None
+ run_result.output is not None or not run_result.accumulated_output
)
if (
run_result.success()
diff --git a/marimo/_runtime/threads.py b/marimo/_runtime/threads.py
index 4d14ec05ae..62a991ef56 100644
--- a/marimo/_runtime/threads.py
+++ b/marimo/_runtime/threads.py
@@ -7,6 +7,7 @@ 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.cell_output_list import CellOutputList
from marimo._runtime.context.kernel_context import KernelRuntimeContext
from marimo._runtime.context.script_context import ScriptRuntimeContext
from marimo._runtime.context.types import (
@@ -148,11 +149,12 @@ class Thread(threading.Thread):
except RuntimeError:
pass
- output = None
- if (mctx := self._marimo_ctx) is not None and (
- ectx := mctx.execution_context
- ) is not None:
- output = ectx.output
+ output = CellOutputList()
+ if self._marimo_ctx is not None:
+ if (exec_ctx := self._marimo_ctx.execution_context) is not None:
+ # Share the parent's CellOutputList so appends from threads
+ # are visible to the main execution context.
+ output = exec_ctx.output
if isinstance(self._marimo_ctx, KernelRuntimeContext):
self._marimo_ctx.execution_context = ExecutionContext(
diff --git a/marimo/_smoke_tests/output.py b/marimo/_smoke_tests/output.py
index 385d407d88..a5ea962cf2 100644
--- a/marimo/_smoke_tests/output.py
+++ b/marimo/_smoke_tests/output.py
@@ -2,19 +2,21 @@
import marimo
-__generated_with = "0.15.5"
+__generated_with = "0.20.1"
app = marimo.App()
@app.cell
def _():
import marimo as mo
+
return (mo,)
@app.cell
def _():
import time
+
return (time,)
@@ -29,12 +31,15 @@ def _(mo, time):
for i in range(5):
mo.output.append(mo.md(f"Loading {i}/5"))
time.sleep(.01)
+
return loop_append, loop_replace
@app.cell
def _(mo):
- mo.md("""### Replace""")
+ mo.md("""
+ ### Replace
+ """)
return
@@ -54,7 +59,9 @@ def _(loop_replace, mo):
@app.cell
def _(mo):
- mo.md("""### Append""")
+ mo.md("""
+ ### Append
+ """)
return
@@ -74,7 +81,9 @@ def _(loop_append, mo):
@app.cell
def _(mo):
- mo.md("""### Clear""")
+ mo.md("""
+ ### Clear
+ """)
return
@@ -96,7 +105,9 @@ def _(loop_append, mo):
@app.cell
def _(mo):
- mo.md("""### Sleep (stale)""")
+ mo.md("""
+ ### Sleep (stale)
+ """)
return
diff --git a/marimo/_smoke_tests/status.py b/marimo/_smoke_tests/status/status.py
similarity index 100%
rename from marimo/_smoke_tests/status.py
rename to marimo/_smoke_tests/status/status.py
diff --git a/marimo/_smoke_tests/threads/multiple_appends.py b/marimo/_smoke_tests/threads/multiple_appends.py
new file mode 100644
index 0000000000..f4b3955082
--- /dev/null
+++ b/marimo/_smoke_tests/threads/multiple_appends.py
@@ -0,0 +1,61 @@
+import marimo
+
+__generated_with = "0.20.1"
+app = marimo.App()
+
+
+def _():
+ import marimo as mo
+ import time
+ import threading
+
+ return mo, threading, time
+
+
+def _(mo, threading, time):
+ def append():
+ for i in range(3):
+ mo.output.append(f"{i}: Hello from {threading.get_ident()}")
+ time.sleep(1)
+
+ return (append,)
+
+
+def _(mo, threading, time):
+ def replace():
+ for i in range(3):
+ mo.output.replace(f"{i}: Hello from {threading.get_ident()}")
+ time.sleep(1)
+
+ return (replace,)
+
+
+def _(mo):
+ def run_threads(fn):
+ _threads = [mo.Thread(target=fn) for _ in range(3)]
+ for _t in _threads:
+ _t.start()
+ for _t in _threads:
+ _t.join()
+
+ return (run_threads,)
+
+
+def _(append, run_threads):
+ run_threads(append)
+ return
+
+
+def _(replace, run_threads):
+ run_threads(replace)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/marimo/_smoke_tests/threads/progress_bar_threads.py b/marimo/_smoke_tests/threads/progress_bar_threads.py
new file mode 100644
index 0000000000..ed5e44fa1d
--- /dev/null
+++ b/marimo/_smoke_tests/threads/progress_bar_threads.py
@@ -0,0 +1,52 @@
+import marimo
+
+__generated_with = "0.20.1"
+app = marimo.App()
+
+
+def _():
+ import marimo as mo
+ import random
+ import time
+ import threading
+
+ return mo, random, threading, time
+
+
+def _(mo, random, threading, time):
+ def step(pbar: mo.status.progress_bar, work: int):
+ for _ in range(work):
+ # Sleep... or anything else that releases GIL
+ time.sleep(random.uniform(0.5, 1))
+ pbar.update(
+ subtitle=f"work completed by thread {threading.get_ident()}"
+ )
+
+ return (step,)
+
+
+def _(mo, random, step, time):
+ total = 30
+ with mo.status.progress_bar(total=total) as pbar:
+ n_threads = 4
+ work = total // n_threads
+ remainder = total % n_threads
+ threads = [
+ mo.Thread(target=step, args=(pbar, work))
+ for _ in range(n_threads)
+ ]
+ for t in threads:
+ t.start()
+ for t in threads:
+ t.join()
+ for _ in range(remainder):
+ time.sleep(random.uniform(0.5, 1))
+ pbar.update(subtitle="work completed by main thread")
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/marimo/_smoke_tests/state_threading.py b/marimo/_smoke_tests/threads/state_threading.py
similarity index 100%
rename from marimo/_smoke_tests/state_threading.py
rename to marimo/_smoke_tests/threads/state_threading.py
diff --git a/marimo/_smoke_tests/threads.py b/marimo/_smoke_tests/threads/threads.py
similarity index 100%
rename from marimo/_smoke_tests/threads.py
rename to marimo/_smoke_tests/threads/threads.py
diff --git a/tests/_runtime/test_cell_output_list.py b/tests/_runtime/test_cell_output_list.py
new file mode 100644
index 0000000000..6c898e9294
--- /dev/null
+++ b/tests/_runtime/test_cell_output_list.py
@@ -0,0 +1,71 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from unittest.mock import MagicMock
+
+import pytest
+
+from marimo._runtime.cell_output_list import CellOutputList
+
+
+def _html(text: str) -> MagicMock:
+ m = MagicMock()
+ m.__repr__ = lambda _: text
+ return m
+
+
+class TestCellOutputList:
+ def test_append_and_len(self) -> None:
+ col = CellOutputList()
+ assert len(col) == 0
+ col.append(_html("a"))
+ col.append(_html("b"))
+ assert len(col) == 2
+
+ def test_bool(self) -> None:
+ col = CellOutputList()
+ assert not col
+ col.append(_html("a"))
+ assert col
+
+ def test_clear(self) -> None:
+ col = CellOutputList()
+ col.append(_html("a"))
+ col.clear()
+ assert len(col) == 0
+
+ def test_replace_at_index(self) -> None:
+ col = CellOutputList()
+ col.append(_html("a"))
+ col.append(_html("b"))
+ replacement = _html("c")
+ col.replace_at_index(replacement, 0)
+ assert col._items[0] is replacement
+ assert len(col) == 2
+
+ def test_replace_at_index_appends_at_end(self) -> None:
+ col = CellOutputList()
+ col.append(_html("a"))
+ new = _html("b")
+ col.replace_at_index(new, 1)
+ assert len(col) == 2
+ assert col._items[1] is new
+
+ def test_replace_at_index_out_of_range(self) -> None:
+ col = CellOutputList()
+ with pytest.raises(IndexError):
+ col.replace_at_index(_html("a"), 5)
+
+ def test_remove_by_identity(self) -> None:
+ col = CellOutputList()
+ a = _html("a")
+ b = _html("b")
+ col.append(a)
+ col.append(b)
+ col.remove(a)
+ assert len(col) == 1
+ assert col._items[0] is b
+
+ def test_stack_empty_returns_none(self) -> None:
+ col = CellOutputList()
+ assert col.stack() is None
diff --git a/tests/_runtime/test_threads.py b/tests/_runtime/test_threads.py
index 90018a59e4..c6c300780a 100644
--- a/tests/_runtime/test_threads.py
+++ b/tests/_runtime/test_threads.py
@@ -238,3 +238,50 @@ async def test_thread_should_exit_on_deletion(
await k.delete_cell(DeleteCellCommand(er.cell_id))
assert thread.should_exit
+
+
+async def test_threads_share_output_list_with_parent(
+ k: Kernel, exec_req: ExecReqProvider
+) -> None:
+ """Test that mo.Threads share the parent's output list.
+
+ Sharing ensures that accumulated_output is non-None after cell
+ execution, so the post-execution hook doesn't wipe thread output.
+ """
+
+ await k.run(
+ [
+ exec_req.get("import marimo as mo"),
+ exec_req.get(
+ """
+ output_ids = []
+ def target():
+ import marimo as mo
+ ctx = mo._runtime.context.get_context()
+ output_ids.append(id(ctx.execution_context.output))
+ mo.output.append("from thread")
+ """
+ ),
+ exec_req.get(
+ """
+ mo.output.append("some output")
+ threads = [mo.Thread(target=target) for _ in range(3)]
+ for t in threads:
+ t.start()
+ for t in threads:
+ t.join()
+ """
+ ),
+ ]
+ )
+
+ assert not k.errors
+ time.sleep(0.01) # noqa: ASYNC251
+
+ output_ids = k.globals["output_ids"]
+ # All threads should share the same output list as the parent
+ assert len(output_ids) == 3
+ assert len(set(output_ids)) == 1, (
+ "All threads should share the same output list, "
+ f"but got ids: {output_ids}"
+ )