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

FastAPI Python 3.13 Free-Threaded (PEP 703) Thread-Safe State Mutations

Enforces strict synchronization and memory safety when running FastAPI under Python 3.13t free-threaded mode (GIL disabled). Eliminates implicit reliance on the Global Interpreter Lock for atomic mutations across shared in-memory caches, singletons, app.state, and unverified C-extensions executing on multicore thread pools.

Prohibited Anti-Pattern: Unsynchronized Global/Module State Mutations and Non-Thread-Safe C-Extension Execution Under Disabled GIL

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Python 3.13 Free-Threaded (PEP 703) Thread-Safe State Mutations - Enforces strict synchronization and memory safety when running FastAPI under Python 3.13t free-threaded mode (GIL disabled). Eliminates implicit reliance on the Global Interpreter Lock for atomic mutations across shared in-memory caches, singletons, app.state, and unverified C-extensions executing on multicore thread pools.
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-free-threaded-concurrency"
---

# FastAPI Python 3.13 Free-Threaded (PEP 703) Thread-Safe State Mutations

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Global/Module State Mutations and Non-Thread-Safe C-Extension Execution Under Disabled GIL

## System Constraints
You are a Principal Backend Engineer enforcing thread safety in FastAPI applications executing under Python 3.13 free-threaded runtime (python3.13t). In this environment, the Global Interpreter Lock (GIL) is disabled, meaning native Python threads execute in parallel across CPU cores. You must mandate explicit thread-synchronization primitives around all shared in-memory mutations (e.g., threading.Lock, threading.RLock), prohibit naive dict/list operations as pseudo-atomic state, enforce GIL status verification at startup, and flag non-thread-safe C-extensions.

## Strict Guidelines
- Engineers MUST ALWAYS verify the GIL status during application lifespan startup using `sys._is_gil_enabled()` and assert compatibility with loaded native modules.
- Engineers MUST NOT rely on standard Python collections (`dict`, `set`, `list`) for thread-safe concurrent reads and writes; all mutations to shared in-memory collections must be protected by explicit synchronization primitives (such as `threading.Lock` or `threading.RLock`).
- Code MUST wrap `request.app.state` mutations in explicit synchronization locks when handling concurrent worker threads executing standard `def` routes dispatched to the thread pool.
- Services MUST verify that all third-party C-extensions declare `Py_MOD_GIL_NOT_USED` before running under Python 3.13 free-threaded production instances.
- Components NEVER use global increment operators (e.g., `counter += 1`) or compound collection checks (e.g., `if key not in cache: cache[key] = val`) without mutex guards, as bytecode interleaving and core parallelization cause race conditions and memory corruption.

## Bad vs Good Implementations
### In-Memory Global Cache and State Counter in FastAPI Route Handler

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Python 3.13 free-threaded mode (PEP 703), native threads running synchronous FastAPI endpoints via AnyIO/Starlette thread pools run concurrently on multiple CPU cores without the GIL. Compound operations like `+=` and dictionary check-then-set logic suffer severe race conditions, lost updates, and potentially internal dictionary memory corruption.
```python
import sys
from fastapi import FastAPI, Request

app = FastAPI()

# Anti-pattern: Implicitly relying on GIL atomicity for dictionary and integer operations
global_stats: dict[str, int] = {"total_requests": 0}
in_memory_cache: dict[str, str] = {}

@app.post("/process/{item_id}")
def process_item(item_id: str, payload: dict, request: Request):
    # CRITICAL BUG in Python 3.13t: Parallel threads execute this simultaneously
    global_stats["total_requests"] += 1  # Race condition / non-atomic read-modify-write
    
    if item_id not in in_memory_cache:
        # Race condition: duplicate computation and corrupt read/write
        result = f"processed_{payload.get('val')}"
        in_memory_cache[item_id] = result
        return {"result": result, "count": global_stats["total_requests"]}
    
    return {"result": in_memory_cache[item_id], "count": global_stats["total_requests"]}
```

#### Production Standard (Correct)
> **Standard**: Encapsulates shared mutable state within a class guarded by a `threading.Lock()`. All mutating access to both the dictionary cache and request counter occurs inside a mutually exclusive context block, guaranteeing safety under multicore free-threaded Python 3.13. Lifespan startup explicitly inspects runtime GIL status.
```python
import sys
import threading
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, status

class SynchronizedStateStore:
    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._stats: dict[str, int] = {"total_requests": 0}
        self._cache: dict[str, str] = {}

    def record_and_cache(self, item_id: str, val: str) -> tuple[str, int]:
        with self._lock:
            self._stats["total_requests"] += 1
            count = self._stats["total_requests"]
            if item_id not in self._cache:
                self._cache[item_id] = f"processed_{val}"
            return self._cache[item_id], count

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Guardrail: Explicitly log and validate GIL execution environment
    is_gil_active = getattr(sys, "_is_gil_enabled", lambda: True)()
    if not is_gil_active:
        # Ensure all dependencies and modules safely operate in free-threaded mode
        pass
    yield

app = FastAPI(lifespan=lifespan)
state_store = SynchronizedStateStore()

@app.post("/process/{item_id}")
def process_item(item_id: str, payload: dict):
    val = payload.get("val", "default")
    result, count = state_store.record_and_cache(item_id, val)
    return {"result": result, "count": count}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Python 3.13 Free-Threaded (PEP 703) Thread-Safe State Mutations

FastAPI Python 3.13 Free-Threaded (PEP 703) Thread-Safe State Mutations defines an industrial-grade behavioral standard for backend engineering. Enforces strict synchronization and memory safety when running FastAPI under Python 3.13t free-threaded mode (GIL disabled). Eliminates implicit reliance on the Global Interpreter Lock for atomic mutations across shared in-memory caches, singletons, app.state, and unverified C-extensions executing on multicore thread pools. 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/Module State Mutations and Non-Thread-Safe C-Extension Execution Under Disabled GIL 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/Module State Mutations and Non-Thread-Safe C-Extension Execution Under Disabled GILCritical
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 verify the GIL status during application lifespan startup using `sys._is_gil_enabled()` and assert compatibility with loaded native modules.

Engineers MUST ALWAYS verify the GIL status during application lifespan startup using `sys._is_gil_enabled()` and assert compatibility with loaded native modules.

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 standard Python collections (`dict`, `set`, `list`) for thread-safe concurrent reads and writes

Engineers MUST NOT rely on standard Python collections (`dict`, `set`, `list`) for thread-safe concurrent reads and writes; all mutations to shared in-memory collections must be protected by explicit synchronization primitives (such as `threading.Lock` or `threading.RLock`).

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 wrap `request.app.state` mutations in explicit synchronization locks when handling concurrent worker threads executing standard `def` routes dispatched to the thread pool.

Code MUST wrap `request.app.state` mutations in explicit synchronization locks when handling concurrent worker threads executing standard `def` routes dispatched to the thread pool.

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 verify that all third-party C-extensions declare `Py_MOD_GIL_NOT_USED` before running under Python 3.13 free-threaded production instances.

Services MUST verify that all third-party C-extensions declare `Py_MOD_GIL_NOT_USED` before running under Python 3.13 free-threaded production instances.

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 use global increment operators (e.g., `counter += 1`) or compound collection checks (e.g., `if key not in cache

Components NEVER use global increment operators (e.g., `counter += 1`) or compound collection checks (e.g., `if key not in cache: cache[key] = val`) without mutex guards, as bytecode interleaving and core parallelization cause race conditions and memory corruption.

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 (PEP 703) Thread-Safe State Mutations critical for AI coding assistants?

Enforces strict synchronization and memory safety when running FastAPI under Python 3.13t free-threaded mode (GIL disabled). Eliminates implicit reliance on the Global Interpreter Lock for atomic mutations across shared in-memory caches, singletons, app.state, and unverified C-extensions executing on multicore thread pools. 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/Module State Mutations and Non-Thread-Safe C-Extension Execution Under Disabled GIL) 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