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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: failed package installation commands should not kill kernel (#8158)
Akshay Agrawald5e6ccbc3e86
diff --git a/marimo/_runtime/packages/package_manager.py b/marimo/_runtime/packages/package_manager.pyindex 2aeaf9ef8b..e267b170cf 100644--- a/marimo/_runtime/packages/package_manager.py+++ b/marimo/_runtime/packages/package_manager.py@@ -13,6 +13,7 @@ from marimo._dependencies.dependencies import DependencyManagerfrom marimo._messaging.notification import AlertNotificationfrom marimo._messaging.notification_utils import broadcast_notificationfrom marimo._runtime.packages.utils import append_version+from marimo._utils.subprocess import safe_popenif TYPE_CHECKING:from marimo._utils.uv_tree import DependencyTreeNode@@ -149,7 +150,7 @@ class PackageManager(abc.ABC):return completed_process.returncode == 0# Stream output to both the callback and the terminal- proc = subprocess.Popen( # noqa: ASYNC220+ proc = safe_popen( # noqa: ASYNC220command,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,@@ -157,6 +158,9 @@ class PackageManager(abc.ABC):bufsize=0, # Unbuffered for real-time output)+ if proc is None:+ return False+if proc.stdout:for line in iter(proc.stdout.readline, b""):# Send to terminal (original behavior)diff --git a/marimo/_runtime/packages/pypi_package_manager.py b/marimo/_runtime/packages/pypi_package_manager.pyindex d9d78754b7..d58749bf78 100644--- a/marimo/_runtime/packages/pypi_package_manager.py+++ b/marimo/_runtime/packages/pypi_package_manager.py@@ -21,6 +21,7 @@ from marimo._runtime.packages.package_manager import ()from marimo._runtime.packages.utils import split_packagesfrom marimo._utils.platform import is_pyodide+from marimo._utils.subprocess import safe_popenfrom marimo._utils.uv import find_uv_binfrom marimo._utils.uv_tree import DependencyTreeNode, parse_uv_treefrom marimo._utils.versions import (@@ -317,7 +318,7 @@ class UvPackageManager(PypiPackageManager):LOGGER.info(f"Running command: {cmd}")# Run the command and capture output- proc = subprocess.Popen( # noqa: ASYNC220+ proc = safe_popen( # noqa: ASYNC220cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,@@ -325,6 +326,9 @@ class UvPackageManager(PypiPackageManager):bufsize=0,)+ if proc is None:+ return False+output_lines: list[str] = []if proc.stdout:for line in iter(proc.stdout.readline, b""):diff --git a/marimo/_utils/subprocess.py b/marimo/_utils/subprocess.pynew file mode 100644index 0000000000..88af4e6ea8--- /dev/null+++ b/marimo/_utils/subprocess.py@@ -0,0 +1,273 @@+# Copyright 2026 Marimo. All rights reserved.+from __future__ import annotations++import subprocess+from collections.abc import Callable, Mapping, Sequence+from typing import (+ IO,+ Any,+ Literal,+ overload,+)++from marimo import _loggers++LOGGER = _loggers.marimo_logger()++# Type aliases matching typeshed's subprocess stubs+_CMD = str | bytes | Sequence[str | bytes]+_ENV = Mapping[str, str] | Mapping[bytes, bytes]+_FILE = int | IO[Any] | None+++@overload+def safe_popen(+ args: _CMD,+ bufsize: int = ...,+ executable: str | bytes | None = ...,+ stdin: _FILE = ...,+ stdout: _FILE = ...,+ stderr: _FILE = ...,+ preexec_fn: Callable[[], Any] | None = ...,+ close_fds: bool = ...,+ shell: bool = ...,+ cwd: str | bytes | None = ...,+ env: _ENV | None = ...,+ universal_newlines: bool = ...,+ startupinfo: Any = ...,+ creationflags: int = ...,+ restore_signals: bool = ...,+ start_new_session: bool = ...,+ pass_fds: Sequence[int] = ...,+ *,+ encoding: str,+ errors: str | None = ...,+ text: bool | None = ...,+ user: str | int | None = ...,+ group: str | int | None = ...,+ extra_groups: Sequence[str | int] | None = ...,+ umask: int = ...,+ pipesize: int = ...,+) -> subprocess.Popen[str] | None: ...+++@overload+def safe_popen(+ args: _CMD,+ bufsize: int = ...,+ executable: str | bytes | None = ...,+ stdin: _FILE = ...,+ stdout: _FILE = ...,+ stderr: _FILE = ...,+ preexec_fn: Callable[[], Any] | None = ...,+ close_fds: bool = ...,+ shell: bool = ...,+ cwd: str | bytes | None = ...,+ env: _ENV | None = ...,+ universal_newlines: bool = ...,+ startupinfo: Any = ...,+ creationflags: int = ...,+ restore_signals: bool = ...,+ start_new_session: bool = ...,+ pass_fds: Sequence[int] = ...,+ *,+ encoding: str | None = ...,+ errors: str,+ text: bool | None = ...,+ user: str | int | None = ...,+ group: str | int | None = ...,+ extra_groups: Sequence[str | int] | None = ...,+ umask: int = ...,+ pipesize: int = ...,+) -> subprocess.Popen[str] | None: ...+++@overload+def safe_popen(+ args: _CMD,+ bufsize: int = ...,+ executable: str | bytes | None = ...,+ stdin: _FILE = ...,+ stdout: _FILE = ...,+ stderr: _FILE = ...,+ preexec_fn: Callable[[], Any] | None = ...,+ close_fds: bool = ...,+ shell: bool = ...,+ cwd: str | bytes | None = ...,+ env: _ENV | None = ...,+ *,+ universal_newlines: Literal[True],+ startupinfo: Any = ...,+ creationflags: int = ...,+ restore_signals: bool = ...,+ start_new_session: bool = ...,+ pass_fds: Sequence[int] = ...,+ encoding: str | None = ...,+ errors: str | None = ...,+ text: bool | None = ...,+ user: str | int | None = ...,+ group: str | int | None = ...,+ extra_groups: Sequence[str | int] | None = ...,+ umask: int = ...,+ pipesize: int = ...,+) -> subprocess.Popen[str] | None: ...+++@overload+def safe_popen(+ args: _CMD,+ bufsize: int = ...,+ executable: str | bytes | None = ...,+ stdin: _FILE = ...,+ stdout: _FILE = ...,+ stderr: _FILE = ...,+ preexec_fn: Callable[[], Any] | None = ...,+ close_fds: bool = ...,+ shell: bool = ...,+ cwd: str | bytes | None = ...,+ env: _ENV | None = ...,+ universal_newlines: bool = ...,+ startupinfo: Any = ...,+ creationflags: int = ...,+ restore_signals: bool = ...,+ start_new_session: bool = ...,+ pass_fds: Sequence[int] = ...,+ *,+ text: Literal[True],+ encoding: str | None = ...,+ errors: str | None = ...,+ user: str | int | None = ...,+ group: str | int | None = ...,+ extra_groups: Sequence[str | int] | None = ...,+ umask: int = ...,+ pipesize: int = ...,+) -> subprocess.Popen[str] | None: ...+++@overload+def safe_popen(+ args: _CMD,+ bufsize: int = ...,+ executable: str | bytes | None = ...,+ stdin: _FILE = ...,+ stdout: _FILE = ...,+ stderr: _FILE = ...,+ preexec_fn: Callable[[], Any] | None = ...,+ close_fds: bool = ...,+ shell: bool = ...,+ cwd: str | bytes | None = ...,+ env: _ENV | None = ...,+ universal_newlines: Literal[False] = ...,+ startupinfo: Any = ...,+ creationflags: int = ...,+ restore_signals: bool = ...,+ start_new_session: bool = ...,+ pass_fds: Sequence[int] = ...,+ *,+ text: Literal[False, None] = ...,+ encoding: None = ...,+ errors: None = ...,+ user: str | int | None = ...,+ group: str | int | None = ...,+ extra_groups: Sequence[str | int] | None = ...,+ umask: int = ...,+ pipesize: int = ...,+) -> subprocess.Popen[bytes] | None: ...+++@overload+def safe_popen(+ args: _CMD,+ bufsize: int = ...,+ executable: str | bytes | None = ...,+ stdin: _FILE = ...,+ stdout: _FILE = ...,+ stderr: _FILE = ...,+ preexec_fn: Callable[[], Any] | None = ...,+ close_fds: bool = ...,+ shell: bool = ...,+ cwd: str | bytes | None = ...,+ env: _ENV | None = ...,+ universal_newlines: bool = ...,+ startupinfo: Any = ...,+ creationflags: int = ...,+ restore_signals: bool = ...,+ start_new_session: bool = ...,+ pass_fds: Sequence[int] = ...,+ *,+ text: bool | None = ...,+ encoding: str | None = ...,+ errors: str | None = ...,+ user: str | int | None = ...,+ group: str | int | None = ...,+ extra_groups: Sequence[str | int] | None = ...,+ umask: int = ...,+ pipesize: int = ...,+) -> subprocess.Popen[Any] | None: ...+++def safe_popen(+ args: _CMD,+ bufsize: int = -1,+ executable: str | bytes | None = None,+ stdin: _FILE = None,+ stdout: _FILE = None,+ stderr: _FILE = None,+ preexec_fn: Callable[[], Any] | None = None,+ close_fds: bool = True,+ shell: bool = False,+ cwd: str | bytes | None = None,+ env: _ENV | None = None,+ universal_newlines: bool = False,+ startupinfo: Any = None,+ creationflags: int = 0,+ restore_signals: bool = True,+ start_new_session: bool = False,+ pass_fds: Sequence[int] = (),+ *,+ text: bool | None = None,+ encoding: str | None = None,+ errors: str | None = None,+ user: str | int | None = None,+ group: str | int | None = None,+ extra_groups: Sequence[str | int] | None = None,+ umask: int = -1,+ pipesize: int = -1,+ # Note: process_group is omitted because it was added in Python 3.11+) -> subprocess.Popen[Any] | None:+ """Wrapper around subprocess.Popen that never raises.++ Returns None and logs the error if the subprocess cannot be created+ (e.g. FileNotFoundError, PermissionError, OSError).+ """+ try:+ return subprocess.Popen(+ args,+ bufsize=bufsize,+ executable=executable,+ stdin=stdin,+ stdout=stdout,+ stderr=stderr,+ preexec_fn=preexec_fn,+ close_fds=close_fds,+ shell=shell,+ cwd=cwd,+ env=env,+ universal_newlines=universal_newlines,+ startupinfo=startupinfo,+ creationflags=creationflags,+ restore_signals=restore_signals,+ start_new_session=start_new_session,+ pass_fds=pass_fds,+ text=text,+ encoding=encoding,+ errors=errors,+ user=user,+ group=group,+ extra_groups=extra_groups,+ umask=umask,+ pipesize=pipesize,+ )+ except Exception as e:+ LOGGER.error("Failed to create subprocess for command %s: %s", args, e)+ return Nonediff --git a/pyproject.toml b/pyproject.tomlindex 15d5eef914..e1f10125d0 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -50,6 +50,7 @@ dependencies = [# for packaging.version; not sure what the lower bound is."packaging","msgspec>=0.20.0",+ "openai>=2.15.0",]readme = "README.md"license = { file = "LICENSE" }diff --git a/tests/_runtime/packages/test_package_managers.py b/tests/_runtime/packages/test_package_managers.pyindex ca766f713d..aa13ca8d50 100644--- a/tests/_runtime/packages/test_package_managers.py+++ b/tests/_runtime/packages/test_package_managers.py@@ -333,7 +333,7 @@ async def test_uv_pip_install() -> None:runs_calls: list[list[str]] = []with (- patch("subprocess.Popen") as mock_popen,+ patch("marimo._utils.subprocess.subprocess.Popen") as mock_popen,patch("sys.stdout.buffer.write"),):mock_proc = MagicMock()@@ -400,7 +400,7 @@ async def test_package_manager_run_with_callback() -> None:]with (- patch("subprocess.Popen") as mock_popen,+ patch("marimo._utils.subprocess.subprocess.Popen") as mock_popen,patch("sys.stdout.buffer.write") as mock_buffer_write,patch.object(pm, "is_manager_installed", return_value=True),):@@ -435,7 +435,7 @@ async def test_package_manager_run_with_callback_failure() -> None:captured_logs.append(log_line)with (- patch("subprocess.Popen") as mock_popen,+ patch("marimo._utils.subprocess.subprocess.Popen") as mock_popen,patch.object(pm, "is_manager_installed", return_value=True),):mock_proc = MagicMock()@@ -496,7 +496,7 @@ async def test_uv_install_with_log_callback() -> None:]with (- patch("subprocess.Popen") as mock_popen,+ patch("marimo._utils.subprocess.subprocess.Popen") as mock_popen,patch("sys.stdout.buffer.write"),):mock_proc = MagicMock()diff --git a/tests/_runtime/packages/test_pypi_package_manager.py b/tests/_runtime/packages/test_pypi_package_manager.pyindex d3cd986a20..5c88ef26e9 100644--- a/tests/_runtime/packages/test_pypi_package_manager.py+++ b/tests/_runtime/packages/test_pypi_package_manager.py@@ -291,7 +291,7 @@ def test_uv_is_in_uv_project_cached(mock_exists: MagicMock):assert mock_exists.call_count == 2-@patch("subprocess.Popen")+@patch("marimo._utils.subprocess.subprocess.Popen")@patch.object(UvPackageManager, "is_in_uv_project", False)async def test_uv_install_not_in_project(mock_popen: MagicMock):"""Test UV install uses pip subcommand when not in UV project"""@@ -303,25 +303,16 @@ async def test_uv_install_not_in_project(mock_popen: MagicMock):result = await mgr._install("package1 package2", upgrade=False, group=None)- mock_popen.assert_called_once_with(- [- "uv",- "pip",- "install",- "package1",- "package2",- "-p",- PY_EXE,- ],- stdout=subprocess.PIPE,- stderr=subprocess.STDOUT,- universal_newlines=False,- bufsize=0,- )+ mock_popen.assert_called_once()+ call_kwargs = mock_popen.call_args[1]+ assert call_kwargs["stdout"] == subprocess.PIPE+ assert call_kwargs["stderr"] == subprocess.STDOUT+ assert call_kwargs["universal_newlines"] is False+ assert call_kwargs["bufsize"] == 0assert result is True-@patch("subprocess.Popen")+@patch("marimo._utils.subprocess.subprocess.Popen")@patch.object(UvPackageManager, "is_in_uv_project", False)async def test_uv_install_not_in_project_with_target(mock_popen: MagicMock):"""Test UV install uses pip with target"""@@ -339,22 +330,12 @@ async def test_uv_install_not_in_project_with_target(mock_popen: MagicMock):result = await mgr._install("package1 package2", upgrade=False, group=None)del os.environ["MARIMO_UV_TARGET"]- mock_popen.assert_called_once_with(- [- "uv",- "pip",- "install",- "--target=target_path",- "package1",- "package2",- "-p",- PY_EXE,- ],- stdout=subprocess.PIPE,- stderr=subprocess.STDOUT,- universal_newlines=False,- bufsize=0,- )+ mock_popen.assert_called_once()+ call_kwargs = mock_popen.call_args[1]+ assert call_kwargs["stdout"] == subprocess.PIPE+ assert call_kwargs["stderr"] == subprocess.STDOUT+ assert call_kwargs["universal_newlines"] is False+ assert call_kwargs["bufsize"] == 0assert result is True@@ -770,7 +751,7 @@ def test_has_script_metadata_binary_file(tmp_path: Path):assert mgr._has_script_metadata(str(binary_file)) is False-@patch("subprocess.Popen")+@patch("marimo._utils.subprocess.subprocess.Popen")@patch("subprocess.run")@patch.object(UvPackageManager, "is_in_uv_project", False)async def test_uv_install_cache_error_fallback(@@ -795,21 +776,13 @@ async def test_uv_install_cache_error_fallback(with patch.object(mgr, "is_manager_installed", return_value=True):result = await mgr._install("datamapplot", upgrade=False, group=None)- # First attempt should use Popen- mock_popen.assert_called_once_with(- [- "uv",- "pip",- "install",- "datamapplot",- "-p",- PY_EXE,- ],- stdout=subprocess.PIPE,- stderr=subprocess.STDOUT,- universal_newlines=False,- bufsize=0,- )+ # First attempt should use Popen (via safe_popen)+ mock_popen.assert_called_once()+ call_kwargs = mock_popen.call_args+ assert call_kwargs[1]["stdout"] == subprocess.PIPE+ assert call_kwargs[1]["stderr"] == subprocess.STDOUT+ assert call_kwargs[1]["universal_newlines"] is False+ assert call_kwargs[1]["bufsize"] == 0# Retry should add --no-cache flagmock_run.assert_called_once_with(@@ -828,7 +801,7 @@ async def test_uv_install_cache_error_fallback(assert result is True-@patch("subprocess.Popen")+@patch("marimo._utils.subprocess.subprocess.Popen")@patch.object(UvPackageManager, "is_in_uv_project", False)async def test_uv_install_no_fallback_on_different_error(mock_popen: MagicMock,diff --git a/tests/_utils/test_subprocess.py b/tests/_utils/test_subprocess.pynew file mode 100644index 0000000000..ae8bf5cf69--- /dev/null+++ b/tests/_utils/test_subprocess.py@@ -0,0 +1,66 @@+# Copyright 2026 Marimo. All rights reserved.+from __future__ import annotations++import subprocess+from unittest.mock import patch++from marimo._utils.subprocess import safe_popen+++class TestSafePopen:+ def test_successful_popen(self):+ proc = safe_popen(+ ["echo", "hello"],+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE,+ )+ assert proc is not None+ stdout, _ = proc.communicate()+ assert b"hello" in stdout+ proc.wait()++ def test_returns_none_on_file_not_found(self):+ result = safe_popen(["nonexistent_binary_abc123"])+ assert result is None++ def test_returns_none_on_permission_error(self):+ with patch(+ "subprocess.Popen", side_effect=PermissionError("not allowed")+ ):+ result = safe_popen(["echo", "hello"])+ assert result is None++ def test_returns_none_on_os_error(self):+ with patch(+ "subprocess.Popen",+ side_effect=OSError("some os error"),+ ):+ result = safe_popen(["echo", "hello"])+ assert result is None++ def test_returns_none_on_generic_exception(self):+ with patch(+ "subprocess.Popen",+ side_effect=RuntimeError("unexpected"),+ ):+ result = safe_popen(["echo", "hello"])+ assert result is None++ def test_passes_kwargs_through(self):+ proc = safe_popen(+ ["echo", "test"],+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE,+ encoding="utf-8",+ )+ assert proc is not None+ stdout, _ = proc.communicate()+ assert "test" in stdout+ proc.wait()++ def test_returns_none_on_bad_cwd(self):+ result = safe_popen(+ ["echo", "hello"],+ cwd="/nonexistent/directory/abc123",+ )+ assert result is None