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

FastAPI StreamingResponse Upstream Async Generator Disconnect Cancellation Guardrail

FastAPI `StreamingResponse` iterators do not automatically interrupt long-running upstream database or network generators when clients abort connections unless explicit ASGI disconnect monitoring is bound to the async generator loop. Unbound stream generators continue iterating in background tasks, exhausting connection pools, leaking cursor memory, and leaving zombie database transactions open. This standard mandates explicit request disconnect polling via `request.is_disconnected()`, `asyncio.CancelledError` trapping, and deterministic resource release within upstream async generator iterators.

Prohibited Anti-Pattern: Unmonitored StreamingResponse generator causing zombie database queries and pool exhaustion on client disconnect

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI StreamingResponse Upstream Async Generator Disconnect Cancellation Guardrail - FastAPI `StreamingResponse` iterators do not automatically interrupt long-running upstream database or network generators when clients abort connections unless explicit ASGI disconnect monitoring is bound to the async generator loop. Unbound stream generators continue iterating in background tasks, exhausting connection pools, leaking cursor memory, and leaving zombie database transactions open. This standard mandates explicit request disconnect polling via `request.is_disconnected()`, `asyncio.CancelledError` trapping, and deterministic resource release within upstream async generator iterators.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-streamingresponse-async-generator-cancellation-guardrail"
---

# FastAPI StreamingResponse Upstream Async Generator Disconnect Cancellation Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unmonitored StreamingResponse generator causing zombie database queries and pool exhaustion on client disconnect

## System Constraints
You are a Principal Backend Architect enforcing strict ASGI lifecycle and async cancellation semantics in FastAPI. You must ensure all `StreamingResponse` streaming iterators actively monitor client disconnection status via `request.is_disconnected()`, properly propagate `asyncio.CancelledError`, and deterministically close upstream database cursors and network buffers when clients terminate HTTP streams prematurely.

## Strict Guidelines
- Engineers MUST ALWAYS pass the ASGI `Request` instance into any async generator passed to `StreamingResponse`.
- Engineers MUST periodically check `await request.is_disconnected()` inside the generator loop before or immediately following expensive chunk yields.
- Engineers MUST wrap streaming yields in `try...finally` blocks or asynchronous context managers to guarantee deterministic disposal of database sessions, cursors, or upstream network sockets.
- Engineers MUST NOT swallow `asyncio.CancelledError` or `GeneratorExit`; they must be caught only to perform immediate cleanup and re-raised or allowed to terminate the iterator.
- Engineers MUST NOT rely solely on Starlette/FastAPI middleware to abort un-awaited upstream background generators, as ASGI `http.disconnect` events only terminate the response writer, not detached generator tasks.
- Streaming generators MUST break early and cleanly raise or return when `request.is_disconnected()` evaluates to `True`.

## Bad vs Good Implementations
### Unbounded StreamingResponse generator vs. Disconnect-aware Generator with Guaranteed Resource Teardown

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: When an HTTP client aborts the request, FastAPI's ASGI response sender stops reading from the generator, but `stream_report_bad` continues executing until the entire query is fetched. The database session remains locked, consuming async worker loop time and exhausting connection pools under high abort/cancellation rates.
```python
from fastapi import FastAPI, Depends
from fastapi.responses import StreamingResponse
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db

app = FastAPI()

async def stream_report_bad(session: AsyncSession):
    # ANTI-PATTERN: If client disconnects at chunk 5, loop continues processing all 100,000
    # rows in memory. Session is never returned to the pool until complete iteration.
    result = await session.stream(LargeQuery())
    async for row in result:
        yield row.to_csv_chunk()

@app.get('/export')
async def export_data(db: AsyncSession = Depends(get_db)):
    return StreamingResponse(
        stream_report_bad(db),
        media_type='text/csv'
    )
```

#### Production Standard (Correct)
> **Standard**: By injecting `Request` and checking `await request.is_disconnected()`, the generator immediately halts execution when the client closes the TCP socket. Enclosing the logic in `try...finally` with `CancelledError` handling guarantees that upstream database cursors and sessions are cleanly reclaimed immediately rather than leaking in the event loop.
```python
import asyncio
import logging
from fastapi import FastAPI, Request, Depends
from fastapi.responses import StreamingResponse
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db

logger = logging.getLogger(__name__)
app = FastAPI()

async def stream_report_good(request: Request, session: AsyncSession):
    try:
        result = await session.stream(LargeQuery())
        async for row in result:
            if await request.is_disconnected():
                logger.warning('Client disconnected prematurely; aborting query iterator.')
                break
            yield row.to_csv_chunk()
            # Cooperative scheduling checkpoint
            await asyncio.sleep(0)
    except (asyncio.CancelledError, GeneratorExit):
        logger.info('Stream generator explicitly cancelled.')
        raise
    finally:
        # Invariant: Guarantee clean session and cursor release
        await session.close()

@app.get('/export')
async def export_data(request: Request, db: AsyncSession = Depends(get_db)):
    return StreamingResponse(
        stream_report_good(request, db),
        media_type='text/csv'
    )
```

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 StreamingResponse Upstream Async Generator Disconnect Cancellation Guardrail

FastAPI StreamingResponse Upstream Async Generator Disconnect Cancellation Guardrail defines an industrial-grade behavioral standard for backend engineering. FastAPI `StreamingResponse` iterators do not automatically interrupt long-running upstream database or network generators when clients abort connections unless explicit ASGI disconnect monitoring is bound to the async generator loop. Unbound stream generators continue iterating in background tasks, exhausting connection pools, leaking cursor memory, and leaving zombie database transactions open. This standard mandates explicit request disconnect polling via `request.is_disconnected()`, `asyncio.CancelledError` trapping, and deterministic resource release within upstream async generator iterators. 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: Unmonitored StreamingResponse generator causing zombie database queries and pool exhaustion on client disconnect 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 DriftUnmonitored StreamingResponse generator causing zombie database queries and pool exhaustion on client disconnectCritical
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 pass the ASGI `Request` instance into any async generator passed to `StreamingResponse`.

Engineers MUST ALWAYS pass the ASGI `Request` instance into any async generator passed to `StreamingResponse`.

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 periodically check `await request.is_disconnected()` inside the generator loop before or immediately following expensive chunk yields.

Engineers MUST periodically check `await request.is_disconnected()` inside the generator loop before or immediately following expensive chunk yields.

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 wrap streaming yields in `try...finally` blocks or asynchronous context managers to guarantee deterministic disposal of database sessions, cursors, or upstream network sockets.

Engineers MUST wrap streaming yields in `try...finally` blocks or asynchronous context managers to guarantee deterministic disposal of database sessions, cursors, or upstream network sockets.

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 NOT swallow `asyncio.CancelledError` or `GeneratorExit`

Engineers MUST NOT swallow `asyncio.CancelledError` or `GeneratorExit`; they must be caught only to perform immediate cleanup and re-raised or allowed to terminate the iterator.

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 rely solely on Starlette/FastAPI middleware to abort un-awaited upstream background generators, as ASGI `http.disconnect` events only terminate the response writer, not detached generator tasks.

Engineers MUST NOT rely solely on Starlette/FastAPI middleware to abort un-awaited upstream background generators, as ASGI `http.disconnect` events only terminate the response writer, not detached generator 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.
6

Streaming generators MUST break early and cleanly raise or return when `request.is_disconnected()` evaluates to `True`.

Streaming generators MUST break early and cleanly raise or return when `request.is_disconnected()` evaluates to `True`.

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 StreamingResponse Upstream Async Generator Disconnect Cancellation Guardrail critical for AI coding assistants?

FastAPI `StreamingResponse` iterators do not automatically interrupt long-running upstream database or network generators when clients abort connections unless explicit ASGI disconnect monitoring is bound to the async generator loop. Unbound stream generators continue iterating in background tasks, exhausting connection pools, leaking cursor memory, and leaving zombie database transactions open. This standard mandates explicit request disconnect polling via `request.is_disconnected()`, `asyncio.CancelledError` trapping, and deterministic resource release within upstream async generator iterators. 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 (Unmonitored StreamingResponse generator causing zombie database queries and pool exhaustion on client disconnect) 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