This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: fastapi-async-generator-streaming-session-cleanup

FastAPI Async Generator Lifecycle & DB Session Cleanup in Streaming Responses

Enforces deterministic teardown of asynchronous database sessions and contextual resources during FastAPI StreamingResponse lifecycles. Guards against database connection pool starvation caused by unhandled asyncio.CancelledError and GeneratorExit exceptions when clients disconnect prematurely during chunk iteration.

Prohibited Anti-Pattern: Leaking Active Database Connections via Unhandled Generator Cancellations in Streaming Responses

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Async Generator Lifecycle & DB Session Cleanup in Streaming Responses - Enforces deterministic teardown of asynchronous database sessions and contextual resources during FastAPI StreamingResponse lifecycles. Guards against database connection pool starvation caused by unhandled asyncio.CancelledError and GeneratorExit exceptions when clients disconnect prematurely during chunk iteration.
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-generator-streaming-session-cleanup"
---

# FastAPI Async Generator Lifecycle & DB Session Cleanup in Streaming Responses

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Leaking Active Database Connections via Unhandled Generator Cancellations in Streaming Responses

## System Constraints
You are a Principal Backend Architect enforcing production invariants for FastAPI and Pydantic v2 streaming services. You must enforce deterministic resource teardown, preventing connection pool exhaustion and dangling asynchronous database sessions on client disconnections.

## Strict Guidelines
- Engineers MUST ALWAYS wrap async generator bodies in explicit `try...finally` blocks or asynchronous context managers (`async with`) to guarantee session close and rollback on premature client aborts.
- Engineers MUST NOT rely on FastAPI's `Depends(get_db)` to manage database session lifetimes for `StreamingResponse` payloads, as route dependency teardown occurs outside the asynchronous streaming iterator loop.
- Engineers MUST explicitly handle both `asyncio.CancelledError` and `GeneratorExit` within generator loops yielding chunks to the client.
- Engineers MUST serialize output records using Pydantic v2 `model.model_dump_json()` before yielding bytes to decouple serialization overhead from the active database cursor lifecycle.
- Async generators streaming from database cursors MUST explicitly roll back uncommitted transactions in the `finally` block before closing the session to prevent connection recycling poisons.
- Services MUST monitor client disconnection states via `request.is_disconnected()` in long-running stream iterations to preemptively break execution loops before invoking heavy database round-trips.

## Bad vs Good Implementations
### Database Streaming Response Lifecycle and Session Teardown

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using FastAPI's `Depends(get_db)` injects an external session whose context exit timing conflicts with `StreamingResponse`. When a client abruptly disconnects, `GeneratorExit` or `asyncio.CancelledError` is raised inside the generator; without local `try...finally` semantics and transaction rollback, the SQLAlchemy connection remains unreturned to the pool, triggering starvation under high concurrency.
```python
from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.models import Item
from sqlalchemy import select

router = APIRouter()

# ANTI-PATTERN: get_db dependency closes before or unpredictably during stream execution.
# If client disconnects, GeneratorExit leaves the cursor open and leaks pool connections.
@router.get('/items/stream')
async def stream_items(db: AsyncSession = Depends(get_db)):
    async def item_generator():
        result = await db.stream(select(Item))
        async for row in result.scalars():
            yield row.model_dump_json() + '\n'

    return StreamingResponse(item_generator(), media_type='application/x-ndjson')
```

#### Production Standard (Correct)
> **Standard**: The session lifecycle is fully owned inside the generator using `async with async_session_factory()`. A robust `try...finally` block guarantees `session.rollback()` and connection deallocation even if the client disconnects or an `asyncio.CancelledError`/`GeneratorExit` is raised mid-stream.
```python
import asyncio
from collections.abc import AsyncGenerator
from fastapi import APIRouter, Request
from fastapi.responses import StreamingResponse
from sqlalchemy import select
from app.database import async_session_factory
from app.models import Item
from app.schemas import ItemRead

router = APIRouter()

async def iter_items_stream(request: Request) -> AsyncGenerator[str, None]:
    # INVARIANT: Localize session scope to the generator lifecycle with explicit cleanup
    async with async_session_factory() as session:
        try:
            result = await session.stream(select(Item))
            async for row in result.scalars():
                if await request.is_disconnected():
                    break
                # Serialize via Pydantic v2 model before yield
                serialized = ItemRead.model_validate(row).model_dump_json()
                yield f"{serialized}\n"
        except (asyncio.CancelledError, GeneratorExit):
            # Handle client aborts gracefully
            raise
        finally:
            # Ensure clean rollback and connection pool return
            await session.rollback()
```

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 Generator Lifecycle & DB Session Cleanup in Streaming Responses

FastAPI Async Generator Lifecycle & DB Session Cleanup in Streaming Responses defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic teardown of asynchronous database sessions and contextual resources during FastAPI StreamingResponse lifecycles. Guards against database connection pool starvation caused by unhandled asyncio.CancelledError and GeneratorExit exceptions when clients disconnect prematurely during chunk iteration. 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: Leaking Active Database Connections via Unhandled Generator Cancellations in Streaming Responses 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 DriftLeaking Active Database Connections via Unhandled Generator Cancellations in Streaming ResponsesCritical
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 wrap async generator bodies in explicit `try...finally` blocks or asynchronous context managers (`async with`) to guarantee session close and rollback on premature client aborts.

Engineers MUST ALWAYS wrap async generator bodies in explicit `try...finally` blocks or asynchronous context managers (`async with`) to guarantee session close and rollback on premature client aborts.

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 rely on FastAPI's `Depends(get_db)` to manage database session lifetimes for `StreamingResponse` payloads, as route dependency teardown occurs outside the asynchronous streaming iterator loop.

Engineers MUST NOT rely on FastAPI's `Depends(get_db)` to manage database session lifetimes for `StreamingResponse` payloads, as route dependency teardown occurs outside the asynchronous streaming iterator loop.

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 explicitly handle both `asyncio.CancelledError` and `GeneratorExit` within generator loops yielding chunks to the client.

Engineers MUST explicitly handle both `asyncio.CancelledError` and `GeneratorExit` within generator loops yielding chunks to the client.

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

Engineers MUST serialize output records using Pydantic v2 `model.model_dump_json()` before yielding bytes to decouple serialization overhead from the active database cursor lifecycle.

Engineers MUST serialize output records using Pydantic v2 `model.model_dump_json()` before yielding bytes to decouple serialization overhead from the active database cursor lifecycle.

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

Async generators streaming from database cursors MUST explicitly roll back uncommitted transactions in the `finally` block before closing the session to prevent connection recycling poisons.

Async generators streaming from database cursors MUST explicitly roll back uncommitted transactions in the `finally` block before closing the session to prevent connection recycling poisons.

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

Services MUST monitor client disconnection states via `request.is_disconnected()` in long-running stream iterations to preemptively break execution loops before invoking heavy database round-trips.

Services MUST monitor client disconnection states via `request.is_disconnected()` in long-running stream iterations to preemptively break execution loops before invoking heavy database round-trips.

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 Generator Lifecycle & DB Session Cleanup in Streaming Responses critical for AI coding assistants?

Enforces deterministic teardown of asynchronous database sessions and contextual resources during FastAPI StreamingResponse lifecycles. Guards against database connection pool starvation caused by unhandled asyncio.CancelledError and GeneratorExit exceptions when clients disconnect prematurely during chunk iteration. 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 (Leaking Active Database Connections via Unhandled Generator Cancellations in Streaming Responses) 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