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

FastAPI AnyIO TaskGroup Structured Concurrency & Deterministic Cancellation Standard

Enforces strict structured concurrency in FastAPI backends using AnyIO TaskGroups to prevent orphaned coroutines, resource exhaustion, and unhandled exception swallowing. Unhandled child errors must cascade deterministic cancellation to all siblings within the failure domain and cleanly unwind via shielded cancellation scopes.

Prohibited Anti-Pattern: Unmanaged asyncio.create_task Fire-and-Forget Pipelines Causing Zombie Coroutines and Silent Exception Swallowing

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI AnyIO TaskGroup Structured Concurrency & Deterministic Cancellation Standard - Enforces strict structured concurrency in FastAPI backends using AnyIO TaskGroups to prevent orphaned coroutines, resource exhaustion, and unhandled exception swallowing. Unhandled child errors must cascade deterministic cancellation to all siblings within the failure domain and cleanly unwind via shielded cancellation scopes.
globs: app/**/*.py, src/**/*.py, routers/**/*.py, services/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-anyio-taskgroup-structured-concurrency-protocol"
---

# FastAPI AnyIO TaskGroup Structured Concurrency & Deterministic Cancellation Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unmanaged asyncio.create_task Fire-and-Forget Pipelines Causing Zombie Coroutines and Silent Exception Swallowing

## System Constraints
You are a Principal Backend Architect enforcing structured concurrency invariants in FastAPI applications powered by AnyIO. You mandate deterministic async task lifecycles: no raw asyncio.create_task invocations, mandatory TaskGroup boundaries for fan-out execution, explicit exception group unwinding, and shielded cancel scopes for non-abortable resource teardown.

## Strict Guidelines
- Engineers MUST ALWAYS encapsulate concurrent worker fan-outs inside an 'anyio.create_task_group()' context manager to ensure parent-child lifecycle coupling.
- Engineers MUST NOT invoke 'asyncio.create_task' or 'asyncio.gather(..., return_exceptions=True)' for background request processing, as they leak coroutines across request boundaries.
- Code MUST use 'with anyio.CancelScope(shield=True):' exclusively when executing critical teardown logic (e.g., socket release, transaction rollback) within cancellation handlers.
- Services MUST capture and handle 'ExceptionGroup' (or use PEP 654 'except*') when catching failures from task group exits to avoid masking sibling diagnostic traces.
- Components NEVER catch 'BaseException' or AnyIO cancellation exceptions without immediately re-raising, ensuring cancellation propagation is never suppressed.

## Bad vs Good Implementations
### Unstructured Task Spawning vs AnyIO Structured TaskGroup Teardown

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using asyncio.create_task or unbounded asyncio.gather leaves pending background coroutines orphaned when a sibling raises an error. The runtime leaks memory, active sockets, and database connections while dropping exception traces.
```python
import asyncio
from fastapi import APIRouter, HTTPException

router = APIRouter()

async def process_item(item_id: str):
    if item_id == "bad":
        raise ValueError("Processing failed")
    await asyncio.sleep(2)

@router.post("/pipeline/bad")
async def trigger_pipeline(items: list[str]):
    # ANTI-PATTERN: Raw asyncio.create_task creates unmanaged, dangling tasks.
    # If one task fails, the other runs forever; exceptions are dropped or logged as unretrievable.
    tasks = [asyncio.create_task(process_item(item)) for item in items]
    try:
        results = await asyncio.gather(*tasks)
        return {"status": "ok", "results": results}
    except Exception as exc:
        # Leaks pending tasks in 'tasks' without cancellation!
        raise HTTPException(status_code=500, detail=str(exc))
```

#### Production Standard (Correct)
> **Standard**: anyio.create_task_group guarantees that any unhandled child exception immediately signals cancellation to all sibling tasks. Cleanups are protected with shielded CancelScopes, and PEP 654 exception group filtering isolates failures deterministically.
```python
import anyio
from fastapi import APIRouter, HTTPException, status

router = APIRouter()

async def process_item(item_id: str, cancel_scope: anyio.CancelScope) -> str:
    try:
        if item_id == "bad":
            raise ValueError(f"Invalid item payload: {item_id}")
        await anyio.sleep(2)
        return f"processed_{item_id}"
    except anyio.get_cancelled_exc_class():
        # Execute atomic resource cleanup under a shielded scope
        with anyio.CancelScope(shield=True):
            # Teardown logic guaranteed to run to completion despite parent cancellation
            pass
        raise

@router.post("/pipeline/good")
async def trigger_pipeline(items: list[str]):
    results: list[str] = []
    
    try:
        async with anyio.create_task_group() as tg:
            for item in items:
                tg.start_soon(process_item, item, tg.cancel_scope)
    except* ValueError as eg:
        # PEP 654 ExceptionGroup handling guarantees clean propagation and introspection
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=f"Batch processing aborted: {eg.exceptions[0]}"
        ) from eg
        
    return {"status": "ok", "processed_count": len(items)}
```

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 TaskGroup Structured Concurrency & Deterministic Cancellation Standard

FastAPI AnyIO TaskGroup Structured Concurrency & Deterministic Cancellation Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict structured concurrency in FastAPI backends using AnyIO TaskGroups to prevent orphaned coroutines, resource exhaustion, and unhandled exception swallowing. Unhandled child errors must cascade deterministic cancellation to all siblings within the failure domain and cleanly unwind via shielded cancellation scopes. 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: Unmanaged asyncio.create_task Fire-and-Forget Pipelines Causing Zombie Coroutines and Silent Exception Swallowing 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 DriftUnmanaged asyncio.create_task Fire-and-Forget Pipelines Causing Zombie Coroutines and Silent Exception SwallowingCritical
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 encapsulate concurrent worker fan-outs inside an 'anyio.create_task_group()' context manager to ensure parent-child lifecycle coupling.

Engineers MUST ALWAYS encapsulate concurrent worker fan-outs inside an 'anyio.create_task_group()' context manager to ensure parent-child lifecycle coupling.

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' or 'asyncio.gather(..., return_exceptions=True)' for background request processing, as they leak coroutines across request boundaries.

Engineers MUST NOT invoke 'asyncio.create_task' or 'asyncio.gather(..., return_exceptions=True)' for background request processing, as they leak coroutines across request boundaries.

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 MUST use 'with anyio.CancelScope(shield=True)

Code MUST use 'with anyio.CancelScope(shield=True):' exclusively when executing critical teardown logic (e.g., socket release, transaction rollback) within cancellation handlers.

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 capture and handle 'ExceptionGroup' (or use PEP 654 'except*') when catching failures from task group exits to avoid masking sibling diagnostic traces.

Services MUST capture and handle 'ExceptionGroup' (or use PEP 654 'except*') when catching failures from task group exits to avoid masking sibling diagnostic traces.

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 catch 'BaseException' or AnyIO cancellation exceptions without immediately re-raising, ensuring cancellation propagation is never suppressed.

Components NEVER catch 'BaseException' or AnyIO cancellation exceptions without immediately re-raising, ensuring cancellation propagation is never suppressed.

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 TaskGroup Structured Concurrency & Deterministic Cancellation Standard critical for AI coding assistants?

Enforces strict structured concurrency in FastAPI backends using AnyIO TaskGroups to prevent orphaned coroutines, resource exhaustion, and unhandled exception swallowing. Unhandled child errors must cascade deterministic cancellation to all siblings within the failure domain and cleanly unwind via shielded cancellation scopes. 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 (Unmanaged asyncio.create_task Fire-and-Forget Pipelines Causing Zombie Coroutines and Silent Exception Swallowing) 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