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: key pending tasks by event loop (#8875)

Dylan Madisetti4e6c70916c79
diff --git a/marimo/_save/save.py b/marimo/_save/save.py
index e12238e460..c63256921a 100644
--- a/marimo/_save/save.py
+++ b/marimo/_save/save.py
@@ -515,11 +515,17 @@ class _cache_call_async(_cache_call[P, R]):
will share the same execution, preventing duplicate work.
"""
- # Track pending executions per cache instance to prevent race conditions
- # WeakKeyDictionary ensures instances are cleaned up when garbage collected
- # Key: cache instance, Value: dict of {cache_key: Task}
+ # Track pending executions per (instance, event loop) to prevent race
+ # conditions and cross-session interference. The outer WeakKeyDictionary
+ # is keyed by cache instance; the inner one is keyed by event loop so
+ # that concurrent sessions (each with their own loop, as in marimo run)
+ # never share or evict each other's pending tasks. Both levels use
+ # WeakKeyDictionary so entries are GC'd when the instance or loop dies.
_pending_executions: weakref.WeakKeyDictionary[
- _cache_call_async[Any, Any], dict[str, asyncio.Task[Any]]
+ _cache_call_async[Any, Any],
+ weakref.WeakKeyDictionary[
+ asyncio.AbstractEventLoop, dict[str, asyncio.Task[Any]]
+ ],
] = weakref.WeakKeyDictionary()
_pending_lock = threading.Lock()
@@ -544,16 +550,22 @@ class _cache_call_async(_cache_call[P, R]):
# Prepare execution context to get cache key
scope, ctx, attempt = self._prepare_call_execution(args, kwargs)
cache_key = attempt.hash
+ current_loop = asyncio.get_running_loop()
- # Check for pending execution (task deduplication)
+ # Check for pending execution (task deduplication).
+ # Each event loop gets its own pending dict so concurrent sessions
+ # (each with their own loop, as in marimo run) never share tasks.
existing_task = None
with self._pending_lock:
if self not in self._pending_executions:
- self._pending_executions[self] = {}
- pending = self._pending_executions[self]
+ self._pending_executions[self] = weakref.WeakKeyDictionary()
+ loop_dict = self._pending_executions[self]
+ if current_loop not in loop_dict:
+ loop_dict[current_loop] = {}
+ pending = loop_dict[current_loop]
if cache_key in pending:
- # Another coroutine is already executing this - save the task
+ # Another coroutine in this session is already executing this
existing_task = pending[cache_key]
# Await the existing task AFTER releasing the lock to avoid deadlock
@@ -571,13 +583,16 @@ class _cache_call_async(_cache_call[P, R]):
try:
result = await task
finally:
- # Clean up completed task
+ # Clean up completed task and empty dicts
with self._pending_lock:
- if cache_key in pending:
- del pending[cache_key]
- # Clean up empty instance dict (WeakKeyDictionary handles instance cleanup)
- if not pending and self in self._pending_executions:
- del self._pending_executions[self]
+ pending.pop(cache_key, None)
+ try:
+ if not pending and current_loop in loop_dict:
+ del loop_dict[current_loop]
+ if not loop_dict and self in self._pending_executions:
+ del self._pending_executions[self]
+ except KeyError:
+ pass
return cast(R, result)
diff --git a/tests/_save/test_cache.py b/tests/_save/test_cache.py
index b56782b6d7..018c3bee08 100644
--- a/tests/_save/test_cache.py
+++ b/tests/_save/test_cache.py
@@ -3409,3 +3409,78 @@ class TestAsyncCacheDecorator:
assert k.globals["expensive_async_compute"].hits == 0, (
"First execution should be a miss, deduplication doesn't count as hits"
)
+
+ async def test_async_cache_stale_task_different_loop(
+ self, k: Kernel, exec_req: ExecReqProvider
+ ) -> None:
+ """Regression test for GH-8866.
+
+ In marimo run, each session runs in its own thread with a separate event
+ loop. When a pending task from session A's loop is found by session B
+ (different loop), it must not be awaited — doing so raises RuntimeError.
+ """
+ import asyncio
+
+ from marimo._save.save import _cache_call_async
+
+ await k.run(
+ [
+ exec_req.get(
+ """
+ import asyncio
+ from marimo._save.save import cache
+
+ @cache
+ async def compute(x):
+ await asyncio.sleep(0)
+ return x * 2
+
+ result = await compute(42)
+ """
+ )
+ ]
+ )
+
+ assert not k.stderr.messages
+ assert k.globals["result"] == 84
+
+ fn = k.globals["compute"]
+ cache_key = fn._last_hash
+
+ # Inject a stale future from a different event loop into the per-loop
+ # pending dict, simulating session A's in-progress task. Session B
+ # (the current kernel loop) must use its own loop's pending dict and
+ # never see or touch session A's future.
+ other_loop = asyncio.new_event_loop()
+ try:
+ stale_future = (
+ other_loop.create_future()
+ ) # Future, no coroutine → no warning
+
+ with _cache_call_async._pending_lock:
+ import weakref
+
+ if fn not in _cache_call_async._pending_executions:
+ _cache_call_async._pending_executions[fn] = (
+ weakref.WeakKeyDictionary()
+ )
+ _cache_call_async._pending_executions[fn][other_loop] = {
+ cache_key: stale_future
+ }
+
+ # Session B looks up its own loop's pending dict → not found →
+ # fresh execution → cache hit → result = 84, no RuntimeError.
+ await k.run([exec_req.get("result2 = await compute(42)")])
+
+ assert not k.stderr.messages, k.stderr
+ assert k.globals["result2"] == 84
+ # The stale future must be untouched: if __call__ had tried to
+ # await it the future would have raised an error and been resolved.
+ assert not stale_future.done(), (
+ "stale future was awaited/cancelled — __call__ must not touch "
+ "pending tasks from a different event loop"
+ )
+ finally:
+ with _cache_call_async._pending_lock:
+ _cache_call_async._pending_executions.pop(fn, None)
+ other_loop.close()