Graceful Shutdown

Handle OS signals and drain connections during application shutdown.

1|Updated Sep 14, 2025
One-click install
npx skills add https://github.com/CleanExpo/DR-NRPG --skill graceful-shutdown-cleanexpo
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Graceful Shutdown
Source: https://github.com/CleanExpo/DR-NRPG/tree/main/.skills/custom/graceful-shutdown
Command: npx skills add https://github.com/CleanExpo/DR-NRPG --skill graceful-shutdown-cleanexpo

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill ensures that applications shut down cleanly by properly handling signals, draining active connections, and completing in-flight operations before termination, preventing data loss and ensuring system stability.

Core Features & Use Cases

  • Signal Handling: Catches OS signals like SIGTERM and SIGINT to initiate a controlled shutdown.
  • Connection Draining: Gracefully closes database connections, WebSocket connections, and other persistent resources.
  • Resource Teardown: Ensures background tasks, scheduled jobs, and subagents are properly cancelled or completed.
  • Orchestration Integration: Works with Docker and Kubernetes stop_grace_period and terminationGracePeriodSeconds.
  • Health Probe Awareness: Integrates with readiness probes to signal shutdown status.
  • Use Case: When deploying a web application to Kubernetes, this Skill ensures that all active user requests are finished and database connections are closed before the pod is terminated, preventing errors and data corruption.

Quick Start

Implement the graceful shutdown process for your NodeJS-Starter-V1 application by configuring signal handlers and resource draining logic.

Frequently Asked Questions about Graceful Shutdown

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I handle Kubernetes SIGTERM signals to drain active connections before pod termination?

Graceful shutdown catches OS signals like SIGTERM to initiate controlled connection draining, allowing active requests to finish and database pools to close before the pod termination grace period expires.

What is graceful shutdown and when do I need it for FastAPI or Next.js applications?

Graceful shutdown is the orderly termination of applications by completing in-flight requests and cleaning up resources like WebSockets. It is needed when deploying FastAPI or Next.js to Docker or Kubernetes to prevent data loss.

Does this graceful shutdown approach work with Docker stop_grace_period and Kubernetes terminationGracePeriodSeconds?

Yes, the approach integrates with Docker stop_grace_period and Kubernetes terminationGracePeriodSeconds, ensuring all resource teardown and background task cancellations complete within the bounded grace period.

How do I properly close database pools and WebSocket connections during application shutdown?

To close database pools and WebSocket connections during application shutdown, configure signal handlers that trigger connection draining logic, ensuring persistent resources are properly cleaned up within a bounded grace period.

Why are my FastAPI Uvicorn in-flight requests failing when Kubernetes terminates the pod?

In-flight requests fail during pod termination because the application lacks proper signal handling to drain connections. Implementing graceful shutdown catches SIGTERM to complete active requests before the container exits.

Can I use readiness probes to signal application shutdown status to Kubernetes?

Yes, you can integrate readiness probes to signal shutdown status to Kubernetes, stopping traffic routing to the pod while allowing the application to finish processing active user requests during the termination lifecycle.