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

FastAPI Async Lifespan Structured Concurrency & Deterministic Resource Teardown

Enforces strict structured concurrency and deterministic two-phase resource teardown using ASGI Lifespan context managers in FastAPI applications. Mandates explicit cancellation and completion of asynchronous background tasks alongside ordered draining and closing of distributed connection pools (PostgreSQL, Redis, RabbitMQ) to eliminate socket exhaustion, orphaned coroutines, and dirty process termination.

Prohibited Anti-Pattern: Ad-Hoc Startup Event Handlers and Fire-and-Forget Background Tasks Causing Silent Socket Leaks

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Async Lifespan Structured Concurrency & Deterministic Resource Teardown - Enforces strict structured concurrency and deterministic two-phase resource teardown using ASGI Lifespan context managers in FastAPI applications. Mandates explicit cancellation and completion of asynchronous background tasks alongside ordered draining and closing of distributed connection pools (PostgreSQL, Redis, RabbitMQ) to eliminate socket exhaustion, orphaned coroutines, and dirty process termination.
globs: app/**/*.py, src/**/*.py, main.py, lifespan.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-async-lifespan-structured-teardown"
---

# FastAPI Async Lifespan Structured Concurrency & Deterministic Resource Teardown

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Ad-Hoc Startup Event Handlers and Fire-and-Forget Background Tasks Causing Silent Socket Leaks

## System Constraints
You are a Principal Software Architect enforcing enterprise-grade structured concurrency and lifecycle management in FastAPI services. You must reject deprecated event handlers, unmonitored fire-and-forget background tasks, and unmanaged database engine pools. Enforce deterministic two-phase teardown protocols using `@asynccontextmanager` lifespans that guarantee task cancellation and pool drainage within strict timeout budgets.

## Strict Guidelines
- Engineers MUST ALWAYS use `@asynccontextmanager` ASGI lifespan protocols on `FastAPI(lifespan=...)` instead of deprecated `@app.on_event('startup')` or `@app.on_event('shutdown')` decorators.
- Engineers MUST wrap all lifespan resource finalization logic within a strict `try ... yield ... finally` construct to guarantee teardown execution even during early initialization failure or SIGTERM signals.
- Engineers MUST track all long-running background tasks spawned during application lifespan and explicitly cancel, await, and catch `asyncio.CancelledError` on teardown within an `asyncio.wait_for` timeout envelope.
- Services MUST shut down resources in reverse dependency order: halt external ingress tasks first, drain in-flight message queues second, and terminate stateful database/cache connection pools (`engine.dispose()`, `redis.aclose()`) last.
- Engineers MUST NOT spawn detached background worker loops via `asyncio.create_task` without retaining strong references inside application state (`app.state`) and integrating them into structured lifecycle cancellation.
- Code SHOULD use `asyncio.shield` during critical database commit and socket flushing stages during teardown to prevent abrupt mid-write termination during aggressive SIGKILL escalations.

## Bad vs Good Implementations
### FastAPI Lifespan Structured Concurrency & Pool Teardown

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Uses deprecated on_event handlers, spawns an untracked background task that gets abruptly severed on worker shutdown, and fails to dispose of the SQLAlchemy connection pool, leading to open socket exhaustion and corrupted connection states.
```python
import asyncio
from fastapi import FastAPI
from redis.asyncio import Redis
from sqlalchemy.ext.asyncio import create_async_engine

app = FastAPI()
engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db")
redis = Redis.from_url("redis://localhost")

async def background_poller():
    while True:
        await asyncio.sleep(5)

# Anti-pattern: Deprecated event handlers, unreferenced task, no pool teardown
@app.on_event("startup")
async def startup_event():
    app.state.db = engine
    app.state.redis = redis
    # Fire-and-forget task; socket leaked on SIGTERM
    asyncio.create_task(background_poller())

@app.on_event("shutdown")
async def shutdown_event():
    # Incomplete cleanup: DB connection pool is not disposed, background task is abandoned
    await redis.close()
```

#### Production Standard (Correct)
> **Standard**: Implements the ASGI lifespan context manager protocol. Holds references to workers, terminates execution using bounded timeouts, handles CancelledError gracefully, and disposes of Redis and PostgreSQL socket pools in reverse order of initialization.
```python
import asyncio
import logging
from contextlib import asynccontextmanager
from typing import AsyncGenerator
from fastapi import FastAPI
from redis.asyncio import Redis
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine

logger = logging.getLogger(__name__)

async def background_poller() -> None:
    try:
        while True:
            await asyncio.sleep(5)
    except asyncio.CancelledError:
        logger.info("Background poller cancellation received; flushing state.")
        raise

@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
    # Phase 1: Resource Initialization
    engine: AsyncEngine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db", pool_pre_ping=True)
    redis: Redis = Redis.from_url("redis://localhost", decode_responses=True)
    worker_task = asyncio.create_task(background_poller(), name="background_poller")

    app.state.db_engine = engine
    app.state.redis = redis
    logger.info("All core infrastructure services initialized successfully.")

    try:
        yield
    finally:
        # Phase 2: Ordered Teardown (Reverse Dependency Order)
        logger.info("Commencing structured application teardown...")

        # Step 2a: Cancel background workers
        worker_task.cancel()
        try:
            await asyncio.wait_for(worker_task, timeout=5.0)
        except (asyncio.CancelledError, asyncio.TimeoutError):
            logger.warning("Worker task forcefully terminated after timeout budget.")

        # Step 2b: Drain and close cache sockets
        try:
            await asyncio.wait_for(redis.aclose(), timeout=3.0)
        except Exception as exc:
            logger.error("Error terminating Redis connection pool: %s", exc)

        # Step 2c: Dispose database engine pools
        try:
            await asyncio.wait_for(engine.dispose(), timeout=5.0)
        except Exception as exc:
            logger.error("Error disposing SQLAlchemy engine pool: %s", exc)

        logger.info("Structured teardown completed. Sockets closed safely.")

app = FastAPI(lifespan=lifespan)
```

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 Structured Concurrency & Deterministic Resource Teardown

FastAPI Async Lifespan Structured Concurrency & Deterministic Resource Teardown defines an industrial-grade behavioral standard for backend engineering. Enforces strict structured concurrency and deterministic two-phase resource teardown using ASGI Lifespan context managers in FastAPI applications. Mandates explicit cancellation and completion of asynchronous background tasks alongside ordered draining and closing of distributed connection pools (PostgreSQL, Redis, RabbitMQ) to eliminate socket exhaustion, orphaned coroutines, and dirty process termination. 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: Ad-Hoc Startup Event Handlers and Fire-and-Forget Background Tasks Causing Silent Socket Leaks 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 DriftAd-Hoc Startup Event Handlers and Fire-and-Forget Background Tasks Causing Silent Socket LeaksCritical
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 `@asynccontextmanager` ASGI lifespan protocols on `FastAPI(lifespan=...)` instead of deprecated `@app.on_event('startup')` or `@app.on_event('shutdown')` decorators.

Engineers MUST ALWAYS use `@asynccontextmanager` ASGI lifespan protocols on `FastAPI(lifespan=...)` instead of deprecated `@app.on_event('startup')` or `@app.on_event('shutdown')` decorators.

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 wrap all lifespan resource finalization logic within a strict `try ... yield ... finally` construct to guarantee teardown execution even during early initialization failure or SIGTERM signals.

Engineers MUST wrap all lifespan resource finalization logic within a strict `try ... yield ... finally` construct to guarantee teardown execution even during early initialization failure or SIGTERM signals.

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 track all long-running background tasks spawned during application lifespan and explicitly cancel, await, and catch `asyncio.CancelledError` on teardown within an `asyncio.wait_for` timeout envelope.

Engineers MUST track all long-running background tasks spawned during application lifespan and explicitly cancel, await, and catch `asyncio.CancelledError` on teardown within an `asyncio.wait_for` timeout envelope.

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

Services MUST shut down resources in reverse dependency order

Services MUST shut down resources in reverse dependency order: halt external ingress tasks first, drain in-flight message queues second, and terminate stateful database/cache connection pools (`engine.dispose()`, `redis.aclose()`) last.

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

Engineers MUST NOT spawn detached background worker loops via `asyncio.create_task` without retaining strong references inside application state (`app.state`) and integrating them into structured lifecycle cancellation.

Engineers MUST NOT spawn detached background worker loops via `asyncio.create_task` without retaining strong references inside application state (`app.state`) and integrating them into structured lifecycle 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

Code SHOULD use `asyncio.shield` during critical database commit and socket flushing stages during teardown to prevent abrupt mid-write termination during aggressive SIGKILL escalations.

Code SHOULD use `asyncio.shield` during critical database commit and socket flushing stages during teardown to prevent abrupt mid-write termination during aggressive SIGKILL escalations.

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 Structured Concurrency & Deterministic Resource Teardown critical for AI coding assistants?

Enforces strict structured concurrency and deterministic two-phase resource teardown using ASGI Lifespan context managers in FastAPI applications. Mandates explicit cancellation and completion of asynchronous background tasks alongside ordered draining and closing of distributed connection pools (PostgreSQL, Redis, RabbitMQ) to eliminate socket exhaustion, orphaned coroutines, and dirty process termination. 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 (Ad-Hoc Startup Event Handlers and Fire-and-Forget Background Tasks Causing Silent Socket Leaks) 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