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

FastAPI 0.115+ AnyIO Structured Concurrency and TaskGroup Isolation

Enforces strict structured concurrency in FastAPI 0.115+ via AnyIO TaskGroups to ensure deterministic cancellation trees and bounded resource lifecycles. Forbids unmanaged asyncio.create_task and legacy BackgroundTasks for core business operations, completely eliminating dangling child coroutines, silent task drop, and unhandled nursery exceptions.

Prohibited Anti-Pattern: Fire-and-Forget Coroutine Spawning via asyncio.create_task and Unbound BackgroundTasks

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI 0.115+ AnyIO Structured Concurrency and TaskGroup Isolation - Enforces strict structured concurrency in FastAPI 0.115+ via AnyIO TaskGroups to ensure deterministic cancellation trees and bounded resource lifecycles. Forbids unmanaged asyncio.create_task and legacy BackgroundTasks for core business operations, completely eliminating dangling child coroutines, silent task drop, and unhandled nursery exceptions.
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-structured-concurrency-guardrail"
---

# FastAPI 0.115+ AnyIO Structured Concurrency and TaskGroup Isolation

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Fire-and-Forget Coroutine Spawning via asyncio.create_task and Unbound BackgroundTasks

## System Constraints
You are a Principal Backend Engineer enforcing structured concurrency in FastAPI 0.115+ using AnyIO 4+. Mandate that all asynchronous operations run within bounded cancellation scopes and explicit TaskGroups. Prohibit fire-and-forget task scheduling (e.g., raw asyncio.create_task or Starlette BackgroundTasks for critical flows) that escape request boundaries or mask task failures.

## Strict Guidelines
- Engineers MUST ALWAYS use `anyio.create_task_group()` context managers to manage concurrently running coroutines within a request lifecycle.
- Engineers MUST NOT invoke `asyncio.create_task()`, `asyncio.ensure_future()`, or `loop.create_task()` directly, as they bypass cancellation propagation and orphan tasks upon client disconnect.
- Engineers MUST NOT rely on Starlette `BackgroundTasks` for mission-critical operations; durable message brokers (e.g., Celery, ARQ, Temporal) or monitored application-level nurseries MUST be used instead.
- Code MUST handle `anyio.get_cancelled_exc_class()` explicitly or allow it to propagate without swallowing cancellation signals during HTTP client aborts.
- Code MUST use `anyio.fail_after()` or `anyio.move_on_after()` instead of `asyncio.wait_for()` to enforce timeout bounds while preserving parent cancellation trees.

## Bad vs Good Implementations
### Concurrent Fan-Out and Aggregation in API Handlers

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using asyncio.create_task and asyncio.gather leaves orphaned coroutines running if the HTTP request is aborted by the client or if one task raises an exception, causing resource leaks and unbounded execution.
```python
import asyncio
from fastapi import APIRouter, HTTPException

router = APIRouter()

async def fetch_metrics(service_id: str) -> dict:
    await asyncio.sleep(0.1)
    return {"service": service_id, "status": "ok"}

@router.get("/services/{service_id}/status")
async def get_service_status(service_id: str):
    # CRITICAL: Bypasses cancellation tree; orphaned on client disconnect
    task1 = asyncio.create_task(fetch_metrics(f"{service_id}-primary"))
    task2 = asyncio.create_task(fetch_metrics(f"{service_id}-replica"))
    
    try:
        results = await asyncio.gather(task1, task2)
    except Exception as exc:
        # task1 or task2 may continue executing in background if one fails
        raise HTTPException(status_code=500, detail=str(exc))
    
    return {"results": results}
```

#### Production Standard (Correct)
> **Standard**: Using anyio.create_task_group ensures strict nursery lifecycle: if the client disconnects or one task fails, all sibling tasks inside the TaskGroup are deterministically cancelled immediately.
```python
import anyio
from fastapi import APIRouter, HTTPException

router = APIRouter()

async def fetch_metrics(service_id: str, results: list, index: int) -> None:
    await anyio.sleep(0.1)
    results[index] = {"service": service_id, "status": "ok"}

@router.get("/services/{service_id}/status")
async def get_service_status(service_id: str):
    results = [None, None]
    try:
        async with anyio.create_task_group() as tg:
            tg.start_soon(fetch_metrics, f"{service_id}-primary", results, 0)
            tg.start_soon(fetch_metrics, f"{service_id}-replica", results, 1)
    except ExceptionGroup as eg:
        raise HTTPException(status_code=500, detail=f"Upstream failure: {eg.exceptions}")
    
    return {"results": results}
```

### Bounded Timeout and Cancellation Scopes

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: asyncio.wait_for cancels the inner task via Task.cancel(), but subtle race conditions in asyncio can leave inner work running or swallow parent cancellation scopes.
```python
import asyncio
from fastapi import APIRouter, HTTPException

router = APIRouter()

@router.post("/process")
async def process_payload(data: dict):
    try:
        # CRITICAL: asyncio.wait_for can cause race conditions during cancellation
        result = await asyncio.wait_for(asyncio.sleep(5), timeout=2.0)
    except asyncio.TimeoutError:
        raise HTTPException(status_code=504, detail="Gateway Timeout")
    return {"status": "completed"}
```

#### Production Standard (Correct)
> **Standard**: anyio.fail_after uses an AnyIO CancelScope which cleanly integrates with AnyIO structured task trees, ensuring that timeout deadlines cancel children synchronously and predictably.
```python
import anyio
from fastapi import APIRouter, HTTPException

router = APIRouter()

@router.post("/process")
async def process_payload(data: dict):
    try:
        with anyio.fail_after(2.0):
            await anyio.sleep(5)
    except* TimeoutError:
        raise HTTPException(status_code=504, detail="Gateway Timeout")
    return {"status": "completed"}
```

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 0.115+ AnyIO Structured Concurrency and TaskGroup Isolation

FastAPI 0.115+ AnyIO Structured Concurrency and TaskGroup Isolation defines an industrial-grade behavioral standard for backend engineering. Enforces strict structured concurrency in FastAPI 0.115+ via AnyIO TaskGroups to ensure deterministic cancellation trees and bounded resource lifecycles. Forbids unmanaged asyncio.create_task and legacy BackgroundTasks for core business operations, completely eliminating dangling child coroutines, silent task drop, and unhandled nursery exceptions. 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: Fire-and-Forget Coroutine Spawning via asyncio.create_task and Unbound BackgroundTasks 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 DriftFire-and-Forget Coroutine Spawning via asyncio.create_task and Unbound BackgroundTasksCritical
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 use `anyio.create_task_group()` context managers to manage concurrently running coroutines within a request lifecycle.

Engineers MUST ALWAYS use `anyio.create_task_group()` context managers to manage concurrently running coroutines within a request 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.
2

Engineers MUST NOT invoke `asyncio.create_task()`, `asyncio.ensure_future()`, or `loop.create_task()` directly, as they bypass cancellation propagation and orphan tasks upon client disconnect.

Engineers MUST NOT invoke `asyncio.create_task()`, `asyncio.ensure_future()`, or `loop.create_task()` directly, as they bypass cancellation propagation and orphan tasks 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.
3

Engineers MUST NOT rely on Starlette `BackgroundTasks` for mission-critical operations

Engineers MUST NOT rely on Starlette `BackgroundTasks` for mission-critical operations; durable message brokers (e.g., Celery, ARQ, Temporal) or monitored application-level nurseries MUST be used instead.

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

Code MUST handle `anyio.get_cancelled_exc_class()` explicitly or allow it to propagate without swallowing cancellation signals during HTTP client aborts.

Code MUST handle `anyio.get_cancelled_exc_class()` explicitly or allow it to propagate without swallowing cancellation signals during HTTP 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.
5

Code MUST use `anyio.fail_after()` or `anyio.move_on_after()` instead of `asyncio.wait_for()` to enforce timeout bounds while preserving parent cancellation trees.

Code MUST use `anyio.fail_after()` or `anyio.move_on_after()` instead of `asyncio.wait_for()` to enforce timeout bounds while preserving parent cancellation trees.

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 0.115+ AnyIO Structured Concurrency and TaskGroup Isolation critical for AI coding assistants?

Enforces strict structured concurrency in FastAPI 0.115+ via AnyIO TaskGroups to ensure deterministic cancellation trees and bounded resource lifecycles. Forbids unmanaged asyncio.create_task and legacy BackgroundTasks for core business operations, completely eliminating dangling child coroutines, silent task drop, and unhandled nursery exceptions. 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 (Fire-and-Forget Coroutine Spawning via asyncio.create_task and Unbound BackgroundTasks) 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