Heuristic:1mcp app Agent Graceful Stop Signal Escalation
| Knowledge Sources | |
|---|---|
| Domains | Reliability, Process_Management |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Escalate from SIGTERM to SIGKILL after a 10-second grace period when stopping the 1MCP runtime, with identity re-verification before the kill to avoid stopping a replacement process.
Description
The `runServeStop` command implements a two-phase signal escalation for graceful runtime shutdown. First, it sends `SIGTERM` to the runtime process and waits up to 10 seconds for it to exit. If the process does not exit within the grace period, it escalates to `SIGKILL` to force termination. Critically, before sending `SIGKILL`, the command re-verifies the process identity (PID + birth timestamp) to ensure the PID has not been recycled and a different process now occupies that PID. This prevents accidentally killing an unrelated process that happened to reuse the same PID.
For background-supervised runtimes, the stop sequence first sends `SIGTERM` to the supervisor, which in turn drains and stops the worker. The cooperative runtime path (`stopCooperativeRuntime`) handles graceful drain with a configurable `--drain-timeout` and `--on-drain-timeout` policy (`restart` or `abort`).
Usage
Use this heuristic when:
- Stopping a 1MCP runtime that is unresponsive to SIGTERM
- Understanding the 10-second grace period before forced termination
- Debugging stop failures where the wrong process is being signaled
- Configuring drain behavior for cooperative runtime restarts
The Insight (Rule of Thumb)
- Action: `serve --stop` sends SIGTERM, waits 10s, then sends SIGKILL if the process is still alive.
- Grace period: 10 seconds (configurable via `deps.gracefulTimeoutMs`, but not exposed as a CLI flag).
- Identity re-verification: Before SIGKILL, the command checks that the process identity matches the original snapshot. If a replacement process is detected, the stop is abandoned.
- Cooperative drain: Use `--drain-timeout <seconds>` to control how long the old runtime has to drain in-flight tool calls. Use `--on-drain-timeout restart|abort` to decide what happens when the drain deadline expires.
- Trade-off: A longer grace period gives the runtime more time for graceful shutdown (draining connections, saving state) but delays the operator. A shorter grace period is more aggressive but may interrupt in-flight work.
Reasoning
SIGTERM allows the runtime to perform cleanup: close MCP server connections, flush logs, persist session state, and release file locks. However, a hung or deadlocked process may never respond to SIGTERM. The 10-second window balances the need for graceful shutdown against the operator's need for the process to actually stop.
PID recycling is a real risk on long-running systems: after a process exits, the OS may assign its PID to a new process. Without identity verification, a delayed SIGKILL could kill the wrong process. The `inspectProcessIdentity` function compares the process start time and PID to the snapshot taken before SIGTERM.
The cooperative drain timeout (`--drain-timeout`) is distinct from the graceful stop timeout. The drain timeout controls how long in-flight MCP tool calls are allowed to complete before the old runtime is stopped. When the drain deadline expires, `--on-drain-timeout restart` forces an immediate restart (interrupting unfinished calls), while `abort` cancels the restart entirely.
Code Evidence
Graceful timeout default from `src/commands/serve/serveStop.ts:220`:
const gracefulTimeoutMs = deps.gracefulTimeoutMs ?? 10000;
Signal escalation from `src/commands/serve/serveStop.ts:586-588`:
logger.warn('serveStop.pid.did.not.exit.after.sigterm.escalating.to.sigkill.554205a0');
// ...
options.kill(pid, 'SIGKILL');
Process exit polling from `src/commands/serve/serveStop.ts:57-71`:
export async function waitForProcessExit(pid: number, options: WaitForExitOptions = {}): Promise<boolean> {
const timeoutMs = options.timeoutMs ?? 10000;
const intervalMs = options.intervalMs ?? 200;
// ...
while (now() - start < timeoutMs) {
if (!isAlive(pid)) return true;
await sleep(intervalMs);
}
return !isAlive(pid);
}
Cooperative drain timeout from `src/commands/serve/cooperativeRuntime.ts:298`:
const onDrainTimeout = z.enum(['restart', 'abort']).parse(options['on-drain-timeout'] ?? 'restart');