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

FastAPI Python 3.13 Free-Threaded Shared-Memory Concurrency Fence

Enforces strict memory fence, atomic synchronization, and immutability invariants for FastAPI services deployed on free-threaded Python 3.13 (PEP 703). Prevents silent data races, torn reads, and state corruption across OS worker threads running without the Global Interpreter Lock (GIL).

Prohibited Anti-Pattern: Unsynchronized in-memory state mutations and naked dictionary read-modify-writes across free-threaded worker pools.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Python 3.13 Free-Threaded Shared-Memory Concurrency Fence - Enforces strict memory fence, atomic synchronization, and immutability invariants for FastAPI services deployed on free-threaded Python 3.13 (PEP 703). Prevents silent data races, torn reads, and state corruption across OS worker threads running without the Global Interpreter Lock (GIL).
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-freethreaded-concurrency-fence"
---

# FastAPI Python 3.13 Free-Threaded Shared-Memory Concurrency Fence

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized in-memory state mutations and naked dictionary read-modify-writes across free-threaded worker pools.

## System Constraints
You are a Principal Software Architect enforcing thread safety for Python 3.13 free-threaded (PEP 703 / nogil) runtimes in FastAPI applications. In free-threaded Python, standard assumptions about CPython GIL-mediated atomic operations (such as compound operations on dicts, sets, and lists) are completely void. You must strictly mandate thread-safe synchronization fences, prevent mutable module-level state, isolate app.state mutations behind re-entrant locks or thread-safe atomic references, and ensure all thread-pool dispatches via anyio or asyncio run inside verified critical sections.

## Strict Guidelines
- Engineers MUST ALWAYS protect shared in-memory state (caches, counters, registries, app.state) using explicit synchronization primitives (`threading.Lock`, `threading.RLock`) or thread-safe concurrent data structures.
- Engineers MUST NOT rely on CPython GIL atomicity assumptions; compound operations like `dict[k] = dict.get(k, 0) + 1` or check-then-act sequences (`if k not in d: d[k] = v`) are non-atomic and race-prone in free-threaded mode.
- Code SHOULD prefer immutable data structures (such as `types.MappingProxyType`, frozen dataclasses, or Pydantic models with `frozen=True`) for all shared application contexts.
- Services MUST route background tasks and synchronous offloaded workers (`asyncio.to_thread`, `anyio.to_thread.run_sync`, Starlette sync endpoints) through thread-isolated boundaries or lock-guarded pipelines.
- Components NEVER store request-scoped or mutable transaction state in global or module-level variables; `contextvars.ContextVar` MUST be used for request-isolated context.

## Bad vs Good Implementations
### Module-Level Shared State Protection in Free-Threaded Python 3.13

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Python 3.13 free-threaded builds, Starlette sync routes run on concurrent OS worker threads without the GIL. Compound check-then-act operations on standard mutable dicts trigger race conditions, resulting in lost updates and inconsistent read-after-write state.
```python
from fastapi import FastAPI, Request

app = FastAPI()

# VIOLATION: Naked mutable dictionary shared across un-GIL'd threads
# In Python 3.13t, concurrent writes to this dict cause torn states or lost updates
metrics_cache: dict[str, int] = {}

@app.post("/track/{event_id}")
def track_event(event_id: str):
    # Sync route executes on worker thread pool (anyio.to_thread)
    # Race condition: Read-modify-write is not atomic without the GIL
    current = metrics_cache.get(event_id, 0)
    metrics_cache[event_id] = current + 1
    return {"status": "ok", "count": metrics_cache[event_id]}
```

#### Production Standard (Correct)
> **Standard**: Encapsulates the shared memory boundary inside an atomic concurrency fence using `threading.Lock()`, guaranteeing safe read-modify-write semantics across un-GIL'd worker threads in Python 3.13 free-threaded runtimes.
```python
from fastapi import FastAPI
from threading import Lock
from typing import Final

app = FastAPI()

class AtomicMetricRegistry:
    def __init__(self) -> None:
        self._lock: Final[Lock] = Lock()
        self._store: dict[str, int] = {}

    def increment(self, key: str, delta: int = 1) -> int:
        with self._lock:
            val = self._store.get(key, 0) + delta
            self._store[key] = val
            return val

    def get(self, key: str) -> int:
        with self._lock:
            return self._store.get(key, 0)

registry: Final[AtomicMetricRegistry] = AtomicMetricRegistry()

@app.post("/track/{event_id}")
def track_event(event_id: str):
    # Thread-safe fence ensures atomic read-modify-write across un-GIL'd worker threads
    new_count = registry.increment(event_id, 1)
    return {"status": "ok", "count": new_count}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Python 3.13 Free-Threaded Shared-Memory Concurrency Fence

FastAPI Python 3.13 Free-Threaded Shared-Memory Concurrency Fence defines an industrial-grade behavioral standard for backend engineering. Enforces strict memory fence, atomic synchronization, and immutability invariants for FastAPI services deployed on free-threaded Python 3.13 (PEP 703). Prevents silent data races, torn reads, and state corruption across OS worker threads running without the Global Interpreter Lock (GIL). 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 in-memory state mutations and naked dictionary read-modify-writes across free-threaded worker pools. 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 in-memory state mutations and naked dictionary read-modify-writes across free-threaded worker pools.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 protect shared in-memory state (caches, counters, registries, app.state) using explicit synchronization primitives (`threading.Lock`, `threading.RLock`) or thread-safe concurrent data structures.

Engineers MUST ALWAYS protect shared in-memory state (caches, counters, registries, app.state) using explicit synchronization primitives (`threading.Lock`, `threading.RLock`) or thread-safe concurrent data structures.

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 rely on CPython GIL atomicity assumptions

Engineers MUST NOT rely on CPython GIL atomicity assumptions; compound operations like `dict[k] = dict.get(k, 0) + 1` or check-then-act sequences (`if k not in d: d[k] = v`) are non-atomic and race-prone in free-threaded mode.

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 SHOULD prefer immutable data structures (such as `types.MappingProxyType`, frozen dataclasses, or Pydantic models with `frozen=True`) for all shared application contexts.

Code SHOULD prefer immutable data structures (such as `types.MappingProxyType`, frozen dataclasses, or Pydantic models with `frozen=True`) for all shared application contexts.

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 route background tasks and synchronous offloaded workers (`asyncio.to_thread`, `anyio.to_thread.run_sync`, Starlette sync endpoints) through thread-isolated boundaries or lock-guarded pipelines.

Services MUST route background tasks and synchronous offloaded workers (`asyncio.to_thread`, `anyio.to_thread.run_sync`, Starlette sync endpoints) through thread-isolated boundaries or lock-guarded pipelines.

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 or mutable transaction state in global or module-level variables

Components NEVER store request-scoped or mutable transaction state in global or module-level variables; `contextvars.ContextVar` MUST be used for request-isolated context.

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 Shared-Memory Concurrency Fence critical for AI coding assistants?

Enforces strict memory fence, atomic synchronization, and immutability invariants for FastAPI services deployed on free-threaded Python 3.13 (PEP 703). Prevents silent data races, torn reads, and state corruption across OS worker threads running without the Global Interpreter Lock (GIL). 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 in-memory state mutations and naked dictionary read-modify-writes across free-threaded worker pools.) 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