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

Python 3.13 Free-Threaded Shared Memory Fence and Concurrency Guardrails for FastAPI

Enforces strict memory isolation, explicit synchronization fences, and lock-free concurrency primitives when running FastAPI under Python 3.13 free-threaded mode (PEP 703 / nogil). Without the Global Interpreter Lock, shared mutable state across asynchronous event loops, thread pools, and subinterpreters will cause race conditions, heap corruption, and phantom writes during high-throughput I/O.

Prohibited Anti-Pattern: Unsynchronized Mutable In-Memory State and Naked Cross-Thread Object Sharing in Free-Threaded (nogil) Runtimes

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Python 3.13 Free-Threaded Shared Memory Fence and Concurrency Guardrails for FastAPI - Enforces strict memory isolation, explicit synchronization fences, and lock-free concurrency primitives when running FastAPI under Python 3.13 free-threaded mode (PEP 703 / nogil). Without the Global Interpreter Lock, shared mutable state across asynchronous event loops, thread pools, and subinterpreters will cause race conditions, heap corruption, and phantom writes during high-throughput I/O.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-python313-freethreaded-memory-fence"
---

# Python 3.13 Free-Threaded Shared Memory Fence and Concurrency Guardrails for FastAPI

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Mutable In-Memory State and Naked Cross-Thread Object Sharing in Free-Threaded (nogil) Runtimes

## System Constraints
You are a Principal Python Architect specializing in Python 3.13 free-threaded (nogil) concurrency and low-level runtime safety for FastAPI. You must enforce explicit thread-safe object fences, copy-on-write immutability, thread-safe synchronization (such as threading.Lock, atomics, or cross-process/subinterpreter shared-memory abstractions), and prevent naked in-memory mutable state mutations across concurrent worker threads and subinterpreters.

## Strict Guidelines
- Engineers MUST ALWAYS treat built-in Python collections (dict, list, set) as non-thread-safe when mutated across concurrent threads in Python 3.13t (free-threaded).
- Engineers MUST NOT rely on implicit GIL atomicity for shared dictionary insertions, counter increments (`+=`), or cache lookups.
- Code MUST employ explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or lock-free transactional wrappers) or utilize immutable data structures (`types.MappingProxyType`, frozen dataclasses) for any state shared across worker threads.
- Services MUST isolate cross-interpreter and cross-thread pipeline state using thread-safe channels (`queue.Queue`, multiprocessing shared memory with synchronization) rather than direct reference sharing.
- Components NEVER store request-scoped mutable references in application-level singletons or global module-level variables without `contextvars.ContextVar` fences.

## Bad vs Good Implementations
### Thread-Safe Memory Fence for Application State in Python 3.13 Free-Threaded FastAPI

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Python 3.13 free-threaded mode (nogil), operations on standard dictionaries and integer increments lose implicit bytecode-level atomicity previously guaranteed by the GIL. Concurrent request threads executing simultaneously will produce race conditions, lost updates, and potential internal structure corruption in collection objects.
```python
from fastapi import FastAPI

app = FastAPI()

# ANTI-PATTERN: In Python 3.13t (free-threaded), built-in dicts and bare integer increments
# are NOT protected by a GIL. Concurrent async workers or anyio threads will cause
# race conditions, torn writes, or crash under high-throughput I/O.
metrics_cache: dict[str, int] = {"total_requests": 0}

@app.post("/track/{event_id}")
async def track_event(event_id: str):
    # Naked increment: Non-atomic in PEP 703
    metrics_cache["total_requests"] += 1
    metrics_cache[event_id] = metrics_cache.get(event_id, 0) + 1
    return {"status": "ok", "count": metrics_cache[event_id]}
```

#### Production Standard (Correct)
> **Standard**: The good example encapsulates shared state inside a dedicated thread-safe class equipped with an explicit `threading.Lock` fence and memory barrier. It exposes safe mutation boundaries and returns immutable read views (`MappingProxyType`), preventing data races in Python 3.13 free-threaded environments.
```python
from fastapi import FastAPI
from threading import Lock
from dataclasses import dataclass, field
from types import MappingProxyType

app = FastAPI()

@dataclass(slots=True)
class ThreadSafeMetricsRegistry:
    _lock: Lock = field(default_factory=Lock)
    _counts: dict[str, int] = field(default_factory=dict)
    _total_requests: int = 0

    def record(self, event_id: str) -> int:
        # Synchronized memory fence ensuring thread safety across Python 3.13 subinterpreters/threads
        with self._lock:
            self._total_requests += 1
            new_count = self._counts.get(event_id, 0) + 1
            self._counts[event_id] = new_count
            return new_count

    def snapshot(self) -> MappingProxyType[str, int]:
        with self._lock:
            # Return immutable copy-on-write view to prevent external mutation
            return MappingProxyType(dict(self._counts))

registry = ThreadSafeMetricsRegistry()

@app.post("/track/{event_id}")
async def track_event(event_id: str):
    event_count = registry.record(event_id)
    return {"status": "ok", "count": event_count}
```

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: Python 3.13 Free-Threaded Shared Memory Fence and Concurrency Guardrails for FastAPI

Python 3.13 Free-Threaded Shared Memory Fence and Concurrency Guardrails for FastAPI defines an industrial-grade behavioral standard for backend engineering. Enforces strict memory isolation, explicit synchronization fences, and lock-free concurrency primitives when running FastAPI under Python 3.13 free-threaded mode (PEP 703 / nogil). Without the Global Interpreter Lock, shared mutable state across asynchronous event loops, thread pools, and subinterpreters will cause race conditions, heap corruption, and phantom writes during high-throughput I/O. 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 Mutable In-Memory State and Naked Cross-Thread Object Sharing in Free-Threaded (nogil) 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 DriftUnsynchronized Mutable In-Memory State and Naked Cross-Thread Object Sharing in Free-Threaded (nogil) 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 treat built-in Python collections (dict, list, set) as non-thread-safe when mutated across concurrent threads in Python 3.13t (free-threaded).

Engineers MUST ALWAYS treat built-in Python collections (dict, list, set) as non-thread-safe when mutated across concurrent threads in Python 3.13t (free-threaded).

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 atomicity for shared dictionary insertions, counter increments (`+=`), or cache lookups.

Engineers MUST NOT rely on implicit GIL atomicity for shared dictionary insertions, counter increments (`+=`), or cache lookups.

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 employ explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or lock-free transactional wrappers) or utilize immutable data structures (`types.MappingProxyType`, frozen dataclasses) for any state shared across worker threads.

Code MUST employ explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or lock-free transactional wrappers) or utilize immutable data structures (`types.MappingProxyType`, frozen dataclasses) for any state shared across worker 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.
4

Services MUST isolate cross-interpreter and cross-thread pipeline state using thread-safe channels (`queue.Queue`, multiprocessing shared memory with synchronization) rather than direct reference sharing.

Services MUST isolate cross-interpreter and cross-thread pipeline state using thread-safe channels (`queue.Queue`, multiprocessing shared memory with synchronization) rather than direct reference sharing.

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 mutable references in application-level singletons or global module-level variables without `contextvars.ContextVar` fences.

Components NEVER store request-scoped mutable references in application-level singletons or global module-level variables without `contextvars.ContextVar` fences.

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 Python 3.13 Free-Threaded Shared Memory Fence and Concurrency Guardrails for FastAPI critical for AI coding assistants?

Enforces strict memory isolation, explicit synchronization fences, and lock-free concurrency primitives when running FastAPI under Python 3.13 free-threaded mode (PEP 703 / nogil). Without the Global Interpreter Lock, shared mutable state across asynchronous event loops, thread pools, and subinterpreters will cause race conditions, heap corruption, and phantom writes during high-throughput I/O. 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 Mutable In-Memory State and Naked Cross-Thread Object Sharing in Free-Threaded (nogil) 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

Python