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

FastAPI Python 3.13 Free-Threaded Concurrency Boundary Standard

Enforces strict memory isolation, atomic updates, and thread-safe shared state primitives within FastAPI services compiled and executed under Python 3.13 free-threaded (nogil) runtimes. Eliminates shared mutable state corruption, race conditions in sub-millisecond ASGI pipelines, and thread-unsafe caching layers. Mandates immutable data structures, concurrent-safe primitives, and thread-local state management across concurrent request workers.

Prohibited Anti-Pattern: Unsynchronized Global State Mutation in Python 3.13 Free-Threaded ASGI Request Pipelines

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Python 3.13 Free-Threaded Concurrency Boundary Standard - Enforces strict memory isolation, atomic updates, and thread-safe shared state primitives within FastAPI services compiled and executed under Python 3.13 free-threaded (nogil) runtimes. Eliminates shared mutable state corruption, race conditions in sub-millisecond ASGI pipelines, and thread-unsafe caching layers. Mandates immutable data structures, concurrent-safe primitives, and thread-local state management across concurrent request workers.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-python-313-nogil-concurrency-boundary"
---

# FastAPI Python 3.13 Free-Threaded Concurrency Boundary Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Global State Mutation in Python 3.13 Free-Threaded ASGI Request Pipelines

## System Constraints
You are a Principal Software Architect enforcing Python 3.13 free-threaded (nogil) concurrency safety in FastAPI applications. In Python 3.13t, bytecode execution occurs concurrently across OS threads without the Global Interpreter Lock (GIL). You must prohibit un-synchronized in-memory mutable state, naive global dictionaries/sets, race-prone singleton caches, and thread-unsafe I/O pool accesses. Enforce structural immutability, thread-safe collections, synchronization primitives (threading.Lock, RWLock), or external centralized state stores.

## Strict Guidelines
- Engineers MUST NEVER mutate raw module-level or application-level collections (dict, list, set) concurrently across ASGI request handlers in free-threaded runtimes.
- Engineers MUST synchronize access to in-process shared state using explicit synchronization primitives (`threading.Lock`, `threading.RLock`) or lock-free atomic abstractions.
- Code MUST use frozen Pydantic models (`ConfigDict(frozen=True)`) or immutable `dataclasses` (`@dataclass(frozen=True)`) for payload and domain models exchanged across thread boundaries.
- Services MUST instantiate database connection pools, HTTP clients, and background execution queues using explicitly thread-safe connection managers verified for nogil environments.
- Components NEVER store request-scoped data in global variables or static class attributes; request context MUST reside strictly in ASGI scope, function parameters, or `contextvars.ContextVar`.

## Bad vs Good Implementations
### Module-Level Shared Cache Mutation vs Thread-Safe Locked Cache

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Without the GIL in Python 3.13, concurrent threads executing ASGI requests simultaneously read and write to SHARED_METRICS_CACHE. Non-atomic reads and writes produce race conditions, data loss, and undefined internal hashtable state.
```python
from fastapi import FastAPI
from typing import Dict, Any

app = FastAPI()
# CRITICAL BUG: Under Python 3.13 nogil, concurrent OS threads mutate
# this raw dictionary simultaneously, causing memory corruption or lost updates.
SHARED_METRICS_CACHE: Dict[str, int] = {}

@app.post("/track/{metric_name}")
async def track_metric(metric_name: str) -> dict[str, int]:
    current_count = SHARED_METRICS_CACHE.get(metric_name, 0)
    # Race condition: non-atomic read-modify-write without GIL protection
    SHARED_METRICS_CACHE[metric_name] = current_count + 1
    return {"metric": metric_name, "count": SHARED_METRICS_CACHE[metric_name]}
```

#### Production Standard (Correct)
> **Standard**: Encapsulates the state mutation behind an explicit `threading.Lock` boundary. Under Python 3.13 nogil, this guarantees mutual exclusion during read-modify-write operations, preserving invariant consistency across OS threads.
```python
from fastapi import FastAPI
import threading
from typing import Dict

app = FastAPI()

class ConcurrentMetricsStore:
    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._metrics: Dict[str, int] = {}

    def increment(self, metric_name: str) -> int:
        with self._lock:
            new_count = self._metrics.get(metric_name, 0) + 1
            self._metrics[metric_name] = new_count
            return new_count

metrics_store = ConcurrentMetricsStore()

@app.post("/track/{metric_name}")
async def track_metric(metric_name: str) -> dict[str, int]:
    count = metrics_store.increment(metric_name)
    return {"metric": metric_name, "count": count}
```

### Thread-Unsafe Singleton Session vs Thread-Local / ContextVar Safe Access

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: A mutable global class attribute is shared across all concurrent threads. Under free-threading, requests concurrently overwrite `context.tenant_id`, resulting in data leakage between separate tenants.
```python
from fastapi import FastAPI, Depends, Request

app = FastAPI()

# Anti-pattern: Global state mutated by ASGI middleware or dependencies
class GlobalRequestContext:
    tenant_id: str | None = None

context = GlobalRequestContext()

@app.middleware("http")
async def set_tenant_context(request: Request, call_next):
    # Multiple threads clobber this attribute concurrently across requests
    context.tenant_id = request.headers.get("X-Tenant-ID")
    return await call_next(request)

@app.get("/tenant-info")
def get_tenant_info():
    return {"tenant": context.tenant_id}
```

#### Production Standard (Correct)
> **Standard**: Uses `contextvars.ContextVar`, which is natively isolated across asynchronous execution contexts and OS threads in Python 3.13, ensuring zero cross-tenant state leakage under nogil workloads.
```python
from fastapi import FastAPI, Request
import contextvars

app = FastAPI()

# Production fix: ContextVar guarantees thread-local and task-isolated state
TENANT_CONTEXT: contextvars.ContextVar[str | None] = contextvars.ContextVar(
    "tenant_context", default=None
)

@app.middleware("http")
async def set_tenant_context(request: Request, call_next):
    token = TENANT_CONTEXT.set(request.headers.get("X-Tenant-ID"))
    try:
        return await call_next(request)
    finally:
        TENANT_CONTEXT.reset(token)

@app.get("/tenant-info")
def get_tenant_info():
    return {"tenant": TENANT_CONTEXT.get()}
```

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 Python 3.13 Free-Threaded Concurrency Boundary Standard

FastAPI Python 3.13 Free-Threaded Concurrency Boundary Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict memory isolation, atomic updates, and thread-safe shared state primitives within FastAPI services compiled and executed under Python 3.13 free-threaded (nogil) runtimes. Eliminates shared mutable state corruption, race conditions in sub-millisecond ASGI pipelines, and thread-unsafe caching layers. Mandates immutable data structures, concurrent-safe primitives, and thread-local state management across concurrent request workers. 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: Unsynchronized Global State Mutation in Python 3.13 Free-Threaded ASGI Request Pipelines 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 DriftUnsynchronized Global State Mutation in Python 3.13 Free-Threaded ASGI Request PipelinesCritical
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 NEVER mutate raw module-level or application-level collections (dict, list, set) concurrently across ASGI request handlers in free-threaded runtimes.

Engineers MUST NEVER mutate raw module-level or application-level collections (dict, list, set) concurrently across ASGI request handlers in free-threaded runtimes.

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 synchronize access to in-process shared state using explicit synchronization primitives (`threading.Lock`, `threading.RLock`) or lock-free atomic abstractions.

Engineers MUST synchronize access to in-process shared state using explicit synchronization primitives (`threading.Lock`, `threading.RLock`) or lock-free atomic abstractions.

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 frozen Pydantic models (`ConfigDict(frozen=True)`) or immutable `dataclasses` (`@dataclass(frozen=True)`) for payload and domain models exchanged across thread boundaries.

Code MUST use frozen Pydantic models (`ConfigDict(frozen=True)`) or immutable `dataclasses` (`@dataclass(frozen=True)`) for payload and domain models exchanged across thread 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.
4

Services MUST instantiate database connection pools, HTTP clients, and background execution queues using explicitly thread-safe connection managers verified for nogil environments.

Services MUST instantiate database connection pools, HTTP clients, and background execution queues using explicitly thread-safe connection managers verified for nogil environments.

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 store request-scoped data in global variables or static class attributes

Components NEVER store request-scoped data in global variables or static class attributes; request context MUST reside strictly in ASGI scope, function parameters, or `contextvars.ContextVar`.

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 Python 3.13 Free-Threaded Concurrency Boundary Standard critical for AI coding assistants?

Enforces strict memory isolation, atomic updates, and thread-safe shared state primitives within FastAPI services compiled and executed under Python 3.13 free-threaded (nogil) runtimes. Eliminates shared mutable state corruption, race conditions in sub-millisecond ASGI pipelines, and thread-unsafe caching layers. Mandates immutable data structures, concurrent-safe primitives, and thread-local state management across concurrent request workers. 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 (Unsynchronized Global State Mutation in Python 3.13 Free-Threaded ASGI Request Pipelines) 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