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: sandbox graceful termination (#2225)

Akshay Agrawal7804ff2f43f0
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index cb8f847177..3aaa12b59d 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -396,6 +396,17 @@ def edit(
help="Base URL for the server. Should start with a /.",
callback=validators.base_url,
)
+ "--sandbox",
+ is_flag=True,
+ default=False,
+ show_default=True,
+ type=bool,
+ help="""
+ Run the command in an isolated virtual environment using
+ 'uv run --isolated'. Requires `uv`.
+ """,
+)
def new(
port: Optional[int],
host: str,
@@ -404,7 +415,14 @@ def new(
token: bool,
token_password: Optional[str],
base_url: str,
+ sandbox: bool,
) -> None:
+ if sandbox:
+ from marimo._cli.sandbox import run_in_sandbox
+
+ run_in_sandbox(sys.argv[1:], None)
+ return
+
start(
file_router=AppFileRouter.new_file(),
development_mode=GLOBAL_SETTINGS.DEVELOPMENT_MODE,
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.py
index c0fce48bd2..86acfb8d51 100644
--- a/marimo/_cli/sandbox.py
+++ b/marimo/_cli/sandbox.py
@@ -4,7 +4,9 @@ from __future__ import annotations
import atexit
import os
import re
+import signal
import subprocess
+import sys
import tempfile
from typing import Any, Dict, List, Optional, cast
@@ -23,7 +25,7 @@ REGEX = (
def run_in_sandbox(
args: List[str],
name: Optional[str] = None,
-) -> subprocess.CompletedProcess[Any]:
+) -> int:
if not DependencyManager.which("uv"):
raise click.UsageError("uv must be installed to use --sandbox")
@@ -58,7 +60,20 @@ def run_in_sandbox(
] + cmd
click.echo(f"Running in a sandbox: {' '.join(cmd)}")
- return subprocess.run(cmd)
+
+ process = subprocess.Popen(cmd)
+
+ def handler(sig: int, frame: Any) -> None:
+ del sig
+ del frame
+ if sys.platform == "win32":
+ os.kill(process.pid, signal.CTRL_C_EVENT)
+ else:
+ os.kill(process.pid, signal.SIGINT)
+
+ signal.signal(signal.SIGINT, handler)
+
+ return process.wait()
def _get_dependencies(script: str) -> List[str] | None: