Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

added graceful shutdown

Alishahryar1de7677ba6c1e
diff --git a/api/app.py b/api/app.py
index 15e40a7f..a8d6e688 100644
--- a/api/app.py
+++ b/api/app.py
@@ -1,5 +1,6 @@
"""FastAPI application factory and configuration."""
+import asyncio
import os
# Opt-in to future behavior for python-telegram-bot
@@ -37,6 +38,18 @@ logging.getLogger("uvicorn").setLevel(logging.WARNING)
logging.getLogger("uvicorn.access").setLevel(logging.WARNING)
logging.getLogger("uvicorn.error").setLevel(logging.WARNING)
+_SHUTDOWN_TIMEOUT_S = 5.0
+
+
+async def _best_effort(name: str, awaitable, timeout_s: float = _SHUTDOWN_TIMEOUT_S) -> None:
+ """Run a shutdown step with timeout; never raise to callers."""
+ try:
+ await asyncio.wait_for(awaitable, timeout=timeout_s)
+ except asyncio.TimeoutError:
+ logger.warning(f"Shutdown step timed out: {name} ({timeout_s}s)")
+ except Exception as e:
+ logger.warning(f"Shutdown step failed: {name}: {type(e).__name__}: {e}")
+
@asynccontextmanager
async def lifespan(app: FastAPI):
@@ -144,12 +157,27 @@ async def lifespan(app: FastAPI):
yield
# Cleanup
+ logger.info("Shutdown requested, cleaning up...")
if messaging_platform:
- await messaging_platform.stop()
+ await _best_effort("messaging_platform.stop", messaging_platform.stop())
if cli_manager:
- await cli_manager.stop_all()
- await cleanup_provider()
- logger.info("Server shutting down...")
+ await _best_effort("cli_manager.stop_all", cli_manager.stop_all())
+ await _best_effort("cleanup_provider", cleanup_provider())
+
+ # Ensure background limiter worker doesn't keep the loop alive.
+ try:
+ from messaging.limiter import MessagingRateLimiter
+
+ await _best_effort(
+ "MessagingRateLimiter.shutdown_instance",
+ MessagingRateLimiter.shutdown_instance(),
+ timeout_s=2.0,
+ )
+ except Exception:
+ # Limiter may never have been imported/initialized.
+ pass
+
+ logger.info("Server shut down cleanly")
def create_app() -> FastAPI:
diff --git a/messaging/limiter.py b/messaging/limiter.py
index fdbbb449..8f7c7e40 100644
--- a/messaging/limiter.py
+++ b/messaging/limiter.py
@@ -36,8 +36,8 @@ class MessagingRateLimiter:
async with cls._lock:
if cls._instance is None:
cls._instance = cls()
- # Start the background worker
- asyncio.create_task(cls._instance._worker())
+ # Start the background worker (tracked for graceful shutdown).
+ cls._instance._start_worker()
return cls._instance
def __init__(self):
@@ -55,6 +55,8 @@ class MessagingRateLimiter:
str, tuple[Callable[[], Awaitable[Any]], List[asyncio.Future]]
] = {}
self._condition = asyncio.Condition()
+ self._shutdown = asyncio.Event()
+ self._worker_task: Optional[asyncio.Task] = None
self._initialized = True
self._paused_until = 0
@@ -63,16 +65,26 @@ class MessagingRateLimiter:
f"MessagingRateLimiter initialized ({rate_limit} req / {rate_window}s with Task Compaction)"
)
+ def _start_worker(self) -> None:
+ """Ensure the worker task exists."""
+ if self._worker_task and not self._worker_task.done():
+ return
+ # Named task helps debugging shutdown hangs.
+ self._worker_task = asyncio.create_task(self._worker(), name="msg-limiter-worker")
+
async def _worker(self):
"""Background worker that processes queued messaging tasks."""
logger.info("MessagingRateLimiter worker started")
- while True:
+ while not self._shutdown.is_set():
try:
# Get a task from the queue
async with self._condition:
- while not self._queue_list:
+ while not self._queue_list and not self._shutdown.is_set():
await self._condition.wait()
+ if self._shutdown.is_set():
+ break
+
dedup_key = self._queue_list.pop(0)
func, futures = self._queue_map.pop(dedup_key)
@@ -135,6 +147,44 @@ class MessagingRateLimiter:
)
await asyncio.sleep(1)
+ async def shutdown(self, timeout: float = 2.0) -> None:
+ """Stop the background worker so process shutdown doesn't hang."""
+ self._shutdown.set()
+ try:
+ async with self._condition:
+ self._condition.notify_all()
+ except Exception:
+ # Best-effort: condition may be bound to a closing loop.
+ pass
+
+ task = self._worker_task
+ if not task or task.done():
+ self._worker_task = None
+ return
+
+ task.cancel()
+ try:
+ await asyncio.wait_for(task, timeout=timeout)
+ except asyncio.TimeoutError:
+ logger.warning("MessagingRateLimiter worker did not stop before timeout")
+ except asyncio.CancelledError:
+ pass
+ except Exception as e:
+ logger.debug(f"MessagingRateLimiter worker shutdown error: {e}")
+ finally:
+ self._worker_task = None
+
+ @classmethod
+ async def shutdown_instance(cls, timeout: float = 2.0) -> None:
+ """Shutdown and clear the singleton instance (safe to call multiple times)."""
+ inst = cls._instance
+ if not inst:
+ return
+ try:
+ await inst.shutdown(timeout=timeout)
+ finally:
+ cls._instance = None
+
async def _enqueue_internal(self, func, future, dedup_key, front=False):
await self._enqueue_internal_multi(func, [future], dedup_key, front)
diff --git a/tests/test_app_lifespan_and_errors.py b/tests/test_app_lifespan_and_errors.py
index 3a97a34f..bbe94e82 100644
--- a/tests/test_app_lifespan_and_errors.py
+++ b/tests/test_app_lifespan_and_errors.py
@@ -146,3 +146,54 @@ def test_app_lifespan_sets_state_and_cleans_up(tmp_path, messaging_enabled):
assert getattr(app.state, "messaging_platform", "missing") is None
cleanup_provider.assert_awaited_once()
+
+
+def test_app_lifespan_cleanup_continues_if_platform_stop_raises(tmp_path):
+ from api.app import create_app
+
+ app = create_app()
+
+ settings = SimpleNamespace(
+ messaging_platform="telegram",
+ telegram_bot_token="token",
+ allowed_telegram_user_id="123",
+ allowed_dir=str(tmp_path / "workspace"),
+ claude_workspace=str(tmp_path / "data"),
+ host="127.0.0.1",
+ port=8082,
+ max_cli_sessions=1,
+ log_file=str(tmp_path / "server.log"),
+ )
+
+ fake_platform = MagicMock()
+ fake_platform.name = "fake"
+ fake_platform.on_message = MagicMock()
+ fake_platform.start = AsyncMock()
+ fake_platform.stop = AsyncMock(side_effect=RuntimeError("stop failed"))
+
+ session_store = MagicMock()
+ session_store.get_all_trees.return_value = []
+ session_store.get_node_mapping.return_value = {}
+ session_store.sync_from_tree_data = MagicMock()
+
+ cli_manager = MagicMock()
+ cli_manager.stop_all = AsyncMock()
+
+ api_app_mod = importlib.import_module("api.app")
+ cleanup_provider = AsyncMock()
+ with patch.object(api_app_mod, "get_settings", return_value=settings), patch.object(
+ api_app_mod, "cleanup_provider", new=cleanup_provider
+ ), patch(
+ "messaging.factory.create_messaging_platform",
+ return_value=fake_platform,
+ ), patch(
+ "messaging.session.SessionStore", return_value=session_store
+ ), patch(
+ "cli.manager.CLISessionManager", return_value=cli_manager
+ ):
+ with TestClient(app):
+ pass
+
+ fake_platform.stop.assert_awaited_once()
+ cli_manager.stop_all.assert_awaited_once()
+ cleanup_provider.assert_awaited_once()
diff --git a/tests/test_limiter.py b/tests/test_limiter.py
index d3fc49b3..23d40870 100644
--- a/tests/test_limiter.py
+++ b/tests/test_limiter.py
@@ -22,17 +22,14 @@ class TestMessagingRateLimiter:
@pytest_asyncio.fixture(autouse=True)
async def reset_limiter(self):
"""Reset singleton and environment before each test."""
- # Reset the singleton manually if needed (although __init__ protection exists)
- if MessagingRateLimiter._instance:
- # Stop worker if possible or just reset instance
- pass
- MessagingRateLimiter._instance = None
+ # Ensure the singleton worker is stopped between tests to avoid dangling tasks.
+ await MessagingRateLimiter.shutdown_instance(timeout=0.1)
os.environ["MESSAGING_RATE_LIMIT"] = "1"
os.environ["MESSAGING_RATE_WINDOW"] = "0.5"
yield
- MessagingRateLimiter._instance = None
+ await MessagingRateLimiter.shutdown_instance(timeout=0.1)
@pytest.mark.asyncio
async def test_singleton_pattern(self):