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 asyncio tasks starved by blocking control loop (#8825)

Trevor Manz91b0f57fb7fc
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index cc00c0073a..8fe47bf304 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -3568,22 +3568,17 @@ def launch_kernel(
ui_element_request_mgr = SetUIElementRequestManager(set_ui_element_queue)
async def control_loop(kernel: Kernel) -> None:
- from queue import Empty
+ loop = asyncio.get_running_loop()
while True:
try:
- TIMEOUT_S = 0.1
- # 100ms timeout to avoid blocking
- # this does not mean ControlRequest will be blocked for 100ms
- # but rather background tasks may not start until 100ms have passed
- request: CommandMessage | None = control_queue.get(
- timeout=TIMEOUT_S
+ # Offload the blocking queue.get() to a thread so the event
+ # loop stays free to service background asyncio tasks (e.g.
+ # user-created tasks via create_task / ensure_future).
+ request: CommandMessage | None = await loop.run_in_executor(
+ None,
+ control_queue.get,
)
- except Empty:
- # Yield control back to the event loop to give
- # other tasks a chance to run
- await asyncio.sleep(0)
- continue
except Exception as e:
# triggered on Windows when quit with Ctrl+C
LOGGER.debug("kernel queue.get() failed %s", e)
@@ -3611,10 +3606,12 @@ def launch_kernel(
type(request).__name__,
)
- # The control loop is asynchronous only because we allow user code to use
- # top-level await; nothing else is awaited. Don't introduce async
- # primitives anywhere else in the runtime unless there is a *very* good
- # reason; prefer using threads (for performance and clarity).
+ # The control loop is asynchronous so that (a) user code can use
+ # top-level await, and (b) background asyncio tasks created by user code
+ # (via create_task / ensure_future) are not starved by a blocking
+ # queue.get(). The queue read is offloaded to a thread via
+ # run_in_executor; avoid adding further async primitives elsewhere in the
+ # runtime unless there is a very good reason.
asyncio.run(control_loop(kernel))
if not use_fd_redirect:
diff --git a/tests/_runtime/test_control_loop.py b/tests/_runtime/test_control_loop.py
new file mode 100644
index 0000000000..2a886bc96f
--- /dev/null
+++ b/tests/_runtime/test_control_loop.py
@@ -0,0 +1,49 @@
+# Copyright 2026 Marimo. All rights reserved.
+"""Tests for the control loop's event loop behavior.
+
+The kernel control loop polls a synchronous queue for messages.
+These tests verify that the polling strategy does not starve
+background asyncio tasks.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import queue
+import time
+
+import pytest
+
+
+async def test_executor_queue_get_does_not_starve_background_tasks():
+ """run_in_executor keeps the event loop free for background tasks.
+
+ This is the pattern used by the fixed control loop: the blocking
+ queue.get() runs in a thread, so the event loop can service other
+ tasks while waiting.
+ """
+ q: queue.Queue[None] = queue.Queue()
+ loop = asyncio.get_running_loop()
+ count = 0
+
+ async def background():
+ nonlocal count
+ end = time.time() + 0.5
+ while time.time() < end:
+ count += 1
+ await asyncio.sleep(0.01)
+
+ task = asyncio.create_task(background())
+
+ # Simulate the NEW control loop: run_in_executor
+ end = time.time() + 0.5
+ while time.time() < end:
+ try:
+ await loop.run_in_executor(None, lambda: q.get(timeout=0.1))
+ except queue.Empty:
+ pass
+
+ await task
+ # With run_in_executor, the task runs freely: ~45 iterations in 0.5s.
+ assert count > 30