Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:1mcp app Agent Background Runtime Supervision

From Leeroopedia
Knowledge Sources
Domains Process_Management, Reliability
Last Updated 2026-10-29 00:00 GMT

Overview

Pattern that supervises a background runtime process with crash recovery via exponential backoff, cooperative upgrade with drain, verified ownership recovery for unclean shutdowns, and graceful shutdown with signal escalation.

Description

Background Runtime Supervision manages a detached supervisor process that owns a runtime scope (config directory, PID file, ownership record). When the worker runtime crashes, the supervisor respawns it after an exponential backoff delay (1s, 2s, 4s, 8s, 16s), resetting the attempt counter after a stable period. Cooperative restart replaces the runtime without downtime by draining in-flight requests before activating the replacement. Unclean shutdown recovery verifies ownership (PID alive + identity match) before reclaiming stale scopes. Graceful shutdown escalates from SIGTERM to SIGKILL with a configurable timeout.

Usage

Use this principle when:

  • A runtime must run detached from the terminal as a background process
  • Crashed workers need automatic restart with backoff to avoid crash loops
  • Runtime upgrades must drain active sessions before activating a replacement
  • Stale ownership from unclean shutdowns must be safely reclaimed
  • Graceful shutdown must escalate from graceful to forced termination

Theoretical Basis

Crash recovery uses exponential backoff:

delay(n)=[1000,2000,4000,8000,16000]n ms

Stable reset after a calm period:

// Abstract algorithm (NOT real implementation)
onWorkerExit(code, signal) {
  if (restartAttempt < maxAttempts) {
    delay = BACKOFF_DELAYS[restartAttempt];
    setTimeout(() => respawn(), delay);
    restartAttempt++;
  }
  // Reset after BACKGROUND_STABLE_RESET_MS of stability
}

Cooperative drain with timeout:

drain(existing, replacement, timeout):
  admission = snapshot(existing)
  if all in-flight complete before timeout:
    activate(replacement)
  else:
    if onDrainTimeout == 'restart': activate(replacement)
    if onDrainTimeout == 'abort': cancel(replacement)

Ownership verification:

reclaimable(scope)=pidAlive(scope.pid)∧identityMatches(scope)∨stale(scope)

Practical Guide

  1. Start background runtime with `serve --background` which spawns a detached supervisor
  2. Monitor with `serve --status` which probes readiness and reports crash-loop/orphaned states
  3. Use `serve --restart` for cooperative restart that drains before activating replacement
  4. Set `--drain-timeout` and `--on-drain-timeout` to control drain behavior
  5. Use `serve --stop` for graceful shutdown with SIGTERM→SIGKILL escalation

Related Pages

Implemented By

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment