This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: fastapi-anyio-sse-taskgroup-cancellation-invariant

FastAPI AnyIO SSE TaskGroup Structured Concurrency & Socket Disconnect Guardrail

Enforces strict structured concurrency using AnyIO task groups and cancel scopes for all FastAPI Server-Sent Events (SSE) and long-lived streaming endpoints. Mandates bidirectional client-disconnect listening and immediate cancellation of upstream database cursors, LLM inference streams, and background workers upon socket termination to prevent resource exhaustion and connection leaks.

Prohibited Anti-Pattern: Orphaned Streaming Tasks and Unbounded Database Cursor Leaks on Silent SSE Disconnects

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI AnyIO SSE TaskGroup Structured Concurrency & Socket Disconnect Guardrail - Enforces strict structured concurrency using AnyIO task groups and cancel scopes for all FastAPI Server-Sent Events (SSE) and long-lived streaming endpoints. Mandates bidirectional client-disconnect listening and immediate cancellation of upstream database cursors, LLM inference streams, and background workers upon socket termination to prevent resource exhaustion and connection leaks.
globs: **/api/**/*.py, **/routers/**/*.py, **/endpoints/**/*.py, **/streaming/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-anyio-sse-taskgroup-cancellation-invariant"
---

# FastAPI AnyIO SSE TaskGroup Structured Concurrency & Socket Disconnect Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Orphaned Streaming Tasks and Unbounded Database Cursor Leaks on Silent SSE Disconnects

## System Constraints
You are a Principal Software Architect enforcing production-grade structured concurrency invariants in Python/FastAPI backends. All Server-Sent Events (SSE) and streaming generators MUST be bound to AnyIO TaskGroups or CancelScopes monitored against `request.is_disconnected()`. Unbounded generators, fire-and-forget background tasks during streaming, and untracked database/RPC iterators are critical architecture violations that cause thread/connection starvation.

## Strict Guidelines
- Engineers MUST ALWAYS monitor `request.is_disconnected()` in an independent, concurrent task within an `anyio.create_task_group()` to signal downstream cancel scopes immediately upon client disconnect.
- Engineers MUST NOT use unshielded infinite generators or native `asyncio.create_task()` inside SSE endpoints without explicit lifecycle ownership tied to the streaming HTTP connection.
- Streaming responses MUST wrap external downstream resources (database cursors, async Redis pub/sub iterators, LLM API client sessions) in deterministic `try...finally` or asynchronous context managers to release pool leases upon cancellation.
- Services MUST yield raw string or EventSource-compliant packets using an intermediary memory object stream (`anyio.create_memory_object_stream()`) rather than executing blocking work inside the generator body.
- Components NEVER assume `StreamingResponse` will automatically terminate deep background coroutines if the client terminates the TCP socket without an active AnyIO cancel scope cancellation trigger.

## Bad vs Good Implementations
### FastAPI SSE Generator Resource Leak vs. AnyIO TaskGroup Structured Concurrency

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: When an SSE client drops the socket (e.g., closing browser tab), `StreamingResponse` only halts when the generator attempts to yield to a broken pipe. Heavy upstream processing, database cursors, and network calls continue executing in the background, exhausting database connection pools and CPU threads.
```python
import asyncio
from fastapi import APIRouter, Request
from fastapi.responses import StreamingResponse
from db import get_db_session

router = APIRouter()

@router.get("/events")
async def stream_events(request: Request):
    # ANTI-PATTERN: If client disconnects, cursor and loop run indefinitely until next yield
    async def event_generator():
        async with get_db_session() as session:
            cursor = await session.stream_large_dataset()
            async for row in cursor:
                # No active check for socket termination during heavy processing
                await asyncio.sleep(1)
                yield f"data: {row.json()}\n\n"

    return StreamingResponse(event_generator(), media_type="text/event-stream")
```

#### Production Standard (Correct)
> **Standard**: Uses an AnyIO TaskGroup with an explicit memory object stream. A dedicated task monitors `request.is_disconnected()` and cancels the scope immediately upon TCP drop, instantly halting the producer task and deterministic context managers to release database cursors and pool connections.
```python
import anyio
from fastapi import APIRouter, Request
from fastapi.responses import StreamingResponse
from db import get_db_session

router = APIRouter()

@router.get("/events")
async def stream_events(request: Request):
    send_channel, receive_channel = anyio.create_memory_object_stream(max_buffer_size=10)

    async def monitor_disconnect(cancel_scope: anyio.CancelScope):
        while not cancel_scope.cancel_called:
            if await request.is_disconnected():
                cancel_scope.cancel()
                break
            await anyio.sleep(0.5)

    async def produce_events(cancel_scope: anyio.CancelScope):
        async with send_channel:
            try:
                async with get_db_session() as session:
                    async with session.stream_large_dataset() as cursor:
                        async for row in cursor:
                            if cancel_scope.cancel_called:
                                break
                            await send_channel.send(f"data: {row.json()}\n\n")
                            await anyio.sleep(1)
            except anyio.get_cancelled_exc_class():
                # Downstream async database cursor instantly closed upon cancellation
                raise

    async def stream_orchestrator():
        async with anyio.create_task_group() as tg:
            tg.start_soon(monitor_disconnect, tg.cancel_scope)
            tg.start_soon(produce_events, tg.cancel_scope)

            async with receive_channel:
                async for message in receive_channel:
                    yield message
            tg.cancel_scope.cancel()

    return StreamingResponse(stream_orchestrator(), media_type="text/event-stream")
```

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 AnyIO SSE TaskGroup Structured Concurrency & Socket Disconnect Guardrail

FastAPI AnyIO SSE TaskGroup Structured Concurrency & Socket Disconnect Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict structured concurrency using AnyIO task groups and cancel scopes for all FastAPI Server-Sent Events (SSE) and long-lived streaming endpoints. Mandates bidirectional client-disconnect listening and immediate cancellation of upstream database cursors, LLM inference streams, and background workers upon socket termination to prevent resource exhaustion and connection leaks. 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 Streaming Tasks and Unbounded Database Cursor Leaks on Silent SSE Disconnects 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 Streaming Tasks and Unbounded Database Cursor Leaks on Silent SSE DisconnectsCritical
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 monitor `request.is_disconnected()` in an independent, concurrent task within an `anyio.create_task_group()` to signal downstream cancel scopes immediately upon client disconnect.

Engineers MUST ALWAYS monitor `request.is_disconnected()` in an independent, concurrent task within an `anyio.create_task_group()` to signal downstream cancel scopes immediately upon client disconnect.

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 use unshielded infinite generators or native `asyncio.create_task()` inside SSE endpoints without explicit lifecycle ownership tied to the streaming HTTP connection.

Engineers MUST NOT use unshielded infinite generators or native `asyncio.create_task()` inside SSE endpoints without explicit lifecycle ownership tied to the streaming HTTP connection.

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

Streaming responses MUST wrap external downstream resources (database cursors, async Redis pub/sub iterators, LLM API client sessions) in deterministic `try...finally` or asynchronous context managers to release pool leases upon cancellation.

Streaming responses MUST wrap external downstream resources (database cursors, async Redis pub/sub iterators, LLM API client sessions) in deterministic `try...finally` or asynchronous context managers to release pool leases upon 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.
4

Services MUST yield raw string or EventSource-compliant packets using an intermediary memory object stream (`anyio.create_memory_object_stream()`) rather than executing blocking work inside the generator body.

Services MUST yield raw string or EventSource-compliant packets using an intermediary memory object stream (`anyio.create_memory_object_stream()`) rather than executing blocking work inside the generator body.

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 assume `StreamingResponse` will automatically terminate deep background coroutines if the client terminates the TCP socket without an active AnyIO cancel scope cancellation trigger.

Components NEVER assume `StreamingResponse` will automatically terminate deep background coroutines if the client terminates the TCP socket without an active AnyIO cancel scope cancellation trigger.

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 AnyIO SSE TaskGroup Structured Concurrency & Socket Disconnect Guardrail critical for AI coding assistants?

Enforces strict structured concurrency using AnyIO task groups and cancel scopes for all FastAPI Server-Sent Events (SSE) and long-lived streaming endpoints. Mandates bidirectional client-disconnect listening and immediate cancellation of upstream database cursors, LLM inference streams, and background workers upon socket termination to prevent resource exhaustion and connection leaks. 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 Streaming Tasks and Unbounded Database Cursor Leaks on Silent SSE Disconnects) 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