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: don't install interrupt signal handler in run mode (#1185)

Akshay Agrawal64b60bf6c795
diff --git a/marimo/_runtime/cell_runner.py b/marimo/_runtime/cell_runner.py
index 4825252b34..b853e0683f 100644
--- a/marimo/_runtime/cell_runner.py
+++ b/marimo/_runtime/cell_runner.py
@@ -6,6 +6,7 @@ import contextlib
import functools
import signal
import sys
+import threading
import traceback
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Iterator, Optional
@@ -223,7 +224,13 @@ class Runner:
return_value_future = asyncio.ensure_future(
execute_cell_async(cell, self.glbls)
)
- with Runner._cancel_on_sigint(return_value_future):
+ if threading.current_thread() == threading.main_thread():
+ # edit mode: need to handle user interrupts
+ with Runner._cancel_on_sigint(return_value_future):
+ return_value = await return_value_future
+ else:
+ # run mode: can't use signal.signal, not interruptible
+ # by user anyway.
return_value = await return_value_future
else:
return_value = execute_cell(cell, self.glbls)
diff --git a/tests/_cli/test_cli_export.py b/tests/_cli/test_cli_export.py
index cdd7eee54c..cd23a8c7b1 100644
--- a/tests/_cli/test_cli_export.py
+++ b/tests/_cli/test_cli_export.py
@@ -36,6 +36,21 @@ def test_cli_export_html_no_code(temp_marimo_file: str) -> None:
assert '<marimo-code hidden=""></marimo-code>' in html
+def test_cli_export_async(temp_async_marimo_file: str) -> None:
+ p = subprocess.run(
+ ["marimo", "export", "html", temp_async_marimo_file],
+ capture_output=True,
+ )
+ assert p.returncode == 0, p.stderr.decode()
+ assert "ValueError" not in p.stderr.decode()
+ assert "Traceback" not in p.stderr.decode()
+ html = normalize_index_html(p.stdout.decode())
+ # Remove folder path
+ dirname = path.dirname(temp_async_marimo_file)
+ html = html.replace(dirname, "path")
+ assert '<marimo-code hidden=""></marimo-code>' not in html
+
+
async def test_export_watch(temp_marimo_file: str) -> None:
temp_out_file = temp_marimo_file.replace(".py", ".html")
p = subprocess.Popen( # noqa: ASYNC101
diff --git a/tests/conftest.py b/tests/conftest.py
index 72bcd52cdd..8700e38639 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import dataclasses
+import inspect
import sys
import textwrap
from tempfile import TemporaryDirectory
@@ -145,24 +146,56 @@ def executing_kernel() -> Generator[Kernel, None, None]:
def temp_marimo_file() -> Generator[str, None, None]:
tmp_dir = TemporaryDirectory()
tmp_file = tmp_dir.name + "/notebook.py"
- content = """
- import marimo
- app = marimo.App()
+ content = inspect.cleandoc(
+ """
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def __():
+ import marimo as mo
+ return mo,
+
+ if __name__ == "__main__":
+ app.run()
+ """
+ )
- @app.cell
- def __():
- import marimo as mo
- return mo,
+ try:
+ with open(tmp_file, "w") as f:
+ f.write(content)
+ yield tmp_file
+ finally:
+ tmp_dir.cleanup()
- if __name__ == "__main__":
- app.run()
- """
- with open(tmp_file, "w") as f:
- f.write(content)
- yield tmp_file
+def temp_async_marimo_file() -> Generator[str, None, None]:
+ tmp_dir = TemporaryDirectory()
+ tmp_file = tmp_dir.name + "/notebook.py"
+ content = inspect.cleandoc(
+ """
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ async def __():
+ import asyncio
+ await asyncio.sleep(0.1)
+ return asyncio,
+
+ if __name__ == "__main__":
+ app.run()
+ """
+ )
- tmp_dir.cleanup()
+ try:
+ with open(tmp_file, "w") as f:
+ f.write(content)
+ f.flush()
+ yield tmp_file
+ finally:
+ tmp_dir.cleanup()
# Factory to create ExecutionRequests and abstract away cell ID