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

FastAPI Python 3.13 Free-Threaded Shared-State Memory Fence

Enforces strict synchronization barriers, atomic primitives, and thread-safe dependency lifetimes in FastAPI applications deployed on free-threaded Python 3.13 (PEP 703). Prevents torn reads, check-then-act race conditions, and cross-thread memory corruption caused by the removal of the Global Interpreter Lock.

Prohibited Anti-Pattern: Naked Global Mutation and Unsynchronized Shared State in No-GIL Runtimes

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Python 3.13 Free-Threaded Shared-State Memory Fence - Enforces strict synchronization barriers, atomic primitives, and thread-safe dependency lifetimes in FastAPI applications deployed on free-threaded Python 3.13 (PEP 703). Prevents torn reads, check-then-act race conditions, and cross-thread memory corruption caused by the removal of the Global Interpreter Lock.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-py313-freethreaded-memory-fence"
---

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

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Naked Global Mutation and Unsynchronized Shared State in No-GIL Runtimes

## System Constraints
You are a Principal Systems Architect specializing in Python 3.13 free-threaded runtimes (PEP 703) and ASGI servers (Granian, Uvicorn multi-thread). Mandate atomic memory barriers and explicit synchronization fences on all shared FastAPI application dependencies and app.state access. Never assume dictionary mutations or composite object updates are atomic in no-GIL mode. Eliminate shared mutable state across thread boundaries, enforce frozen value objects for context transfers, and require strict lock hierarchy to prevent thread starvation and deadlocks.

## Strict Guidelines
- Engineers MUST ALWAYS guard mutable shared state in `app.state` or module-level singletons with explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or atomic concurrent collections).
- Engineers MUST NOT rely on implicit GIL-protected atomicity for dict operations, list appends, or in-place numeric mutations (`+=`) in free-threaded mode.
- Code SHOULD enforce immutability for request-spanning context objects using frozen dataclasses (`@dataclass(frozen=True, slots=True)`).
- Services MUST implement explicit acquire/release timeouts on all lock acquisitions to avert deadlocks across competing OS threads.
- Components NEVER expose direct references to mutable internal buffers across dependency-injected boundaries without returning defensive copies or read-only views.

## Bad vs Good Implementations
### Thread-Safe Memory Fence for Shared State in Python 3.13

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Python 3.13 free-threaded builds (`python3.13t`), the removal of the GIL allows true concurrent execution across native threads. In-place dictionary lookups and assignments are vulnerable to race conditions, leading to lost updates and corrupted internal pointer states during concurrent resizing.
```python
from fastapi import FastAPI, Request

app = FastAPI()
app.state.rate_limits = {}

@app.post("/increment/{client_id}")
async def increment_counter(client_id: str):
    # CRITICAL: In free-threaded Python 3.13 (no-GIL), this read-modify-write
    # sequence causes torn reads, lost updates, and memory race conditions.
    current = app.state.rate_limits.get(client_id, 0)
    app.state.rate_limits[client_id] = current + 1
    return {"client_id": client_id, "count": app.state.rate_limits[client_id]}
```

#### Production Standard (Correct)
> **Standard**: Wraps mutable state in an encapsulated container with a `threading.Lock` fence, strict timeout limits to prevent sub-interpreter deadlocks, and slot-optimized storage ensuring safe concurrent execution on no-GIL Python 3.13.
```python
from dataclasses import dataclass
import threading
from typing import Dict
from fastapi import FastAPI, HTTPException, status

@dataclass(slots=True)
class ThreadSafeCounter:
    _lock: threading.Lock = threading.Lock()
    _counts: Dict[str, int] = None

    def __post_init__(self):
        self._counts = {}

    def increment(self, client_id: str, timeout: float = 1.0) -> int:
        # Memory barrier: Explicit lock guarantees mutual exclusion across OS threads
        acquired = self._lock.acquire(timeout=timeout)
        if not acquired:
            raise TimeoutError("Lock acquisition timed out under thread contention")
        try:
            val = self._counts.get(client_id, 0) + 1
            self._counts[client_id] = val
            return val
        finally:
            self._lock.release()

app = FastAPI()
app.state.counter = ThreadSafeCounter()

@app.post("/increment/{client_id}")
async def increment_counter(client_id: str):
    try:
        new_count = app.state.counter.increment(client_id)
        return {"client_id": client_id, "count": new_count}
    except* TimeoutError:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="Service busy: Lock contention detected"
        )
```

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 Shared-State Memory Fence

FastAPI Python 3.13 Free-Threaded Shared-State Memory Fence defines an industrial-grade behavioral standard for backend engineering. Enforces strict synchronization barriers, atomic primitives, and thread-safe dependency lifetimes in FastAPI applications deployed on free-threaded Python 3.13 (PEP 703). Prevents torn reads, check-then-act race conditions, and cross-thread memory corruption caused by the removal of the Global Interpreter Lock. 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: Naked Global Mutation and Unsynchronized Shared State in No-GIL Runtimes 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 DriftNaked Global Mutation and Unsynchronized Shared State in No-GIL RuntimesCritical
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 guard mutable shared state in `app.state` or module-level singletons with explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or atomic concurrent collections).

Engineers MUST ALWAYS guard mutable shared state in `app.state` or module-level singletons with explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or atomic concurrent collections).

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 implicit GIL-protected atomicity for dict operations, list appends, or in-place numeric mutations (`+=`) in free-threaded mode.

Engineers MUST NOT rely on implicit GIL-protected atomicity for dict operations, list appends, or in-place numeric mutations (`+=`) 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 enforce immutability for request-spanning context objects using frozen dataclasses (`@dataclass(frozen=True, slots=True)`).

Code SHOULD enforce immutability for request-spanning context objects using frozen dataclasses (`@dataclass(frozen=True, slots=True)`).

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 implement explicit acquire/release timeouts on all lock acquisitions to avert deadlocks across competing OS threads.

Services MUST implement explicit acquire/release timeouts on all lock acquisitions to avert deadlocks across competing OS threads.

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 expose direct references to mutable internal buffers across dependency-injected boundaries without returning defensive copies or read-only views.

Components NEVER expose direct references to mutable internal buffers across dependency-injected boundaries without returning defensive copies or read-only views.

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

Enforces strict synchronization barriers, atomic primitives, and thread-safe dependency lifetimes in FastAPI applications deployed on free-threaded Python 3.13 (PEP 703). Prevents torn reads, check-then-act race conditions, and cross-thread memory corruption caused by the removal of the Global Interpreter Lock. 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 (Naked Global Mutation and Unsynchronized Shared State in No-GIL Runtimes) 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