This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: fastapi-lifespan-state-graceful-draining-standard

FastAPI Lifespan State Graceful Draining & Signal Invariance

Enforces deterministic teardown of connection pools and active asynchronous worker tasks within ASGI lifespan contexts during container SIGTERM signals. Prevents abrupt socket termination, unhandled CancelledError leaks, and state corruption across Kubernetes zero-downtime rolling deploys.

Prohibited Anti-Pattern: Premature Resource Destruction and Fire-and-Forget Task Severance in Deprecated Event Handlers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Lifespan State Graceful Draining & Signal Invariance - Enforces deterministic teardown of connection pools and active asynchronous worker tasks within ASGI lifespan contexts during container SIGTERM signals. Prevents abrupt socket termination, unhandled CancelledError leaks, and state corruption across Kubernetes zero-downtime rolling deploys.
globs: **/main.py, **/app.py, **/lifespan.py, **/core/**/*.py, **/api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-lifespan-state-graceful-draining-standard"
---

# FastAPI Lifespan State Graceful Draining & Signal Invariance

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Premature Resource Destruction and Fire-and-Forget Task Severance in Deprecated Event Handlers

## System Constraints
You are a Principal Backend Reliability Architect enforcing strict ASGI Lifespan protocol conformance and graceful termination invariant guarantees in FastAPI services. You must reject deprecated event handlers, untracked background coroutines, and immediate resource disposal without drain timeouts.

## Strict Guidelines
- Engineers MUST ALWAYS use the ASGI lifespan async context manager pattern via `@asynccontextmanager async def lifespan(app: FastAPI)` instead of legacy `@app.on_event('startup')` and `@app.on_event('shutdown')`.
- Engineers MUST manage background coroutines via a shared, thread-safe `set[asyncio.Task]` registry stored in `app.state` to enable graceful awaiting during shutdown rather than invoking fire-and-forget `asyncio.create_task` without retention.
- Engineers MUST NOT tear down downstream connection pools (e.g., PostgreSQL asyncpg pools, Redis clusters, gRPC channels) before setting the readiness sentinel to False and draining in-flight async tasks.
- Code MUST wrap lifespan drain sequences in `asyncio.wait_for()` with a bounded timeout strictly lower than the container orchestrator's `terminationGracePeriodSeconds` (defaulting to a 10-25 second window).
- Services MUST catch `asyncio.CancelledError` within tracked background loops and execute clean rollback or state commit sequences prior to yielding final cancellation.
- Components NEVER instantiate unmanaged global state or initialize persistent network clients outside the explicit lifespan context generator.

## Bad vs Good Implementations
### FastAPI ASGI Lifespan Connection Pool and Task Draining

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using deprecated `@app.on_event` handlers couples resource lifetimes directly to process signals without coordination. Downstream database connections close immediately while in-flight HTTP requests are still executing, and untracked background tasks launched with `asyncio.create_task` are dropped mid-execution, causing silent data loss.
```python
from fastapi import FastAPI
import asyncio
import asyncpg

app = FastAPI()
db_pool = None

@app.on_event("startup")
async def startup():
    global db_pool
    db_pool = await asyncpg.create_pool("postgresql://user:pass@db:5432/production")

@app.on_event("shutdown")
async def shutdown():
    # ANTI-PATTERN: Immediatley terminates pool while in-flight requests may still be querying
    # Background tasks running via create_task are abruptly aborted by SIGKILL without draining
    await db_pool.close()

@app.post("/process")
async def process():
    # Untracked fire-and-forget task severed mid-flight upon container shutdown
    asyncio.create_task(db_pool.execute("INSERT INTO audit_log VALUES (NOW())"))
    return {"status": "accepted"}
```

#### Production Standard (Correct)
> **Standard**: Adheres to the official ASGI lifespan protocol. The state object isolates the connection pool and a monitored task registry. Upon SIGTERM, readiness is immediately set to False so orchestration traffic shifts, followed by a bounded wait (`asyncio.wait_for`) allowing in-flight background operations to finish before database pools are closed.
```python
from contextlib import asynccontextmanager
import asyncio
import logging
from typing import AsyncGenerator
from fastapi import FastAPI, Request
import asyncpg

logger = logging.getLogger("app.lifespan")

@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
    # Phase 1: Resource Initialization
    pool = await asyncpg.create_pool("postgresql://user:pass@db:5432/production", min_size=5, max_size=20)
    app.state.db_pool = pool
    app.state.background_tasks: set[asyncio.Task] = set()
    app.state.is_ready = True
    
    yield
    
    # Phase 2: Graceful Draining Sequence
    app.state.is_ready = False
    logger.info("Readiness flipped to false. Draining active background tasks...")
    
    active_tasks = [t for t in app.state.background_tasks if not t.done()]
    if active_tasks:
        try:
            # Bounded drain: Wait up to 15s for active tasks to conclude
            await asyncio.wait_for(asyncio.gather(*active_tasks, return_exceptions=True), timeout=15.0)
        except asyncio.TimeoutError:
            logger.warning("Timed out waiting for tasks to drain. Cancelling remaining tasks.")
            for task in active_tasks:
                task.cancel()
            await asyncio.gather(*active_tasks, return_exceptions=True)
            
    # Phase 3: Resource Teardown
    logger.info("Closing database connection pool...")
    await pool.close()

app = FastAPI(lifespan=lifespan)

@app.post("/process")
async def process(request: Request):
    async def tracked_worker(pool: asyncpg.Pool):
        try:
            await pool.execute("INSERT INTO audit_log VALUES (NOW())")
        except asyncio.CancelledError:
            logger.warning("Task received cancellation during shutdown drain.")
            raise

    task = asyncio.create_task(tracked_worker(request.app.state.db_pool))
    request.app.state.background_tasks.add(task)
    task.add_done_callback(request.app.state.background_tasks.discard)
    return {"status": "accepted"}
```

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Lifespan State Graceful Draining & Signal Invariance

FastAPI Lifespan State Graceful Draining & Signal Invariance defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic teardown of connection pools and active asynchronous worker tasks within ASGI lifespan contexts during container SIGTERM signals. Prevents abrupt socket termination, unhandled CancelledError leaks, and state corruption across Kubernetes zero-downtime rolling deploys. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Premature Resource Destruction and Fire-and-Forget Task Severance in Deprecated Event Handlers Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftPremature Resource Destruction and Fire-and-Forget Task Severance in Deprecated Event HandlersCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS use the ASGI lifespan async context manager pattern via `@asynccontextmanager async def lifespan(app

Engineers MUST ALWAYS use the ASGI lifespan async context manager pattern via `@asynccontextmanager async def lifespan(app: FastAPI)` instead of legacy `@app.on_event('startup')` and `@app.on_event('shutdown')`.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST manage background coroutines via a shared, thread-safe `set[asyncio.Task]` registry stored in `app.state` to enable graceful awaiting during shutdown rather than invoking fire-and-forget `asyncio.create_task` without retention.

Engineers MUST manage background coroutines via a shared, thread-safe `set[asyncio.Task]` registry stored in `app.state` to enable graceful awaiting during shutdown rather than invoking fire-and-forget `asyncio.create_task` without retention.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST NOT tear down downstream connection pools (e.g., PostgreSQL asyncpg pools, Redis clusters, gRPC channels) before setting the readiness sentinel to False and draining in-flight async tasks.

Engineers MUST NOT tear down downstream connection pools (e.g., PostgreSQL asyncpg pools, Redis clusters, gRPC channels) before setting the readiness sentinel to False and draining in-flight async tasks.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Code MUST wrap lifespan drain sequences in `asyncio.wait_for()` with a bounded timeout strictly lower than the container orchestrator's `terminationGracePeriodSeconds` (defaulting to a 10-25 second window).

Code MUST wrap lifespan drain sequences in `asyncio.wait_for()` with a bounded timeout strictly lower than the container orchestrator's `terminationGracePeriodSeconds` (defaulting to a 10-25 second window).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Services MUST catch `asyncio.CancelledError` within tracked background loops and execute clean rollback or state commit sequences prior to yielding final cancellation.

Services MUST catch `asyncio.CancelledError` within tracked background loops and execute clean rollback or state commit sequences prior to yielding final cancellation.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Components NEVER instantiate unmanaged global state or initialize persistent network clients outside the explicit lifespan context generator.

Components NEVER instantiate unmanaged global state or initialize persistent network clients outside the explicit lifespan context generator.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is FastAPI Lifespan State Graceful Draining & Signal Invariance critical for AI coding assistants?

Enforces deterministic teardown of connection pools and active asynchronous worker tasks within ASGI lifespan contexts during container SIGTERM signals. Prevents abrupt socket termination, unhandled CancelledError leaks, and state corruption across Kubernetes zero-downtime rolling deploys. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Premature Resource Destruction and Fire-and-Forget Task Severance in Deprecated Event Handlers) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

FastAPI