agentleFS
PradnyeshpPradnyeshp/Claude-Skills/.claude/skills/graceful-shutdown/SKILL.md

graceful-shutdown

Skill2 starsChanged 4 months agoMITView on GitHub

Make a service shut down cleanly on SIGTERM/SIGINT — stop accepting new work, drain in-flight requests, close DB/queue connections, and exit within the platform's grace period — so deploys and restarts don't drop requests or corrupt state. Use when the user says "add graceful shutdown", "handle SIGTERM", "drain connections on shutdown", "stop dropping requests during deploys", "close connections cleanly on exit", or "my app is killed mid-request".

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 4 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Drain in-flight work with a bounded timeout: Wait for active requests/jobs to complete, but cap the wait (e.g.

    line 22
  2. Very specific:

    Release resources in dependency order: After draining, close DB pools, cache/queue clients, and file handles, and flush logs/metrics/traces.

    line 23
  3. Distinctive:

    Verify: Test that sending SIGTERM while a slow request is in flight lets that request finish and returns before exit, that new connections …

    line 25
  4. Distinctive:

    Stop new work and fail readiness before closing the socket, so the balancer drains traffic first.

    line 30
  5. Distinctive:

    Always bound the drain with a timeout under the platform's kill grace period — drain, but never block shutdown indefinitely.

    line 31

≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Runtime, server & signal signals

!grep -rilE "SIGTERM|SIGINT|process\\.on|signal\\.|shutdown|http\\.Server|createServer|express|fastify|nestjs|flask|fastapi|gunicorn|uvicorn|listen\\(|graceful" --include='*.*' package.json pyproject.toml go.mod Gemfile 2>/dev/null | grep -vE 'node_modules|\.git|dist|build' | head -8

Instructions

Make the service in $ARGUMENTS (or the app's entrypoint) terminate cleanly when the platform sends a shutdown signal, so an in-flight request is finished rather than severed and external resources are released. Reuse the project's server and connection objects rather than introducing a new lifecycle framework.

Method

  1. Handle the right signals. Trap SIGTERM (what Kubernetes/Docker/systemd send) and SIGINT (Ctrl-C in dev). Run the shutdown routine once and guard against re-entry if a second signal arrives.
  2. Stop accepting new work first. Close the listening socket / stop the HTTP server from taking new connections, and if the app pulls from a queue, stop consuming. Existing in-flight requests keep running; new ones are refused (or shed to another instance).
  3. Fail readiness immediately. Flip the readiness probe to unhealthy at the start of shutdown so the load balancer / orchestrator stops routing new traffic before the socket closes (see the healthcheck skill). In Kubernetes, expect traffic for a moment after SIGTERM — a short pre-drain delay avoids races.
  4. Drain in-flight work with a bounded timeout. Wait for active requests/jobs to complete, but cap the wait (e.g. 10–30s, comfortably under the platform's kill grace period). If work exceeds the deadline, log what's still pending and proceed — never block forever.
  5. Release resources in dependency order. After draining, close DB pools, cache/queue clients, and file handles, and flush logs/metrics/traces. Close what depends on the network before the things it depends on.
  6. Exit with an honest code. exit(0) on a clean drain; a non-zero code if shutdown timed out or errored. Don't leave the process hanging — the platform will SIGKILL it and undo the graceful intent.
  7. Verify. Test that sending SIGTERM while a slow request is in flight lets that request finish and returns before exit, that new connections are refused during drain, and that the process exits within the deadline.

Rules

  1. Trap SIGTERM (and SIGINT for dev), make the handler idempotent, and never let a second signal trigger a second concurrent shutdown.
  2. Stop new work and fail readiness before closing the socket, so the balancer drains traffic first; then drain in-flight requests.
  3. Always bound the drain with a timeout under the platform's kill grace period — drain, but never block shutdown indefinitely.
  4. Close DB/queue/cache connections and flush telemetry after draining, in dependency order, then exit with a status code that reflects success or timeout.
  5. End by summarizing which signals are handled, the drain timeout and how it relates to the platform grace period, the readiness behavior, and what gets closed on the way out.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.