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

FastAPI Async TaskGroup Structured Concurrency & Exception Isolation Boundary

Enforces strict structured concurrency in FastAPI asynchronous operations using Python 3.11+ asyncio.TaskGroup instead of raw asyncio.create_task or fire-and-forget background execution. Ensures that any unhandled exception immediately cancels sibling child tasks, isolates failures to the request lifecycle boundary, and prevents event loop corruption or orphaned coroutines leaking memory and resources.

Prohibited Anti-Pattern: Unbounded fire-and-forget asyncio.create_task leaking orphaned coroutines and suppressing unhandled ExceptionGroups.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Async TaskGroup Structured Concurrency & Exception Isolation Boundary - Enforces strict structured concurrency in FastAPI asynchronous operations using Python 3.11+ asyncio.TaskGroup instead of raw asyncio.create_task or fire-and-forget background execution. Ensures that any unhandled exception immediately cancels sibling child tasks, isolates failures to the request lifecycle boundary, and prevents event loop corruption or orphaned coroutines leaking memory and resources.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-taskgroup-structured-concurrency-boundary"
---

# FastAPI Async TaskGroup Structured Concurrency & Exception Isolation Boundary

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded fire-and-forget asyncio.create_task leaking orphaned coroutines and suppressing unhandled ExceptionGroups.

## System Constraints
You are a Principal Backend Architect enforcing structured concurrency and exception isolation invariants in FastAPI services. Forbid raw asyncio.create_task and asyncio.gather without strict cancellation semantics. Enforce Python 3.11+ asyncio.TaskGroup with explicit ExceptionGroup unwrapping or Starlette BackgroundTasks for fire-and-forget telemetry.

## Strict Guidelines
- Engineers MUST ALWAYS encapsulate concurrent child coroutines inside an 'async with asyncio.TaskGroup() as tg:' context block.
- Engineers MUST NOT spawn detached coroutines using 'asyncio.create_task()' inside request handlers without retaining explicit task references and attaching cancellation hooks.
- Services MUST catch and inspect 'ExceptionGroup' (or BaseExceptionGroup) explicitly when handling TaskGroup failures to log all underlying errors individually.
- Engineers MUST NOT use 'asyncio.gather(..., return_exceptions=True)' as a substitute for structured concurrency when failure in any single dependency invalidates the entire composite operation.
- Components NEVER leak background workloads beyond the HTTP request-response boundary unless explicitly managed through Starlette BackgroundTasks or an external durable queue (e.g., Celery, ARQ, Temporal).

## Bad vs Good Implementations
### FastAPI Concurrent Fan-Out: Raw asyncio.create_task vs. asyncio.TaskGroup Boundary

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: If t2 fails while t1 is awaiting, an unhandled exception will raise prematurely or orphan t1, leaving it running in the event loop. Furthermore, unhandled exceptions in raw tasks can escape without proper cancellation, exhausting connection pools and causing silent state leakage.
```python
import asyncio
from fastapi import APIRouter, HTTPException

router = APIRouter()

async def fetch_user_data(user_id: str) -> dict:
    await asyncio.sleep(0.05)
    return {"id": user_id}

async def fetch_billing_data(user_id: str) -> dict:
    await asyncio.sleep(0.02)
    raise ConnectionResetError("Billing service unreachable")

@router.get("/users/{user_id}/dashboard")
async def get_dashboard(user_id: str):
    # ANTI-PATTERN: Detached create_task calls orphan sibling tasks on failure,
    # leak memory, and suppress exceptions until garbage collection.
    t1 = asyncio.create_task(fetch_user_data(user_id))
    t2 = asyncio.create_task(fetch_billing_data(user_id))
    
    # If fetch_billing_data fails, fetch_user_data continues running unmonitored
    user_res = await t1
    billing_res = await t2
    return {"user": user_res, "billing": billing_res}
```

#### Production Standard (Correct)
> **Standard**: Using Python 3.11's asyncio.TaskGroup establishes a deterministic scope boundary. If fetch_billing_data raises ConnectionResetError, fetch_user_data is immediately cancelled, preventing orphaned coroutines and connection pool leakage. The except* syntax cleanly unwraps the resulting ExceptionGroup.
```python
import asyncio
import logging
from fastapi import APIRouter, HTTPException, status

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

async def fetch_user_data(user_id: str) -> dict:
    await asyncio.sleep(0.05)
    return {"id": user_id}

async def fetch_billing_data(user_id: str) -> dict:
    await asyncio.sleep(0.02)
    raise ConnectionResetError("Billing service unreachable")

@router.get("/users/{user_id}/dashboard")
async def get_dashboard(user_id: str):
    user_res: dict | None = None
    billing_res: dict | None = None

    async def _safe_user():
        nonlocal user_res
        user_res = await fetch_user_data(user_id)

    async def _safe_billing():
        nonlocal billing_res
        billing_res = await fetch_billing_data(user_id)

    try:
        # INVARIANT: TaskGroup guarantees fail-fast cancellation of all child tasks
        # and deterministic resource cleanup at scope exit.
        async with asyncio.TaskGroup() as tg:
            tg.create_task(_safe_user())
            tg.create_task(_safe_billing())
    except* ConnectionResetError as eg:
        logger.error("Upstream dependency failed during dashboard fan-out: %s", eg.exceptions)
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="Billing service temporarily unavailable"
        )
    except* Exception as eg:
        logger.critical("Unexpected concurrent failure: %s", eg.exceptions)
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Internal server error during data aggregation"
        )

    return {"user": user_res, "billing": billing_res}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Async TaskGroup Structured Concurrency & Exception Isolation Boundary

FastAPI Async TaskGroup Structured Concurrency & Exception Isolation Boundary defines an industrial-grade behavioral standard for backend engineering. Enforces strict structured concurrency in FastAPI asynchronous operations using Python 3.11+ asyncio.TaskGroup instead of raw asyncio.create_task or fire-and-forget background execution. Ensures that any unhandled exception immediately cancels sibling child tasks, isolates failures to the request lifecycle boundary, and prevents event loop corruption or orphaned coroutines leaking memory and resources. 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 fire-and-forget asyncio.create_task leaking orphaned coroutines and suppressing unhandled ExceptionGroups. 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 fire-and-forget asyncio.create_task leaking orphaned coroutines and suppressing unhandled ExceptionGroups.Critical
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 child coroutines inside an 'async with asyncio.TaskGroup() as tg

Engineers MUST ALWAYS encapsulate concurrent child coroutines inside an 'async with asyncio.TaskGroup() as tg:' context block.

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 spawn detached coroutines using 'asyncio.create_task()' inside request handlers without retaining explicit task references and attaching cancellation hooks.

Engineers MUST NOT spawn detached coroutines using 'asyncio.create_task()' inside request handlers without retaining explicit task references and attaching cancellation hooks.

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

Services MUST catch and inspect 'ExceptionGroup' (or BaseExceptionGroup) explicitly when handling TaskGroup failures to log all underlying errors individually.

Services MUST catch and inspect 'ExceptionGroup' (or BaseExceptionGroup) explicitly when handling TaskGroup failures to log all underlying errors individually.

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 NOT use 'asyncio.gather(..., return_exceptions=True)' as a substitute for structured concurrency when failure in any single dependency invalidates the entire composite operation.

Engineers MUST NOT use 'asyncio.gather(..., return_exceptions=True)' as a substitute for structured concurrency when failure in any single dependency invalidates the entire composite operation.

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 leak background workloads beyond the HTTP request-response boundary unless explicitly managed through Starlette BackgroundTasks or an external durable queue (e.g., Celery, ARQ, Temporal).

Components NEVER leak background workloads beyond the HTTP request-response boundary unless explicitly managed through Starlette BackgroundTasks or an external durable queue (e.g., Celery, ARQ, Temporal).

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 Async TaskGroup Structured Concurrency & Exception Isolation Boundary critical for AI coding assistants?

Enforces strict structured concurrency in FastAPI asynchronous operations using Python 3.11+ asyncio.TaskGroup instead of raw asyncio.create_task or fire-and-forget background execution. Ensures that any unhandled exception immediately cancels sibling child tasks, isolates failures to the request lifecycle boundary, and prevents event loop corruption or orphaned coroutines leaking memory and resources. 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 fire-and-forget asyncio.create_task leaking orphaned coroutines and suppressing unhandled ExceptionGroups.) 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