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

FastAPI Async Lifespan Graceful Draining & Deterministic Cancellation Trees

Enforces deterministic cancellation trees, task registry draining, and socket flushing during container SIGTERM teardown within FastAPI async lifespans. Prohibits fire-and-forget unmanaged background tasks and out-of-order resource teardown to eliminate transaction corruption and dropped in-flight requests.

Prohibited Anti-Pattern: Unmanaged Fire-and-Forget Coroutines Severed Mid-Flight During SIGTERM Container Eviction

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Async Lifespan Graceful Draining & Deterministic Cancellation Trees - Enforces deterministic cancellation trees, task registry draining, and socket flushing during container SIGTERM teardown within FastAPI async lifespans. Prohibits fire-and-forget unmanaged background tasks and out-of-order resource teardown to eliminate transaction corruption and dropped in-flight requests.
globs: **/main.py, **/app.py, **/lifespan.py, **/*lifespan*.py, src/**/*.py, app/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-async-lifespan-graceful-draining-invariant"
---

# FastAPI Async Lifespan Graceful Draining & Deterministic Cancellation Trees

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unmanaged Fire-and-Forget Coroutines Severed Mid-Flight During SIGTERM Container Eviction

## System Constraints
You are a Principal Systems Architect enforcing strict structured concurrency and zero-loss socket draining in FastAPI backend services. You MUST reject deprecated Starlette lifecycle events (`@app.on_event`) and untracked `asyncio.create_task` invocations. Mandate strict async lifespan context managers, explicit task registry tracking, bounded timeout draining, and hierarchical resource termination to guarantee clean container evictions under Kubernetes SIGTERM.

## Strict Guidelines
- Engineers MUST ALWAYS implement lifespan handlers using `@asynccontextmanager async def lifespan(app: FastAPI)` instead of deprecated `@app.on_event('startup')` or `@app.on_event('shutdown')`.
- Engineers MUST NOT spawn unbounded background tasks via naked `asyncio.create_task()` without appending the returned task to a managed Lifespan Task Registry or utilizing structured `asyncio.TaskGroup` (Python 3.11+).
- Services MUST coordinate teardown in strict topological order: stop ingress accepting, signal background worker cancellation, await bounded task group drain, and finally close shared state connections (databases, message brokers, HTTP connection pools).
- Code SHOULD enforce a bounded grace period (`asyncio.wait_for` or `asyncio.timeout`) during teardown that is strictly lower than the Kubernetes `terminationGracePeriodSeconds` (e.g., drain deadline of 20s for a 30s pod grace period).
- Components NEVER swallow `asyncio.CancelledError` without re-raising or properly completing unwinding logic, preventing perpetual deadlocks during cancellation propagation.

## Bad vs Good Implementations
### Lifespan Draining Pattern vs. Untracked Fire-and-Forget Execution

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using deprecated `@app.on_event` decouples startup and shutdown context. The `asyncio.create_task` call escapes lifecycle management; upon container SIGTERM, the Uvicorn/Starlette event loop terminates abruptly, severing `unbounded_data_worker` mid-transaction while database connections are dropped, resulting in corrupt data states and unhandled exceptions.
```python
import asyncio
from fastapi import FastAPI

app = FastAPI()

# ANTI-PATTERN: Deprecated lifecycle handlers and orphan background tasks
@app.on_event("startup")
async def startup_event():
    # Untracked task created into global event loop; will be abruptly severed on SIGTERM
    asyncio.create_task(unbounded_data_worker())

@app.on_event("shutdown")
async def shutdown_event():
    # Database pool closed while unbounded_data_worker is mid-write
    await db_engine.dispose()

async def unbounded_data_worker():
    while True:
        await asyncio.sleep(1)
        await db_engine.execute("INSERT INTO events ...")
```

#### Production Standard (Correct)
> **Standard**: Uses the canonical async lifespan context manager with deterministic phase separation. Background tasks are tracked in a strongly-referenced set, signaled via `asyncio.Event`, and awaited with a strict cancellation timeout before the underlying database connection pool is disposed. This eliminates orphan tasks and mid-flight transaction corruption.
```python
import asyncio
from contextlib import asynccontextmanager
from typing import AsyncGenerator
from fastapi import FastAPI

class GracefulWorkerManager:
    def __init__(self) -> None:
        self._tasks: set[asyncio.Task[None]] = set()
        self._shutdown_event: asyncio.Event = asyncio.Event()

    def spawn_worker(self, coro) -> asyncio.Task[None]:
        task = asyncio.create_task(coro)
        self._tasks.add(task)
        task.add_done_callback(self._tasks.discard)
        return task

    async def drain(self, timeout_sec: float = 20.0) -> None:
        self._shutdown_event.set()
        if not self._tasks:
            return
        for task in self._tasks:
            task.cancel()
        try:
            async with asyncio.timeout(timeout_sec):
                await asyncio.gather(*self._tasks, return_exceptions=True)
        except* TimeoutError:
            # Log critical warning: lingering tasks forcefully abandoned after timeout
            pass

worker_manager = GracefulWorkerManager()

@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
    # Phase 1: Resource Initialization
    await db_engine.connect()
    worker_manager.spawn_worker(managed_data_worker(worker_manager._shutdown_event))
    yield
    # Phase 2: Ingress is cut; Drain coroutines deterministically before pool teardown
    await worker_manager.drain(timeout_sec=20.0)
    # Phase 3: Resource Finalization
    await db_engine.dispose()

app = FastAPI(lifespan=lifespan)

async def managed_data_worker(shutdown_event: asyncio.Event) -> None:
    while not shutdown_event.is_set():
        try:
            await asyncio.sleep(1)
            await db_engine.execute("INSERT INTO events ...")
        except asyncio.CancelledError:
            # Complete localized unwinding or clean flush
            raise
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Async Lifespan Graceful Draining & Deterministic Cancellation Trees

FastAPI Async Lifespan Graceful Draining & Deterministic Cancellation Trees defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic cancellation trees, task registry draining, and socket flushing during container SIGTERM teardown within FastAPI async lifespans. Prohibits fire-and-forget unmanaged background tasks and out-of-order resource teardown to eliminate transaction corruption and dropped in-flight requests. 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: Unmanaged Fire-and-Forget Coroutines Severed Mid-Flight During SIGTERM Container Eviction 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 DriftUnmanaged Fire-and-Forget Coroutines Severed Mid-Flight During SIGTERM Container EvictionCritical
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 implement lifespan handlers using `@asynccontextmanager async def lifespan(app

Engineers MUST ALWAYS implement lifespan handlers using `@asynccontextmanager async def lifespan(app: FastAPI)` instead of deprecated `@app.on_event('startup')` or `@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 NOT spawn unbounded background tasks via naked `asyncio.create_task()` without appending the returned task to a managed Lifespan Task Registry or utilizing structured `asyncio.TaskGroup` (Python 3.11+).

Engineers MUST NOT spawn unbounded background tasks via naked `asyncio.create_task()` without appending the returned task to a managed Lifespan Task Registry or utilizing structured `asyncio.TaskGroup` (Python 3.11+).

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

Services MUST coordinate teardown in strict topological order

Services MUST coordinate teardown in strict topological order: stop ingress accepting, signal background worker cancellation, await bounded task group drain, and finally close shared state connections (databases, message brokers, HTTP connection pools).

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 SHOULD enforce a bounded grace period (`asyncio.wait_for` or `asyncio.timeout`) during teardown that is strictly lower than the Kubernetes `terminationGracePeriodSeconds` (e.g., drain deadline of 20s for a 30s pod grace period).

Code SHOULD enforce a bounded grace period (`asyncio.wait_for` or `asyncio.timeout`) during teardown that is strictly lower than the Kubernetes `terminationGracePeriodSeconds` (e.g., drain deadline of 20s for a 30s pod grace period).

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

Components NEVER swallow `asyncio.CancelledError` without re-raising or properly completing unwinding logic, preventing perpetual deadlocks during cancellation propagation.

Components NEVER swallow `asyncio.CancelledError` without re-raising or properly completing unwinding logic, preventing perpetual deadlocks during cancellation propagation.

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 Async Lifespan Graceful Draining & Deterministic Cancellation Trees critical for AI coding assistants?

Enforces deterministic cancellation trees, task registry draining, and socket flushing during container SIGTERM teardown within FastAPI async lifespans. Prohibits fire-and-forget unmanaged background tasks and out-of-order resource teardown to eliminate transaction corruption and dropped in-flight requests. 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 (Unmanaged Fire-and-Forget Coroutines Severed Mid-Flight During SIGTERM Container Eviction) 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