feat(shutdown): graceful shutdown timeout with force exit | 优雅关闭超时
Summary | 概述
Add a shutdown timeout that force-exits the process if graceful shutdown hangs, preventing zombie processes that require manual kill.
添加关闭超时机制,防止进程因 sdk 连接卡死而僵死。
Motivation | 动机
When the iii-engine WebSocket connection is stuck or the viewer server refuses to close, the shutdown handler hangs indefinitely. SIGTERM from systemd times out after 90s and sends SIGKILL, but during those 90s the process is unresponsive. Adding a configurable force-exit timeout ensures clean termination in bounded time.
当 iii-engine 连接卡死或 viewer 服务器拒绝关闭时,shutdown 流程永久挂起,形成僵尸进程。
Changes | 改动
- Added
AGENTMEMORY_SHUTDOWN_TIMEOUT_MSenv var (default: 10000ms) - On SIGINT/SIGTERM, starts a timer that calls
process.exit(0)if shutdown exceeds the timeout - Timer uses
unref()so it doesn't prevent normal exit if shutdown completes quickly - Calls
clearTimeoutwhen shutdown completes normally
Backwards Compatibility | 向后兼容
Default timeout is 10s. Completely transparent when shutdown works normally — the timer is cleared before firing.
Summary by CodeRabbit
- Bug Fixes
- Improved shutdown robustness by implementing a configurable timeout mechanism (default 10 seconds) that ensures the application exits cleanly rather than hanging indefinitely during shutdown.
@mechanic-Q is attempting to deploy a commit to the rohitg00's projects Team on Vercel.
A member of the Team first needs to authorize it.
Added a shutdown timeout mechanism to the shutdown routine that enforces an overall timeout via the Shutdown Timeout Mechanism 🎯 2 (Simple) | ⏱️ ~8 minutes 🐰 A rabbit's ode to patience lost, ✏️ Tip: You can configure your own custom pre-merge checks in the settings.📝 Walkthrough
Walkthrough
AGENTMEMORY_SHUTDOWN_TIMEOUT_MS environment variable (default 10000 ms). A timer forces process exit if shutdown exceeds the timeout, and the timer is cleared after normal shutdown completes.Changes
Layer / File(s)
Summary
Timeout Setup
src/index.ts (lines 417–424)Initialize a
forceExit timer that logs a warning and forces process.exit(0) if shutdown exceeds the configured timeout. Timer is unref()d to avoid blocking process termination.
Timeout Cleanup
src/index.ts (lines 433–434)Clear the
forceExit timer after normal shutdown steps complete, then perform the final process.exit(0).Estimated code review effort
Poem
No hanging shutdowns! Here's the cost—
A timeout timer, swift and bright,
Ensures the process says goodnight. ✨🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name
Status
Explanation
Resolution
Docstring Coverage
⚠️ Warning
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.
Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name
Status
Explanation
Description Check
✅ Passed
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
✅ Passed
The title accurately describes the main change: adding a graceful shutdown timeout with force exit mechanism. It is concise, specific, and clearly summarizes the primary feature being added.
Linked Issues check
✅ Passed
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
✅ Passed
Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Review rate limit: 5/8 reviews remaining, refill in 22 minutes and 28 seconds.Comment @coderabbitai help to get the list of available commands and usage tips.
Thanks @mechanic-Q — the timeout itself is a real improvement and the unref() is a nice touch so the timer never holds the loop open after a clean exit.
Two things to fix before merge — both flagged by CodeRabbit's outside-diff review and worth addressing while you're here:
1. Make the shutdown handler idempotent.
SIGINT and SIGTERM can both reach shutdown() while the first invocation is still mid-flight (viewerServer.close(), indexPersistence.save(), sdk.shutdown()). A second pass spawns a duplicate force-exit timer and tries to double-close everything. Easiest fix:
let shuttingDown = false;
const shutdown = async () => {
if (shuttingDown) return;
shuttingDown = true;
// ... existing body
};
2. Validate the env var.
parseInt("abc", 10) returns NaN, which setTimeout(..., NaN) collapses to 1ms — i.e. you'd force-exit before any cleanup runs. Fall back to the literal default when invalid:
const raw = parseInt(process.env["AGENTMEMORY_SHUTDOWN_TIMEOUT_MS"] || "10000", 10);
const timeoutMs = Number.isFinite(raw) && raw > 0 ? raw : 10000;
Optional but appreciated: add AGENTMEMORY_SHUTDOWN_TIMEOUT_MS to the env block in README.md so users know it exists.
Happy to merge once those land.