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: improve graceful shutdown when running under uv (#4596)
Akshay Agrawal26969edc7c6e
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.pyindex 343ace66e0..1ba168e756 100644--- a/marimo/_cli/sandbox.py+++ b/marimo/_cli/sandbox.py@@ -384,10 +384,14 @@ def run_in_sandbox(def handler(sig: int, frame: Any) -> None:del sigdel frame- if sys.platform == "win32":- os.kill(process.pid, signal.CTRL_C_EVENT)- else:- os.kill(process.pid, signal.SIGINT)+ try:+ if sys.platform == "win32":+ os.kill(process.pid, signal.CTRL_C_EVENT)+ else:+ os.kill(process.pid, signal.SIGINT)+ except ProcessLookupError:+ # Process may have already been terminated.+ passsignal.signal(signal.SIGINT, handler)diff --git a/marimo/_server/api/interrupt.py b/marimo/_server/api/interrupt.pyindex f044b0d6eb..fed003e559 100644--- a/marimo/_server/api/interrupt.py+++ b/marimo/_server/api/interrupt.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport asyncioimport signal+import timefrom typing import Callablefrom marimo._config.settings import GLOBAL_SETTINGS@@ -18,6 +19,7 @@ class InterruptHandler:self.shutdown = shutdownself.loop = asyncio.get_event_loop()self.original_handler = signal.getsignal(signal.SIGINT)+ self._time_of_last_confirmation: float | None = Nonedef _add_interrupt_handler(self) -> None:try:@@ -31,17 +33,15 @@ 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:+ if (+ self._time_of_last_confirmation is not None+ and (time.time() - self._time_of_last_confirmation) < 0.1+ ):+ # uv can send two SIGINTs for every one sent by the user's Ctrl+C;+ # this hack prevents us from spamming the user with confirm messages.+ return+# 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)@@ -55,18 +55,28 @@ class InterruptHandler:self.shutdown()try:- if GLOBAL_SETTINGS.YES:- self.shutdown()- return+ try:+ if GLOBAL_SETTINGS.YES:+ self.shutdown()+ return- response = input(- f"\r{TAB}\033[1;32mAre you sure you want to quit?\033[0m "- "\033[1m(y/n)\033[0m: "- )- if response.lower().strip() == "y":+ response = input(+ f"\r{TAB}Are you sure you want to quit? (y/n): "+ )+ self._time_of_last_confirmation = time.time()+ if response.lower().strip() == "y":+ self.shutdown()+ return+ except (KeyboardInterrupt, EOFError, asyncio.CancelledError):+ print_()self.shutdown()return- except (KeyboardInterrupt, EOFError, asyncio.CancelledError):+ except KeyboardInterrupt:+ # This is a hack to workaround the fact that uv can send two SIGINT for+ # every one entered by the user. Without this extra except block,+ # when running under uv, two Ctrl-C's from the user (which uv turns+ # into three) causes marimo to hang or abort unceremoniously instead+ # of cleanly exiting.print_()self.shutdown()return