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.

Catch/log exception when writing to pipe (#246)

Akshay Agrawala5623b88c805
diff --git a/marimo/_messaging/streams.py b/marimo/_messaging/streams.py
index ebcdebcf46..7e8c2b073d 100644
--- a/marimo/_messaging/streams.py
+++ b/marimo/_messaging/streams.py
@@ -7,9 +7,12 @@ from collections import deque
from multiprocessing.connection import Connection
from typing import Any, Optional
+from marimo import _loggers
from marimo._ast.cell import CellId_t
from marimo._messaging.console_output_worker import ConsoleMsg, buffered_writer
+LOGGER = _loggers.marimo_logger()
+
# Byte limits on outputs. Limits exist for two reasons:
#
# 1. We use a multiprocessing.Connection object to send outputs from
@@ -56,7 +59,12 @@ class Stream:
def write(self, op: str, data: dict[Any, Any]) -> None:
with self.stream_lock:
- self.pipe.send((op, data))
+ try:
+ self.pipe.send((op, data))
+ except OSError as e:
+ # Most likely a BrokenPipeError, caused by the
+ # server process shutting down
+ LOGGER.debug("Error when writing (op: %s) to pipe: %s", op, e)
class Stdout:
diff --git a/marimo/_runtime/functions.py b/marimo/_runtime/functions.py
index 752e8f2d7d..e86e9d6a35 100644
--- a/marimo/_runtime/functions.py
+++ b/marimo/_runtime/functions.py
@@ -16,6 +16,7 @@ T = TypeVar("T")
@dataclasses.dataclass
class EmptyArgs:
"""Utility type for functions that take no arguments."""
+
...
diff --git a/marimo/_server/server.py b/marimo/_server/server.py
index 5ae99ee1a4..ed83d0d96f 100644
--- a/marimo/_server/server.py
+++ b/marimo/_server/server.py
@@ -63,6 +63,8 @@ def shutdown(with_error: bool = False) -> None:
)
print()
mgr.shutdown()
+ # TODO(akshayka): This method raises an exception sometimes.
+ # debug, or just wrap in a try/except since we're on our way out anyway ...
tornado.ioloop.IOLoop.current().stop()
if with_error:
sys.exit(1)