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: fail CLI edit if shared memory is not available (#7921)

Akshay Agrawal070264cec977
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index 415dc40c5a..4269fc1acc 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -525,6 +525,23 @@ def edit(
"Or: pip install pyzmq"
)
+ # Check shared memory availability early (required for edit mode to
+ # communicate between the server process and kernel subprocess)
+ from marimo._utils.platform import check_shared_memory_available
+
+ shm_available, shm_error = check_shared_memory_available()
+ if not shm_available:
+ _loggers.marimo_logger().error(
+ f"marimo failed to start: marimo edit requires shared memory support for multiprocessing.\n\n"
+ f"{shm_error}\n\n"
+ "Possible solutions:\n"
+ " - If running in Docker, ensure /dev/shm is mounted with sufficient size\n"
+ " (e.g., --shm-size=256m or -v /dev/shm:/dev/shm)\n"
+ " - If /dev/shm is full, clear unused shared memory segments\n"
+ " - Use 'marimo run' instead if you only need to view notebooks"
+ )
+ sys.exit(1)
+
start(
file_router=AppFileRouter.infer(name),
development_mode=GLOBAL_SETTINGS.DEVELOPMENT_MODE,
diff --git a/marimo/_utils/platform.py b/marimo/_utils/platform.py
index 3c296f6e66..8b1b6d2fea 100644
--- a/marimo/_utils/platform.py
+++ b/marimo/_utils/platform.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import sys
+from uuid import uuid4
def is_windows() -> bool:
@@ -10,3 +11,40 @@ def is_windows() -> bool:
def is_pyodide() -> bool:
return "pyodide" in sys.modules
+
+
+def check_shared_memory_available() -> tuple[bool, str]:
+ """Check if shared memory is available for multiprocessing.
+
+ Returns:
+ A tuple of (is_available, error_message).
+ If is_available is True, error_message is empty.
+ If is_available is False, error_message contains the reason.
+ """
+ if is_pyodide():
+ return False, "Shared memory is not supported on the Pyodide platform."
+
+ try:
+ from multiprocessing import shared_memory
+
+ # Try to create a small shared memory segment
+ test_name = f"marimo_test_{uuid4().hex[:8]}"
+ shm = shared_memory.SharedMemory(name=test_name, create=True, size=8)
+ shm.close()
+ shm.unlink()
+ return True, ""
+ except ImportError as e:
+ return (
+ False,
+ f"The multiprocessing.shared_memory module is not available: {e}",
+ )
+ except OSError as e:
+ return (
+ False,
+ f"Unable to create shared memory: {e}. "
+ "This can happen in restricted environments like some Docker "
+ "containers or when /dev/shm is not available or has insufficient "
+ "space.",
+ )
+ except Exception as e:
+ return False, f"Unexpected error checking shared memory: {e}"
diff --git a/tests/_utils/test_platform.py b/tests/_utils/test_platform.py
new file mode 100644
index 0000000000..96f07788f5
--- /dev/null
+++ b/tests/_utils/test_platform.py
@@ -0,0 +1,96 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from unittest.mock import patch
+
+from click.testing import CliRunner
+
+from marimo._cli.cli import main
+from marimo._utils.platform import (
+ check_shared_memory_available,
+ is_pyodide,
+ is_windows,
+)
+
+
+class TestIsWindows:
+ def test_is_windows_on_win32(self) -> None:
+ with patch("marimo._utils.platform.sys.platform", "win32"):
+ assert is_windows()
+
+ def test_is_windows_on_cygwin(self) -> None:
+ with patch("marimo._utils.platform.sys.platform", "cygwin"):
+ assert is_windows()
+
+ def test_is_windows_on_linux(self) -> None:
+ with patch("marimo._utils.platform.sys.platform", "linux"):
+ assert not is_windows()
+
+ def test_is_windows_on_darwin(self) -> None:
+ with patch("marimo._utils.platform.sys.platform", "darwin"):
+ assert not is_windows()
+
+
+class TestIsPyodide:
+ def test_is_pyodide_when_not_loaded(self) -> None:
+ # By default, pyodide should not be in sys.modules
+ assert not is_pyodide()
+
+ def test_is_pyodide_when_loaded(self) -> None:
+ with patch(
+ "marimo._utils.platform.sys.modules", {"pyodide": object()}
+ ):
+ assert is_pyodide()
+
+
+class TestCheckSharedMemoryAvailable:
+ def test_shared_memory_available(self) -> None:
+ # On a normal system, shared memory should be available
+ is_available, error = check_shared_memory_available()
+ assert is_available
+ assert error == ""
+
+ def test_shared_memory_unavailable_on_pyodide(self) -> None:
+ with patch("marimo._utils.platform.is_pyodide", return_value=True):
+ is_available, error = check_shared_memory_available()
+ assert not is_available
+ assert "Pyodide" in error
+
+ def test_shared_memory_oserror(self) -> None:
+ with patch("marimo._utils.platform.is_pyodide", return_value=False):
+ # Mock the SharedMemory class to raise OSError
+ mock_shm_class = type(
+ "MockSharedMemory",
+ (),
+ {
+ "__init__": lambda _self, **_kwargs: (_ for _ in ()).throw(
+ OSError("Cannot allocate memory")
+ )
+ },
+ )
+
+ with patch(
+ "multiprocessing.shared_memory.SharedMemory", mock_shm_class
+ ):
+ is_available, error = check_shared_memory_available()
+ assert not is_available
+ assert "Unable to create shared memory" in error
+ assert "Cannot allocate memory" in error
+ assert (
+ "Docker" in error
+ ) # Should mention Docker as a possible cause
+
+ def test_edit_exits_with_error_when_shared_memory_unavailable(
+ self,
+ ) -> None:
+ runner = CliRunner()
+ with patch(
+ "marimo._utils.platform.check_shared_memory_available",
+ return_value=(False, "Test shared memory error"),
+ ):
+ result = runner.invoke(
+ main,
+ ["edit", "--headless", "--no-token", "--skip-update-check"],
+ )
+ # Should exit with error code 1
+ assert result.exit_code == 1