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

FastAPI AnyIO Structured TaskGroup Exception Barrier Standard

Enforces strict structured concurrency in FastAPI services by replacing unbounded fire-and-forget background tasks with AnyIO TaskGroups. This eliminates silent async task failures, guarantees deterministic cancellation propagation, and prevents socket descriptor and connection pool leaks under peak concurrency. Every spawned concurrent task must be explicitly bounded to a managed lifecycle scope with an aggregate exception barrier.

Prohibited Anti-Pattern: Spawning detached coroutines with asyncio.create_task or unbounded Starlette BackgroundTasks that drop unhandled exceptions and leak network sockets

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI AnyIO Structured TaskGroup Exception Barrier Standard - Enforces strict structured concurrency in FastAPI services by replacing unbounded fire-and-forget background tasks with AnyIO TaskGroups. This eliminates silent async task failures, guarantees deterministic cancellation propagation, and prevents socket descriptor and connection pool leaks under peak concurrency. Every spawned concurrent task must be explicitly bounded to a managed lifecycle scope with an aggregate exception barrier.
globs: app/**/*.py, src/**/*.py, services/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-anyio-taskgroup-exception-barrier"
---

# FastAPI AnyIO Structured TaskGroup Exception Barrier Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Spawning detached coroutines with asyncio.create_task or unbounded Starlette BackgroundTasks that drop unhandled exceptions and leak network sockets

## System Constraints
You are a Principal Software Architect enforcing zero-leak structured concurrency across FastAPI asynchronous services. You must forbid detached coroutines (such as asyncio.create_task or unchecked BackgroundTasks) and mandate AnyIO TaskGroups with explicit ExceptionGroup barriers. Ensure deterministic task lifecycles, graceful teardown, and non-leaking connection management under all failure modes.

## Strict Guidelines
- Engineers MUST ALWAYS bound concurrent operations within an 'anyio.create_task_group()' context manager or an explicitly managed service nursery.
- Engineers MUST NOT invoke 'asyncio.create_task()' or 'asyncio.ensure_future()' directly within FastAPI request scopes.
- Engineers MUST catch multi-task failures using Python 3.11+ 'except* Exception' or AnyIO's exception hierarchy to prevent unhandled exception suppression.
- Services MUST guarantee that all child tasks are cancelled and awaited before exiting the parent scope, ensuring open socket descriptors and database sessions are finalized.
- Code SHOULD inject dependencies and database sessions per task or pass explicitly bound transactional context rather than sharing mutable session instances across task group boundaries.
- Components NEVER allow client disconnects to orphan dangling long-running workers without active cancellation tokens or timeout guards.

## Bad vs Good Implementations
### Bounded TaskGroup with Exception Barrier vs Detached Fire-and-Forget Coroutine

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using asyncio.create_task in request handlers creates detached coroutines that escape FastAPI's exception boundaries. If downstream network failures occur, the task fails silently or pollutes stderr without structured logging, leaving network sockets in CLOSE_WAIT and exhausting file descriptors under load.
```python
import asyncio
import httpx
from fastapi import FastAPI, APIRouter

router = APIRouter()

async def push_audit_metric(event_id: str, payload: dict) -> None:
    async with httpx.AsyncClient() as client:
        # If this times out or fails, the exception is swallowed or printed as an unhandled warning
        await client.post("https://metrics.internal/v1/emit", json={"id": event_id, "data": payload})

@router.post("/items/{item_id}")
async def create_item(item_id: str, payload: dict):
    # CRITICAL FLAW: Fire-and-forget task. Unhandled exceptions are silent,
    # and high concurrency leaks sockets when downstream slows or crashes.
    asyncio.create_task(push_audit_metric(item_id, payload))
    return {"status": "accepted", "item_id": item_id}
```

#### Production Standard (Correct)
> **Standard**: By utilizing 'anyio.create_task_group()' combined with 'except*', all child coroutines are explicitly supervised. Sockets are deterministically closed on exit, timeouts propagate cancellation immediately, and errors cannot escape silently or leak uncollected system resources.
```python
import logging
import anyio
import httpx
from fastapi import FastAPI, APIRouter, HTTPException, status

logger = logging.getLogger(__name__)
router = APIRouter()

async def push_audit_metric(client: httpx.AsyncClient, event_id: str, payload: dict) -> None:
    with anyio.fail_after(2.0):
        response = await client.post("https://metrics.internal/v1/emit", json={"id": event_id, "data": payload})
        response.raise_for_status()

@router.post("/items/{item_id}")
async def create_item(item_id: str, payload: dict):
    # Structured Concurrency: Task lifecycle is strictly bounded by the task group.
    try:
        async with httpx.AsyncClient() as client:
            async with anyio.create_task_group() as tg:
                tg.start_soon(push_audit_metric, client, item_id, payload)
                # Additional concurrent bounded tasks can be spawned here
    except* httpx.HTTPError as eg:
        logger.error("Telemetry delivery failed within bounded task group: %s", eg.exceptions)
        # Exception is bounded; we can choose to record fallback without leaking sockets
    except* anyio.TimeoutError:
        logger.warning("Telemetry dispatch timed out; child task cancelled deterministically")
    except* Exception as eg:
        logger.critical("Fatal unexpected error in task group: %s", eg.exceptions)
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Processing failed")

    return {"status": "accepted", "item_id": item_id}
```

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 Structured TaskGroup Exception Barrier Standard

FastAPI AnyIO Structured TaskGroup Exception Barrier Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict structured concurrency in FastAPI services by replacing unbounded fire-and-forget background tasks with AnyIO TaskGroups. This eliminates silent async task failures, guarantees deterministic cancellation propagation, and prevents socket descriptor and connection pool leaks under peak concurrency. Every spawned concurrent task must be explicitly bounded to a managed lifecycle scope with an aggregate exception barrier. 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: Spawning detached coroutines with asyncio.create_task or unbounded Starlette BackgroundTasks that drop unhandled exceptions and leak network sockets 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 DriftSpawning detached coroutines with asyncio.create_task or unbounded Starlette BackgroundTasks that drop unhandled exceptions and leak network socketsCritical
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 bound concurrent operations within an 'anyio.create_task_group()' context manager or an explicitly managed service nursery.

Engineers MUST ALWAYS bound concurrent operations within an 'anyio.create_task_group()' context manager or an explicitly managed service nursery.

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.ensure_future()' directly within FastAPI request scopes.

Engineers MUST NOT invoke 'asyncio.create_task()' or 'asyncio.ensure_future()' directly within FastAPI request scopes.

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 catch multi-task failures using Python 3.11+ 'except* Exception' or AnyIO's exception hierarchy to prevent unhandled exception suppression.

Engineers MUST catch multi-task failures using Python 3.11+ 'except* Exception' or AnyIO's exception hierarchy to prevent unhandled exception suppression.

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 guarantee that all child tasks are cancelled and awaited before exiting the parent scope, ensuring open socket descriptors and database sessions are finalized.

Services MUST guarantee that all child tasks are cancelled and awaited before exiting the parent scope, ensuring open socket descriptors and database sessions are finalized.

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 SHOULD inject dependencies and database sessions per task or pass explicitly bound transactional context rather than sharing mutable session instances across task group boundaries.

Code SHOULD inject dependencies and database sessions per task or pass explicitly bound transactional context rather than sharing mutable session instances across task group 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.
6

Components NEVER allow client disconnects to orphan dangling long-running workers without active cancellation tokens or timeout guards.

Components NEVER allow client disconnects to orphan dangling long-running workers without active cancellation tokens or timeout guards.

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 Structured TaskGroup Exception Barrier Standard critical for AI coding assistants?

Enforces strict structured concurrency in FastAPI services by replacing unbounded fire-and-forget background tasks with AnyIO TaskGroups. This eliminates silent async task failures, guarantees deterministic cancellation propagation, and prevents socket descriptor and connection pool leaks under peak concurrency. Every spawned concurrent task must be explicitly bounded to a managed lifecycle scope with an aggregate exception barrier. 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 (Spawning detached coroutines with asyncio.create_task or unbounded Starlette BackgroundTasks that drop unhandled exceptions and leak network sockets) 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