This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: fastapi-anyio-bounded-zerocopy-streaming

FastAPI AnyIO Concurrency-Bounded Zero-Copy Memory Streaming

Enforces strict chunked streaming of binary payloads using AnyIO concurrency primitives and zero-copy buffer protocols in FastAPI endpoints. Prevents catastrophic asyncio event-loop starvation and OOM crashes caused by reading unbounded request bodies into heap memory or saturating the default thread pool with blocking disk I/O under high concurrent load.

Prohibited Anti-Pattern: Unbounded `await file.read()` and Unthrottled Disk I/O Ingestion in Asynchronous Endpoints

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI AnyIO Concurrency-Bounded Zero-Copy Memory Streaming - Enforces strict chunked streaming of binary payloads using AnyIO concurrency primitives and zero-copy buffer protocols in FastAPI endpoints. Prevents catastrophic asyncio event-loop starvation and OOM crashes caused by reading unbounded request bodies into heap memory or saturating the default thread pool with blocking disk I/O under high concurrent load.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-anyio-bounded-zerocopy-streaming"
---

# FastAPI AnyIO Concurrency-Bounded Zero-Copy Memory Streaming

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded `await file.read()` and Unthrottled Disk I/O Ingestion in Asynchronous Endpoints

## System Constraints
You are a Principal Engineer enforcing FastAPI AnyIO Concurrency-Bounded Zero-Copy Memory Streaming. Mandate strict chunk-based iteration over ASGI byte streams, backpressure enforcement via bounded AnyIO semaphores, and asynchronous zero-copy buffer passing to avoid event-loop blocking and unmetered heap amplification.

## Strict Guidelines
- Engineers MUST ALWAYS iterate over request byte streams using chunked iterators (`request.stream()` or `UploadFile.read(chunk_size)`) bounded by fixed-size pre-allocated memory buffers (e.g., 64 KiB to 1 MiB).
- Engineers MUST NOT call `UploadFile.read()` or `Request.body()` without an explicit byte limit, as reading unbounded multi-gigabyte payloads directly into Python heap memory triggers immediate Out-Of-Memory (OOM) fatal kills.
- Engineers MUST bound concurrent binary ingestions using an `anyio.CapacityLimiter` or `anyio.Semaphore` to prevent uncontrolled disk thrashing and thread-pool exhaustion.
- Engineers MUST offload file disk I/O to asynchronous streams via `anyio.open_file` instead of standard blocking synchronous Python file operations (`open()`, `os.write()`).
- Services MUST enforce strict stream cancellation handlers using `anyio.create_task_group()` to clean up dangling file descriptors and temp allocations upon client disconnect.

## Bad vs Good Implementations
### Ingesting Large Binary Payloads via AnyIO Bounded Stream

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: `await file.read()` loads the entire file into the Python process heap, causing immediate OOM kills when multiple large files are uploaded concurrently. Furthermore, `open()` and `f.write()` block the async event loop, halting all incoming request handling across the entire server worker.
```python
from fastapi import FastAPI, UploadFile, File
import shutil

app = FastAPI()

@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
    # CRITICAL BUG: Reads entire payload into RAM in one shot
    content = await file.read()
    # CRITICAL BUG: Blocking synchronous file write in async loop
    with open(f"/tmp/{file.filename}", "wb") as f:
        f.write(content)
    return {"size": len(content)}
```

#### Production Standard (Correct)
> **Standard**: Iterates over the ASGI stream in small chunks using `request.stream()`, writing directly to disk via non-blocking `anyio.open_file`. Employs `anyio.CapacityLimiter` to constrain concurrent disk I/O and catches AnyIO cancellation to ensure partial files are cleanly unlinked if the client drops connection.
```python
from fastapi import FastAPI, Request, HTTPException, status
import anyio
from anyio import open_file
from pathlib import Path

app = FastAPI()
CHUNK_SIZE = 64 * 1024  # 64 KiB bounded chunks
GLOBAL_UPLOAD_LIMITER = anyio.CapacityLimiter(10)  # Max 10 concurrent disk stream writes

@app.post("/upload/{filename}")
async def upload_file(filename: str, request: Request):
    target_path = Path("/tmp") / Path(filename).name
    bytes_written = 0
    
    try:
        async with GLOBAL_UPLOAD_LIMITER:
            async with await open_file(target_path, "wb") as out_file:
                async for chunk in request.stream():
                    if not chunk:
                        continue
                    await out_file.write(chunk)
                    bytes_written += len(chunk)
    except anyio.get_cancelled_exc_class():
        if target_path.exists():
            target_path.unlink(missing_ok=True)
        raise
    except Exception as exc:
        if target_path.exists():
            target_path.unlink(missing_ok=True)
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc))
        
    return {"status": "success", "bytes_written": bytes_written}
```

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 Concurrency-Bounded Zero-Copy Memory Streaming

FastAPI AnyIO Concurrency-Bounded Zero-Copy Memory Streaming defines an industrial-grade behavioral standard for backend engineering. Enforces strict chunked streaming of binary payloads using AnyIO concurrency primitives and zero-copy buffer protocols in FastAPI endpoints. Prevents catastrophic asyncio event-loop starvation and OOM crashes caused by reading unbounded request bodies into heap memory or saturating the default thread pool with blocking disk I/O under high concurrent load. 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: Unbounded `await file.read()` and Unthrottled Disk I/O Ingestion in Asynchronous Endpoints 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 DriftUnbounded `await file.read()` and Unthrottled Disk I/O Ingestion in Asynchronous EndpointsCritical
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 iterate over request byte streams using chunked iterators (`request.stream()` or `UploadFile.read(chunk_size)`) bounded by fixed-size pre-allocated memory buffers (e.g., 64 KiB to 1 MiB).

Engineers MUST ALWAYS iterate over request byte streams using chunked iterators (`request.stream()` or `UploadFile.read(chunk_size)`) bounded by fixed-size pre-allocated memory buffers (e.g., 64 KiB to 1 MiB).

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 call `UploadFile.read()` or `Request.body()` without an explicit byte limit, as reading unbounded multi-gigabyte payloads directly into Python heap memory triggers immediate Out-Of-Memory (OOM) fatal kills.

Engineers MUST NOT call `UploadFile.read()` or `Request.body()` without an explicit byte limit, as reading unbounded multi-gigabyte payloads directly into Python heap memory triggers immediate Out-Of-Memory (OOM) fatal kills.

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 bound concurrent binary ingestions using an `anyio.CapacityLimiter` or `anyio.Semaphore` to prevent uncontrolled disk thrashing and thread-pool exhaustion.

Engineers MUST bound concurrent binary ingestions using an `anyio.CapacityLimiter` or `anyio.Semaphore` to prevent uncontrolled disk thrashing and thread-pool exhaustion.

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 offload file disk I/O to asynchronous streams via `anyio.open_file` instead of standard blocking synchronous Python file operations (`open()`, `os.write()`).

Engineers MUST offload file disk I/O to asynchronous streams via `anyio.open_file` instead of standard blocking synchronous Python file operations (`open()`, `os.write()`).

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

Services MUST enforce strict stream cancellation handlers using `anyio.create_task_group()` to clean up dangling file descriptors and temp allocations upon client disconnect.

Services MUST enforce strict stream cancellation handlers using `anyio.create_task_group()` to clean up dangling file descriptors and temp allocations 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.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is FastAPI AnyIO Concurrency-Bounded Zero-Copy Memory Streaming critical for AI coding assistants?

Enforces strict chunked streaming of binary payloads using AnyIO concurrency primitives and zero-copy buffer protocols in FastAPI endpoints. Prevents catastrophic asyncio event-loop starvation and OOM crashes caused by reading unbounded request bodies into heap memory or saturating the default thread pool with blocking disk I/O under high concurrent load. 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 (Unbounded `await file.read()` and Unthrottled Disk I/O Ingestion in Asynchronous Endpoints) 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