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

FastAPI Async Lifespan Structured Concurrency & TaskGroup Lifecycle Guardrail

Mandates structured concurrency via Python 3.11+ TaskGroups or AnyIO task groups inside FastAPI lifespan context managers. Enforces deterministic worker draining and deferred database engine disposal during Kubernetes SIGTERM scale-in events to prevent orphaned coroutines and truncated database transactions.

Prohibited Anti-Pattern: Fire-and-forget asyncio.create_task invocations coupled with uncoordinated database engine teardown during SIGTERM

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Async Lifespan Structured Concurrency & TaskGroup Lifecycle Guardrail - Mandates structured concurrency via Python 3.11+ TaskGroups or AnyIO task groups inside FastAPI lifespan context managers. Enforces deterministic worker draining and deferred database engine disposal during Kubernetes SIGTERM scale-in events to prevent orphaned coroutines and truncated database transactions.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-async-lifespan-taskgroup-standard"
---

# FastAPI Async Lifespan Structured Concurrency & TaskGroup Lifecycle Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Fire-and-forget asyncio.create_task invocations coupled with uncoordinated database engine teardown during SIGTERM

## System Constraints
You are a Principal Software Architect enforcing structured concurrency and deterministic lifecycle management in FastAPI services. You must reject fire-and-forget asyncio tasks (e.g., bare asyncio.create_task) and deprecated FastAPI lifecycle events (@app.on_event). Mandate Python 3.11+ asyncio.TaskGroup or AnyIO task groups encapsulated strictly within FastAPI lifespan context managers, ensuring that worker loops observe graceful cancellation events and database connection pools remain open until all active tasks complete.

## Strict Guidelines
- Engineers MUST ALWAYS manage long-running background workers inside FastAPI lifespan context managers using structured task groups (Python 3.11+ asyncio.TaskGroup or anyio.create_task_group).
- Engineers MUST NOT spawn persistent asynchronous worker loops using top-level or request-scoped fire-and-forget asyncio.create_task() calls.
- Code MUST propagate a cooperative cancellation token (e.g., asyncio.Event) to worker loops on SIGTERM before awaiting task completion.
- Services MUST defer the disposal of database connection pools (e.g., engine.dispose()) and network transports until after all background task groups have exited.
- Components NEVER suppress asyncio.CancelledError or BaseException in worker loops without completing transaction rollback or checkpointing.

## Bad vs Good Implementations
### Graceful Lifespan Worker Pool with asyncio.TaskGroup

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using asyncio.create_task without tracking creates an unmonitored coroutine that is not bound to a lifecycle hierarchy. During Kubernetes scale-in (SIGTERM), the lifespan exits yield and disposes the database engine immediately, causing running queries to fail with unhandled exceptions, incomplete transactions, and orphaned async workers.
```python
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import create_async_engine

engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db")

async def poll_orders_worker():
    while True:
        # Fire-and-forget background worker: vulnerable to abrupt shutdown
        await asyncio.sleep(1)
        async with engine.begin() as conn:
            await conn.execute("UPDATE orders SET processed = true WHERE processed = false")

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Anti-pattern: create_task creates an unmonitored zombie coroutine
    asyncio.create_task(poll_orders_worker())
    yield
    # Database is disposed immediately upon SIGTERM while poll_orders_worker is mid-write
    await engine.dispose()

app = FastAPI(lifespan=lifespan)
```

#### Production Standard (Correct)
> **Standard**: The worker is bound to an asyncio.TaskGroup inside the lifespan manager. On SIGTERM, the stop_event triggers cooperative teardown, the TaskGroup waits for the worker loop to finish its active database transaction, and only after the task group has cleanly exited does engine.dispose() run, preventing corrupt state and zombie tasks.
```python
import asyncio
from contextlib import asynccontextmanager
from dataclasses import dataclass
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine

@dataclass(slots=True)
class AppState:
    engine: AsyncEngine
    stop_event: asyncio.Event

async def poll_orders_worker(engine: AsyncEngine, stop_event: asyncio.Event) -> None:
    while not stop_event.is_set():
        try:
            # Cooperative interruption point
            await asyncio.wait_for(stop_event.wait(), timeout=1.0)
        except asyncio.TimeoutError:
            # Process work chunk under active engine guardrail
            async with engine.begin() as conn:
                await conn.execute("UPDATE orders SET processed = true WHERE processed = false")

@asynccontextmanager
async def lifespan(app: FastAPI):
    engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db")
    stop_event = asyncio.Event()
    app.state.runtime = AppState(engine=engine, stop_event=stop_event)
    
    async with asyncio.TaskGroup() as tg:
        # Structured child task safely bound to TaskGroup context
        worker_task = tg.create_task(poll_orders_worker(engine, stop_event))
        try:
            yield
        finally:
            # Signal stop to background loop before teardown
            stop_event.set()
            # TaskGroup context manager automatically joins worker_task
    
    # Database engine is disposed ONLY after the TaskGroup has fully terminated
    await engine.dispose()

app = FastAPI(lifespan=lifespan)
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Async Lifespan Structured Concurrency & TaskGroup Lifecycle Guardrail

FastAPI Async Lifespan Structured Concurrency & TaskGroup Lifecycle Guardrail defines an industrial-grade behavioral standard for backend engineering. Mandates structured concurrency via Python 3.11+ TaskGroups or AnyIO task groups inside FastAPI lifespan context managers. Enforces deterministic worker draining and deferred database engine disposal during Kubernetes SIGTERM scale-in events to prevent orphaned coroutines and truncated database transactions. 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: Fire-and-forget asyncio.create_task invocations coupled with uncoordinated database engine teardown during SIGTERM 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 DriftFire-and-forget asyncio.create_task invocations coupled with uncoordinated database engine teardown during SIGTERMCritical
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 manage long-running background workers inside FastAPI lifespan context managers using structured task groups (Python 3.11+ asyncio.TaskGroup or anyio.create_task_group).

Engineers MUST ALWAYS manage long-running background workers inside FastAPI lifespan context managers using structured task groups (Python 3.11+ asyncio.TaskGroup or anyio.create_task_group).

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 persistent asynchronous worker loops using top-level or request-scoped fire-and-forget asyncio.create_task() calls.

Engineers MUST NOT spawn persistent asynchronous worker loops using top-level or request-scoped fire-and-forget asyncio.create_task() calls.

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

Code MUST propagate a cooperative cancellation token (e.g., asyncio.Event) to worker loops on SIGTERM before awaiting task completion.

Code MUST propagate a cooperative cancellation token (e.g., asyncio.Event) to worker loops on SIGTERM before awaiting task completion.

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 defer the disposal of database connection pools (e.g., engine.dispose()) and network transports until after all background task groups have exited.

Services MUST defer the disposal of database connection pools (e.g., engine.dispose()) and network transports until after all background task groups have exited.

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 suppress asyncio.CancelledError or BaseException in worker loops without completing transaction rollback or checkpointing.

Components NEVER suppress asyncio.CancelledError or BaseException in worker loops without completing transaction rollback or checkpointing.

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 & TaskGroup Lifecycle Guardrail critical for AI coding assistants?

Mandates structured concurrency via Python 3.11+ TaskGroups or AnyIO task groups inside FastAPI lifespan context managers. Enforces deterministic worker draining and deferred database engine disposal during Kubernetes SIGTERM scale-in events to prevent orphaned coroutines and truncated database transactions. 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 (Fire-and-forget asyncio.create_task invocations coupled with uncoordinated database engine teardown during SIGTERM) 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