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

FastAPI 0.115 & Python 3.13t Free-Threaded Memory Safety & Concurrency Primitives

Enforces thread-safe memory fencing, synchronization primitives, and immutable access patterns for FastAPI 0.115+ applications running on Python 3.13 free-threaded (GIL-less) runtimes. Prevents data races, torn reads, and memory corruption across parallel worker threads executing high-throughput ML inference pipelines.

Prohibited Anti-Pattern: Unsynchronized Shared In-Memory State and Compound Mutex-Free Updates Across GIL-less Worker Threads

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI 0.115 & Python 3.13t Free-Threaded Memory Safety & Concurrency Primitives - Enforces thread-safe memory fencing, synchronization primitives, and immutable access patterns for FastAPI 0.115+ applications running on Python 3.13 free-threaded (GIL-less) runtimes. Prevents data races, torn reads, and memory corruption across parallel worker threads executing high-throughput ML inference pipelines.
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-guardrail"
---

# FastAPI 0.115 & Python 3.13t Free-Threaded Memory Safety & Concurrency Primitives

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Shared In-Memory State and Compound Mutex-Free Updates Across GIL-less Worker Threads

## System Constraints
You are a Principal Systems Architect enforcing memory safety and thread synchronization across FastAPI 0.115 applications hosted on Python 3.13 free-threaded runtimes (python3.13t / PEP 703). In this environment, the Global Interpreter Lock (GIL) is disabled: bytecodes execute truly concurrently on distinct CPU cores. You must mandate strict thread-safe memory primitives, forbid naive module-level mutable singletons, eliminate compound dictionary mutation races, and guarantee deterministic shared-state synchronization for ML inference workers.

## Strict Guidelines
- Engineers MUST ALWAYS protect shared mutable state (e.g., token caches, dynamic batching queues, metrics registries) using explicit synchronization primitives such as 'threading.Lock', 'threading.RLock', or synchronized thread-safe wrappers.
- Engineers MUST NOT rely on historical GIL assumptions where compound built-in operations (such as read-then-write 'if key not in dict:', in-place list extensions, or attribute swaps) were pseudo-atomic.
- Code SHOULD enforce immutability across thread boundaries using 'dataclasses.dataclass(frozen=True)' or read-only views for ML model configurations, weights references, and tokenizer pipelines.
- Services MUST inject shared thread-safe services via FastAPI Lifespan state ('app.state') or formal dependency injection instead of raw global module variables.
- Components NEVER execute raw unsynchronized batching or deque mutations across parallel worker threads spawned via 'anyio.to_thread.run_sync' or Starlette's threadpool.
- ML Inference routines MUST isolate execution contexts or ensure inference sessions (e.g., ONNX Runtime, TorchScript) are compiled with multi-thread concurrency support and protected against concurrent execution state re-entry.

## Bad vs Good Implementations
### Thread-Safe ML Inference Cache & Worker Protection in Python 3.13t

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Under Python 3.13 free-threaded mode (PEP 703), the absence of the GIL means threads truly run in parallel. A compound 'if key not in cache: cache[key] = val' causes data races and duplicate inference executions. Incrementing 'TOTAL_PROCESSED' without an atomic primitive causes lost updates and memory fence inconsistencies.
```python
from fastapi import FastAPI, Depends
from typing import Dict, Any
import time

app = FastAPI()

# ANTI-PATTERN: Global mutable dictionary and counter running on Python 3.13t (No-GIL).
# Multiple concurrent threads will hit race conditions, torn reads, and internal cache corruption
# during compound check-and-set operations.
GLOBAL_INFERENCE_CACHE: Dict[str, Any] = {}
TOTAL_PROCESSED: int = 0

def run_cpu_bound_inference(prompt: str) -> str:
    # Simulated inference
    return f"pred:{prompt[::-1]}"

@app.post("/predict")
def predict(payload: dict):
    global TOTAL_PROCESSED
    prompt = payload.get("prompt", "")
    
    # Race Condition 1: Non-atomic compound check and mutation
    if prompt not in GLOBAL_INFERENCE_CACHE:
        result = run_cpu_bound_inference(prompt)
        # Race condition on dict resize / write under free-threading
        GLOBAL_INFERENCE_CACHE[prompt] = result
    else:
        result = GLOBAL_INFERENCE_CACHE[prompt]
    
    # Race Condition 2: Unfenced counter increment (lost updates / memory inconsistency)
    TOTAL_PROCESSED += 1
    
    return {"result": result, "total": TOTAL_PROCESSED}
```

#### Production Standard (Correct)
> **Standard**: The good implementation uses double-checked locking patterns with 'threading.Lock' to guard shared dictionary updates and counters. State is held in FastAPI's lifespan state instead of module globals, ensuring determinism across GIL-less worker threads in Python 3.13t.
```python
from fastapi import FastAPI, Request, Depends
from contextlib import asynccontextmanager
from dataclasses import dataclass, field
import threading
from typing import Dict, Any, Optional

@dataclass(frozen=True)
class InferenceConfig:
    model_version: str
    max_tokens: int

class ThreadSafeInferenceEngine:
    def __init__(self, config: InferenceConfig):
        self._config = config
        self._cache: Dict[str, str] = {}
        self._cache_lock = threading.Lock()
        self._counter_lock = threading.Lock()
        self._total_processed: int = 0

    def execute(self, prompt: str) -> tuple[str, int]:
        # Double-checked locking pattern for memory safety and low contention
        with self._cache_lock:
            cached = self._cache.get(prompt)
            if cached is not None:
                with self._counter_lock:
                    self._total_processed += 1
                    total = self._total_processed
                return cached, total

        # Heavy compute executed outside lock
        result = f"pred:{prompt[::-1]}"

        with self._cache_lock:
            self._cache[prompt] = result
            
        with self._counter_lock:
            self._total_processed += 1
            total = self._total_processed

        return result, total

@asynccontextmanager
async def lifespan(app: FastAPI):
    config = InferenceConfig(model_version="v1.4-ft", max_tokens=512)
    engine = ThreadSafeInferenceEngine(config=config)
    app.state.engine = engine
    yield

app = FastAPI(lifespan=lifespan)

def get_engine(request: Request) -> ThreadSafeInferenceEngine:
    return request.app.state.engine

@app.post("/predict")
def predict(payload: dict, engine: ThreadSafeInferenceEngine = Depends(get_engine)):
    prompt = payload.get("prompt", "")
    result, total = engine.execute(prompt)
    return {"result": result, "total": total}
```

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: FastAPI 0.115 & Python 3.13t Free-Threaded Memory Safety & Concurrency Primitives

FastAPI 0.115 & Python 3.13t Free-Threaded Memory Safety & Concurrency Primitives defines an industrial-grade behavioral standard for backend engineering. Enforces thread-safe memory fencing, synchronization primitives, and immutable access patterns for FastAPI 0.115+ applications running on Python 3.13 free-threaded (GIL-less) runtimes. Prevents data races, torn reads, and memory corruption across parallel worker threads executing high-throughput ML inference pipelines. 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 Shared In-Memory State and Compound Mutex-Free Updates Across GIL-less Worker Threads 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 Shared In-Memory State and Compound Mutex-Free Updates Across GIL-less Worker ThreadsCritical
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 mutable state (e.g., token caches, dynamic batching queues, metrics registries) using explicit synchronization primitives such as 'threading.Lock', 'threading.RLock', or synchronized thread-safe wrappers.

Engineers MUST ALWAYS protect shared mutable state (e.g., token caches, dynamic batching queues, metrics registries) using explicit synchronization primitives such as 'threading.Lock', 'threading.RLock', or synchronized thread-safe wrappers.

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 historical GIL assumptions where compound built-in operations (such as read-then-write 'if key not in dict

Engineers MUST NOT rely on historical GIL assumptions where compound built-in operations (such as read-then-write 'if key not in dict:', in-place list extensions, or attribute swaps) were pseudo-atomic.

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 across thread boundaries using 'dataclasses.dataclass(frozen=True)' or read-only views for ML model configurations, weights references, and tokenizer pipelines.

Code SHOULD enforce immutability across thread boundaries using 'dataclasses.dataclass(frozen=True)' or read-only views for ML model configurations, weights references, and tokenizer 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.
4

Services MUST inject shared thread-safe services via FastAPI Lifespan state ('app.state') or formal dependency injection instead of raw global module variables.

Services MUST inject shared thread-safe services via FastAPI Lifespan state ('app.state') or formal dependency injection instead of raw global module variables.

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 execute raw unsynchronized batching or deque mutations across parallel worker threads spawned via 'anyio.to_thread.run_sync' or Starlette's threadpool.

Components NEVER execute raw unsynchronized batching or deque mutations across parallel worker threads spawned via 'anyio.to_thread.run_sync' or Starlette's threadpool.

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.
6

ML Inference routines MUST isolate execution contexts or ensure inference sessions (e.g., ONNX Runtime, TorchScript) are compiled with multi-thread concurrency support and protected against concurrent execution state re-entry.

ML Inference routines MUST isolate execution contexts or ensure inference sessions (e.g., ONNX Runtime, TorchScript) are compiled with multi-thread concurrency support and protected against concurrent execution state re-entry.

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 0.115 & Python 3.13t Free-Threaded Memory Safety & Concurrency Primitives critical for AI coding assistants?

Enforces thread-safe memory fencing, synchronization primitives, and immutable access patterns for FastAPI 0.115+ applications running on Python 3.13 free-threaded (GIL-less) runtimes. Prevents data races, torn reads, and memory corruption across parallel worker threads executing high-throughput ML inference pipelines. 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 Shared In-Memory State and Compound Mutex-Free Updates Across GIL-less Worker Threads) 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