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: graceful shutdown for uvicorn==0.29.0 (#972)
Akshay Agrawal313fddf9d5ac
diff --git a/marimo/_server/api/interrupt.py b/marimo/_server/api/interrupt.pyindex 98391b1206..de2fb4b780 100644--- a/marimo/_server/api/interrupt.py+++ b/marimo/_server/api/interrupt.py@@ -29,6 +29,16 @@ class InterruptHandler:lambda signum, frame: self._interrupt_handler(), # noqa: ARG005,E501)+ def restore_interrupt_handler(self) -> None:+ # Restore the original signal handler so re-entering Ctrl+C raises a+ # keyboard interrupt instead of calling this function again (input is+ # not re-entrant, so it's not safe to call this function again)+ try:+ self.loop.remove_signal_handler(signal.SIGINT)+ except NotImplementedError:+ # Windows+ signal.signal(signal.SIGINT, self.original_handler)+def _interrupt_handler(self) -> None:# Restore the original signal handler so re-entering Ctrl+C raises a# keyboard interrupt instead of calling this function again (input isdiff --git a/marimo/_server/start.py b/marimo/_server/start.pyindex 1ccd4a6902..1ab3a3c79d 100644--- a/marimo/_server/start.py+++ b/marimo/_server/start.py@@ -13,6 +13,7 @@ from marimo._server.utils import (import_files,initialize_asyncio,)+from marimo._server.uvicorn_utils import initialize_signalsDEFAULT_PORT = 2718@@ -63,6 +64,7 @@ def start(app.state.base_url = base_urlapp.state.config_manager = user_config_mgr+ initialize_signals()server = uvicorn.Server(uvicorn.Config(app,@@ -94,7 +96,7 @@ def start(timeout_graceful_shutdown=1,))-app.state.server = server+initialize_asyncio()server.run()diff --git a/marimo/_server/uvicorn_utils.py b/marimo/_server/uvicorn_utils.pyindex ca50c3eb6c..8a5684acd5 100644--- a/marimo/_server/uvicorn_utils.py+++ b/marimo/_server/uvicorn_utils.py@@ -1,14 +1,64 @@# Copyright 2024 Marimo. All rights reserved.+import asyncioimport signal+import sys+from typing import Anyimport uvicorn+from packaging import versionfrom marimo import _loggersLOGGER = _loggers.marimo_logger()+def initialize_signals() -> None:+ # 0.29.0 changed how uvicorn handles signals+ #+ # https://github.com/encode/uvicorn/pull/1600+ #+ # In 0.29.0, uvicorn re-throws signals after quitting, which leads to+ # ungraceful shutdowns since we use a SIGINT to kill uvicorn in the+ # first place+ #+ # Must be called before a uvicorn server is started+ if version.parse(uvicorn.__version__) >= version.parse("0.29.0"):++ def noop(signum: int, frame: Any) -> None:+ del signum+ del frame+ ...++ signal.signal(signal.SIGINT, noop)++def close_uvicorn(server: uvicorn.Server) -> None:LOGGER.debug("Shutting down uvicorn")++ # Tried using sys.exit(0) to quit instead, but that ends up not being+ # graceful due to the event loop still running+ if (+ version.parse(uvicorn.__version__) >= version.parse("0.29.0")+ # pytest appears to run tests outside the main thread, causing+ # signal functions to fail+ and "pytest" not in sys.modules+ ):+ loop = asyncio.get_running_loop()++ # remove interrupt handler -- uvicorn shouldn't re-interrupt marimo+ # only needed because uvicorn saves original handlers before+ # server.run() is called, then re-raises signals ...+ try:+ loop.remove_signal_handler(signal.SIGINT)+ except NotImplementedError:+ # Windows+ def noop(signum: int, frame: Any) -> None:+ del signum+ del frame+ ...++ signal.signal(signal.SIGINT, noop)+server.handle_exit(signal.SIGINT, None)- LOGGER.debug("Uvicorn shut down")++ LOGGER.debug("Shut down uvicorn")