This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: fastapi-sse-client-disconnect-backpressure

FastAPI Async Generator SSE Disconnect Detection and Upstream Backpressure Invariant

Requires all FastAPI Server-Sent Events (SSE) and StreamingResponse async generators to actively poll ASGI disconnect state via `request.is_disconnected()` or monitor underlying transport signals. Failing to detect disconnects causes orphaned upstream database/API subscriptions, memory leaks, and worker event-loop starvation when clients drop connections midway.

Prohibited Anti-Pattern: Orphaned Infinite Async Generators Consuming Upstream Streams After Client HTTP Disconnection

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Async Generator SSE Disconnect Detection and Upstream Backpressure Invariant - Requires all FastAPI Server-Sent Events (SSE) and StreamingResponse async generators to actively poll ASGI disconnect state via `request.is_disconnected()` or monitor underlying transport signals. Failing to detect disconnects causes orphaned upstream database/API subscriptions, memory leaks, and worker event-loop starvation when clients drop connections midway.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-sse-client-disconnect-backpressure"
---

# FastAPI Async Generator SSE Disconnect Detection and Upstream Backpressure Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Orphaned Infinite Async Generators Consuming Upstream Streams After Client HTTP Disconnection

## System Constraints
You are a Principal Backend Architect enforcing robust ASGI disconnect detection and backpressure propagation for FastAPI and Starlette streaming endpoints. You mandate that any long-running or continuous async generator serving SSE or chunked responses must actively check for client disconnects, clean up upstream iterators/queues upon termination, and never assume Starlette will abort downstream generators automatically without an intervening yield.

## Strict Guidelines
- Engineers MUST ALWAYS pass the `fastapi.Request` object into streaming generator functions to enable ASGI disconnect polling.
- Engineers MUST ALWAYS check `await request.is_disconnected()` on every iteration or within bounded polling intervals during long waits.
- Code SHOULD utilize an `asyncio.TaskGroup` or race `asyncio.wait` between the upstream message consumer and `request.receive()` when upstream generation is push-based or blocking.
- Services MUST clean up upstream subscriptions, database cursors, and message queues in a `finally` block or context manager when the generator is closed or cancelled.
- Engineers MUST NOT rely exclusively on `yield` throwing `GeneratorExit` or `asyncio.CancelledError`, as network buffers or buffering proxies (e.g., NGINX) can delay generator write errors indefinitely.

## Bad vs Good Implementations
### SSE Async Generator Disconnect Polling & Resource Cleanup

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The generator runs indefinitely without inspecting the ASGI disconnect state. If a client closes their browser tab, the async task continues fetching expensive upstream telemetry and consuming event loop ticks.
```python
import asyncio
from fastapi import FastAPI
from fastapi.responses import StreamingResponse

app = FastAPI()

async def unbounded_event_stream():
    # ANTI-PATTERN: Ignores client connection state.
    # If client drops, the generator keeps consuming from upstream broker indefinitely
    # until a TCP write buffer fills and triggers a broken pipe.
    while True:
        data = await fetch_expensive_upstream_telemetry()
        yield f"data: {data}\n\n"
        await asyncio.sleep(1)

@app.get("/stream")
async def stream_telemetry():
    return StreamingResponse(unbounded_event_stream(), media_type="text/event-stream")
```

#### Production Standard (Correct)
> **Standard**: The generator receives the ASGI Request, proactively polls `request.is_disconnected()`, yields heartbeat keep-alives to detect dead sockets, handles `asyncio.CancelledError`, and cleanly closes upstream resources in a `finally` block.
```python
import asyncio
import logging
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse

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

async def bounded_event_stream(request: Request):
    upstream_subscription = await subscribe_upstream_telemetry()
    try:
        while True:
            # INVARIANT: Check ASGI disconnect status before scheduling upstream operations
            if await request.is_disconnected():
                logger.info("Client disconnected; aborting SSE generator upstream ingestion.")
                break

            try:
                # Enforce timeout or race with disconnect to prevent infinite hangs on upstream
                data = await asyncio.wait_for(upstream_subscription.next_event(), timeout=1.0)
                yield f"data: {data}\n\n"
            except asyncio.TimeoutError:
                # Send SSE keep-alive comment to flush pipeline and trigger disconnect if broken
                yield ": keepalive\n\n"
    except asyncio.CancelledError:
        logger.info("Streaming task cancelled by ASGI server.")
        raise
    finally:
        # Clean up upstream resource deterministically
        await upstream_subscription.unsubscribe()

@app.get("/stream")
async def stream_telemetry(request: Request):
    return StreamingResponse(
        bounded_event_stream(request),
        media_type="text/event-stream",
        headers={"Cache-Control": "no-cache", "Connection": "keep-alive"}
    )
```

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 SSE Disconnect Detection and Upstream Backpressure Invariant

FastAPI Async Generator SSE Disconnect Detection and Upstream Backpressure Invariant defines an industrial-grade behavioral standard for backend engineering. Requires all FastAPI Server-Sent Events (SSE) and StreamingResponse async generators to actively poll ASGI disconnect state via `request.is_disconnected()` or monitor underlying transport signals. Failing to detect disconnects causes orphaned upstream database/API subscriptions, memory leaks, and worker event-loop starvation when clients drop connections midway. 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: Orphaned Infinite Async Generators Consuming Upstream Streams After Client HTTP Disconnection 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 DriftOrphaned Infinite Async Generators Consuming Upstream Streams After Client HTTP DisconnectionCritical
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 `fastapi.Request` object into streaming generator functions to enable ASGI disconnect polling.

Engineers MUST ALWAYS pass the `fastapi.Request` object into streaming generator functions to enable ASGI disconnect polling.

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 ALWAYS check `await request.is_disconnected()` on every iteration or within bounded polling intervals during long waits.

Engineers MUST ALWAYS check `await request.is_disconnected()` on every iteration or within bounded polling intervals during long waits.

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 SHOULD utilize an `asyncio.TaskGroup` or race `asyncio.wait` between the upstream message consumer and `request.receive()` when upstream generation is push-based or blocking.

Code SHOULD utilize an `asyncio.TaskGroup` or race `asyncio.wait` between the upstream message consumer and `request.receive()` when upstream generation is push-based or blocking.

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 clean up upstream subscriptions, database cursors, and message queues in a `finally` block or context manager when the generator is closed or cancelled.

Services MUST clean up upstream subscriptions, database cursors, and message queues in a `finally` block or context manager when the generator is closed or cancelled.

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 exclusively on `yield` throwing `GeneratorExit` or `asyncio.CancelledError`, as network buffers or buffering proxies (e.g., NGINX) can delay generator write errors indefinitely.

Engineers MUST NOT rely exclusively on `yield` throwing `GeneratorExit` or `asyncio.CancelledError`, as network buffers or buffering proxies (e.g., NGINX) can delay generator write errors indefinitely.

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 SSE Disconnect Detection and Upstream Backpressure Invariant critical for AI coding assistants?

Requires all FastAPI Server-Sent Events (SSE) and StreamingResponse async generators to actively poll ASGI disconnect state via `request.is_disconnected()` or monitor underlying transport signals. Failing to detect disconnects causes orphaned upstream database/API subscriptions, memory leaks, and worker event-loop starvation when clients drop connections midway. 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 (Orphaned Infinite Async Generators Consuming Upstream Streams After Client HTTP Disconnection) 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