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
Implement timeout option (#6196)
Thomas Brooksc897586d37a7
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.pyindex 0aac630fb1..2264847c59 100644--- a/marimo/_cli/cli.py+++ b/marimo/_cli/cli.py@@ -445,6 +445,14 @@ https://github.com/marimo-team/marimo/issues/5219.""",hidden=True,help="When opening a .py file, enable fallback conversion from pypercent, script, or text.",)+ "--timeout",+ required=False,+ default=None,+ show_default=False,+ type=float,+ help="Enable a global timeout to shut down the server after specified number of minutes of no connection",+)@click.argument("name",required=False,@@ -468,6 +476,7 @@ def edit(skew_protection: bool,remote_url: Optional[str],convert: bool,+ timeout: Optional[float],name: Optional[str],args: tuple[str, ...],) -> None:@@ -595,6 +604,7 @@ def edit(redirect_console_to_browser=True,ttl_seconds=None,remote_url=remote_url,+ timeout=timeout,)@@ -693,6 +703,14 @@ new_help_msg = "\n".join(type=bool,help="Enable skew protection middleware to prevent version mismatch issues.",)+ "--timeout",+ required=False,+ default=None,+ show_default=False,+ type=float,+ help="Enable a global timeout to shut down the server after specified number of minutes of no connection",+)@click.argument("prompt", required=False)def new(port: Optional[int],@@ -704,6 +722,7 @@ def new(base_url: str,sandbox: Optional[bool],skew_protection: bool,+ timeout: Optional[float],prompt: Optional[str],) -> None:if sandbox:@@ -782,6 +801,7 @@ def new(base_url=base_url,redirect_console_to_browser=True,ttl_seconds=None,+ timeout=timeout,)diff --git a/marimo/_server/api/middleware.py b/marimo/_server/api/middleware.pyindex c3bce83846..1ea206964a 100644--- a/marimo/_server/api/middleware.py+++ b/marimo/_server/api/middleware.py@@ -2,6 +2,7 @@from __future__ import annotationsimport asyncio+import timefrom collections.abc import AsyncIterablefrom dataclasses import dataclassfrom http.client import HTTPResponse, HTTPSConnection@@ -40,9 +41,12 @@ from marimo._server.api.auth import validate_authfrom marimo._server.api.deps import AppState, AppStateBasefrom marimo._server.codes import WebSocketCodesfrom marimo._server.model import SessionMode+from marimo._server.utils import print_tabbed+from marimo._server.uvicorn_utils import close_uvicornfrom marimo._tracer import server_tracerif TYPE_CHECKING:+ from starlette.datastructures import Statefrom starlette.requests import HTTPConnectionfrom starlette.types import ASGIApp, Receive, Scope, Send@@ -533,3 +537,57 @@ class ProxyMiddleware:if websocket.client_state != WebSocketState.DISCONNECTED:await websocket.close(code=WebSocketCodes.UNEXPECTED_ERROR)raise+++class TimeoutMiddleware(BaseHTTPMiddleware):+ def __init__(+ self,+ app: ASGIApp,+ dispatch: DispatchFunction | None = None,+ *,+ timeout_duration_minutes: float,+ app_state: State,+ ) -> None:+ super().__init__(app, dispatch)++ self.app_state = app_state+ self.app_state.timeout_tracker = time.time()+ self.timeout_duration_minutes = timeout_duration_minutes++ asyncio.create_task(self.monitor())++ async def __call__(+ self, scope: Scope, receive: Receive, send: Send+ ) -> None:+ if scope["type"] != "http":+ return await self.app(scope, receive, send)++ request = Request(scope)++ request.app.state.timeout_tracker = time.time()++ return await self.app(scope, receive, send)++ async def monitor(self) -> None:+ while True:+ LOGGER.debug("Checking inactivity timeout")+ timeout_at = (+ self.app_state.timeout_tracker+ + self.timeout_duration_minutes * 60+ )++ now = time.time()+ if now >= timeout_at:+ print_tabbed("Timeout due to inactivity")+ self.shutdown()+ break++ # Sleep until 1s after the next potential activity timeout+ await asyncio.sleep(timeout_at - now + 1)++ def shutdown(self) -> None:+ manager = self.app_state.session_manager++ manager.shutdown()+ if self.app_state.server:+ close_uvicorn(self.app_state.server)diff --git a/marimo/_server/main.py b/marimo/_server/main.pyindex 2f472421c1..b65e899c5a 100644--- a/marimo/_server/main.py+++ b/marimo/_server/main.py@@ -21,6 +21,7 @@ from marimo._server.api.middleware import (OpenTelemetryMiddleware,ProxyMiddleware,SkewProtectionMiddleware,+ TimeoutMiddleware,)from marimo._server.api.router import build_routesfrom marimo._server.api.status import (@@ -55,6 +56,7 @@ def create_starlette_app(allow_origins: Optional[tuple[str, ...]] = None,lsp_servers: Optional[list[LspServer]] = None,skew_protection: bool = True,+ timeout: Optional[float] = None,) -> Starlette:final_middlewares: list[Middleware] = []@@ -105,7 +107,7 @@ def create_starlette_app(final_middlewares.extend(MIDDLEWARE_REGISTRY.get_all())- return Starlette(+ app = Starlette(routes=build_routes(base_url=base_url),middleware=final_middlewares,lifespan=lifespan,@@ -116,6 +118,13 @@ def create_starlette_app(ModuleNotFoundError: handle_error,},)+ if timeout is not None:+ app.add_middleware(+ TimeoutMiddleware,+ app_state=app.state,+ timeout_duration_minutes=timeout,+ )+ return appdef _create_mpl_proxy_middleware(base_url: str) -> Middleware:diff --git a/marimo/_server/start.py b/marimo/_server/start.pyindex 8f6cace71d..9b95dd9bb9 100644--- a/marimo/_server/start.py+++ b/marimo/_server/start.py@@ -90,6 +90,7 @@ def start(redirect_console_to_browser: bool,skew_protection: bool,remote_url: Optional[str] = None,+ timeout: Optional[float] = None,) -> None:"""Start the server.@@ -182,6 +183,7 @@ def start(if lsp_composite_server is not Noneelse None,skew_protection=skew_protection,+ timeout=timeout,)app.state.port = external_portdiff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.pyindex 03f933f7dc..b474922bf6 100644--- a/tests/_cli/test_cli.py+++ b/tests/_cli/test_cli.py@@ -1216,3 +1216,22 @@ def test_cli_edit_with_convert(p.terminate()p.wait(timeout=5)+++def test_cli_edit_with_timeout() -> None:+ p = subprocess.Popen(+ [+ "marimo",+ "edit",+ "--no-token",+ "--headless",+ "--timeout",+ "0.01", # very short timeout+ ],+ stderr=subprocess.PIPE,+ stdout=subprocess.PIPE,+ text=True,+ )++ stdout, _ = p.communicate(timeout=5)+ assert "Timeout due to inactivity" in stdout