sickn33/agentic-awesome-skills/graceful-shutdown
Claude Code and agent skill by sickn33/agentic-awesome-skills
- community
Checking that the SKILL.md file is still in the repository.
Implement graceful shutdown for servers and workers: drain connections, finish in-flight work, release resources, and exit cleanly on SIGTERM/SIGINT.
Install
npx vskill@latest install sickn33/agentic-awesome-skills/graceful-shutdownSKILL.md
Graceful Shutdown
Overview
A skill for implementing graceful shutdown in servers, workers, and long-running processes. Ensures in-flight requests complete, background jobs finish or checkpoint, database connections close cleanly, and the process exits with a proper status code. Essential for zero-downtime deployments in container orchestrators (Kubernetes, ECS, Docker Compose) and bare-metal process managers (systemd, PM2).
When to Use This Skill
- Use when building an HTTP server that must not drop active connections during deploys
- Use when writing a background worker that processes jobs from a queue
- Use when deploying to Kubernetes, Docker, or any environment that sends SIGTERM before killing
- Use when the user says "graceful shutdown", "drain connections", "handle SIGTERM", "zero downtime", or "don't kill active requests"
- Use when implementing health check endpoints (
/healthz,/readyz) for orchestrators
How It Works
Step 1: Register signal handlers early
Trap SIGTERM (orchestrator shutdown) and SIGINT (Ctrl+C) at process startup. Set a flag so the application knows it is shutting down.
Step 2: Stop accepting new work
Immediately stop the server from accepting new connections. For HTTP servers, call server.close(). For queue workers, stop polling for new jobs.
Step 3: Drain in-flight work with a deadline
Wait for active requests and background tasks to finish, but enforce a hard deadline so the process never hangs indefinitely.
Step 4: Implement readiness and liveness probes
Orchestrators use these to decide whether to route traffic and whether to restart the container. Liveness proves the process is alive; readiness controls whether traffic is routed. While the listener is still available during a drain, keep liveness healthy and return 503 only from readiness. After the listener closes, new probes cannot connect, so do not promise that HTTP liveness remains reachable for the entire termination window.
Step 5: Track active connections
Maintain a count of in-flight requests so you know when draining is complete. Use a once guard covering both finish and close events so that client disconnects (aborted requests) correctly decrement the counter.
Examples
Example 1: Express.js server with graceful shutdown
Example 2: Python FastAPI under Uvicorn
Uvicorn owns SIGTERM handling and request draining. It stops accepting new connections, asks existing connections to shut down, waits for connections and tasks up to --timeout-graceful-shutdown, and only then sends the ASGI lifespan shutdown event. Do not replace its signal handler or wait for requests again inside lifespan; use that hook to release application resources after Uvicorn's drain.
Run Uvicorn with a deadline shorter than the orchestrator's kill timeout:
If readiness must turn 503 before SIGTERM, coordinate a separately secured and tested pre-stop drain signal plus a propagation delay. FastAPI's lifespan shutdown hook is too late for that transition because Uvicorn invokes it after request draining.
Example 3: Process-level worker with a bounded drain
JobQueue is an application-owned adapter. Its poll must honor the timeout, and each job must be idempotent because a crash after work but before ack causes redelivery. This is a process, not a Node worker thread (process signals are not delivered to worker threads).
For checkpointing, design cooperative cancellation inside job.execute and await a confirmed checkpoint before releasing the job. Do not concurrently checkpoint mutable job state from a signal handler while execute/ack still run.
Best Practices
- Always set a drain timeout shorter than the orchestrator's kill timeout (
terminationGracePeriodSecondsin Kubernetes defaults to 30s — use 25s for your drain) - Return
Connection: closeheader on responses sent during draining so HTTP/1.1 clients don't reuse the connection - Reject new requests with 503 during shutdown so load balancers learn faster
- Unref your force-exit timer so it doesn't keep the event loop alive after all work is done
- Flush async buffers (log transports, metric aggregators, write-ahead logs) before exiting
- Use
process.exit(0)for clean shutdown andprocess.exit(1)for timeout/error so orchestrators can distinguish the two - Test shutdown behavior explicitly — simulate SIGTERM in integration tests and verify no requests are dropped
Common Pitfalls
- Problem: Drain-rejection middleware turns both readiness and liveness into 503 while the HTTP listener is still available.
Solution: Register health routes before that middleware and flip only readiness. Once the listener closes, new probes may no longer connect; the shutdown deadline, not a promise of HTTP liveness, bounds termination.
- Problem: Drain hangs until the force-exit timeout even though all clients have disconnected.
Solution: Track request completion with both finish and close events (Node.js) or equivalent. If a client aborts the connection, finish may never fire — close will. Use a once guard to prevent double-decrementing the counter.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/graceful-shutdown/SKILL.md
Popularity
- Repository stars
- 47,054
- Repository forks
- 6,853
- 7-day trend score
- 54
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/sickn33/agentic-awesome-skills/graceful-shutdown)