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

FastAPI Python 3.13t Free-Threaded Memory Concurrency Standard

Enforces strict memory safety and thread synchronization across FastAPI applications executing on Python 3.13t free-threaded (PEP 703) runtimes without the Global Interpreter Lock (GIL). Prohibits naked mutable shared state across ASGI thread workers, eliminates torn reads and race conditions on built-in collections, and prevents process crashes caused by non-thread-safe native C-extensions.

Prohibited Anti-Pattern: Unsynchronized shared mutable state and unsafe C-extension invocation across free-threaded worker pools

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Python 3.13t Free-Threaded Memory Concurrency Standard - Enforces strict memory safety and thread synchronization across FastAPI applications executing on Python 3.13t free-threaded (PEP 703) runtimes without the Global Interpreter Lock (GIL). Prohibits naked mutable shared state across ASGI thread workers, eliminates torn reads and race conditions on built-in collections, and prevents process crashes caused by non-thread-safe native C-extensions.
globs: **/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-python313t-freethreaded-concurrency-guardrail"
---

# FastAPI Python 3.13t Free-Threaded Memory Concurrency Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized shared mutable state and unsafe C-extension invocation across free-threaded worker pools

## System Constraints
You are a Principal Systems Architect enforcing thread safety and memory barrier invariants for FastAPI applications running on Python 3.13t with free threading enabled (PYTHON_GIL=0). Mandate explicit synchronization on all shared state, isolate non-thread-safe C-extensions, and reject any assumption that built-in collection operations (dict, list, set) are atomic across threads.

## Strict Guidelines
- Engineers MUST NEVER rely on Python's historical GIL behavior for atomic operations (e.g., in-place addition `count += 1`, dict lookups with updates, list append-and-read operations) when deployed on Python 3.13t.
- Engineers MUST synchronize access to all in-memory mutable module-level or application-state variables across async thread pools using explicit locks (`threading.Lock`, `threading.RLock`, or `anyio.Lock`).
- Services running sync dependencies or sync route handlers via `run_in_threadpool` or AnyIO MUST guarantee that injected state dependencies are thread-safe or encapsulated per-request.
- Components NEVER invoke unverified, legacy native C-extensions directly across concurrent threads; native extensions without free-threaded validation (Py_MOD_GIL_NOT_USED) MUST be isolated behind dedicated worker processes or serialized through a dedicated single-threaded executor.
- Code SHOULD enforce immutability for application-level read-only configurations and global caches using `dataclasses.dataclass(frozen=True)` or `types.MappingProxyType`.
- Engineers MUST verify runtime GIL status at service startup via `sys._is_gil_enabled()` and fail closed or emit diagnostic telemetry if thread-unsafe paths are detected in a free-threaded runtime.

## Bad vs Good Implementations
### Synchronizing Shared In-Memory Metrics & Cache in Free-Threaded FastAPI

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Under Python 3.13t (free-threaded), `dict` is internally concurrent-read safe for some operations, but compound operations like read-then-write or mutations across threads without synchronization cause severe race conditions, torn writes, and intermittent silent data loss.
```python
from fastapi import FastAPI

app = FastAPI()

# VIOLATION: Bare shared dictionary and naked counter.
# In Python 3.13t, concurrent requests executing across OS threads
# will cause torn reads, torn writes, or corrupted dict hash tables.
request_counts: dict[str, int] = {}

@app.post("/track/{event_id}")
def track_event(event_id: str) -> dict[str, int]:
    # Sync handler executes on anyio worker thread pool without GIL
    current = request_counts.get(event_id, 0)
    request_counts[event_id] = current + 1
    return {"event_id": event_id, "count": request_counts[event_id]}
```

#### Production Standard (Correct)
> **Standard**: Encapsulates shared mutable state within a synchronized class using `threading.Lock()` to create an explicit memory barrier. Ensures thread safety across OS threads in 3.13t worker thread pools and validates GIL status during startup.
```python
import sys
import threading
from collections import defaultdict
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup guardrail: explicitly audit runtime GIL status
    is_gil_enabled = getattr(sys, "_is_gil_enabled", lambda: True)()
    app.state.gil_enabled = is_gil_enabled
    yield

app = FastAPI(lifespan=lifespan)

class SynchronizedMetricsRegistry:
    """Thread-safe counter registry designed for Python 3.13t free-threading."""
    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._counts: defaultdict[str, int] = defaultdict(int)

    def increment(self, key: str) -> int:
        with self._lock:
            self._counts[key] += 1
            return self._counts[key]

metrics_registry = SynchronizedMetricsRegistry()

@app.post("/track/{event_id}")
def track_event(event_id: str) -> dict[str, int]:
    """Sync route handler executing safely across AnyIO thread pools."""
    new_count = metrics_registry.increment(event_id)
    return {"event_id": event_id, "count": new_count}
```

### Isolating Legacy C-Extensions via Dedicated Serialized Executor

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Calling a non-thread-safe C-extension from multiple threads on Python 3.13t without the GIL triggers corrupt internal memory structures, invalid pointer dereferences, and fatal SIGSEGV crashes.
```python
from fastapi import FastAPI
# VIOLATION: Calling legacy C-API extension with global C state directly from
# free-threaded FastAPI endpoints leads to hard segmentation faults (SIGSEGV).
import legacy_c_extension  # Non-reentrant, lacks Py_MOD_GIL_NOT_USED

app = FastAPI()

@app.post("/compute")
async def compute(payload: dict):
    # Executes concurrently on multiple thread workers without GIL protection
    result = legacy_c_extension.process_data(payload)
    return {"result": result}
```

#### Production Standard (Correct)
> **Standard**: Constrains non-thread-safe native calls to a single-threaded executor (`max_workers=1`), preventing concurrent C-level execution and eliminating segmentation faults on non-GIL Python 3.13 runtimes.
```python
import asyncio
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI
import legacy_c_extension

app = FastAPI()

# PRODUCTION FIX: Serialize calls to thread-hostile native extensions via
# a dedicated single-threaded executor or dedicated worker process.
_c_extension_executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="c_ext_worker")

@app.post("/compute")
async def compute(payload: dict):
    loop = asyncio.get_running_loop()
    # Confine invocation to a single OS thread to guarantee reentrancy safety
    result = await loop.run_in_executor(
        _c_extension_executor,
        legacy_c_extension.process_data,
        payload
    )
    return {"result": result}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Python 3.13t Free-Threaded Memory Concurrency Standard

FastAPI Python 3.13t Free-Threaded Memory Concurrency Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict memory safety and thread synchronization across FastAPI applications executing on Python 3.13t free-threaded (PEP 703) runtimes without the Global Interpreter Lock (GIL). Prohibits naked mutable shared state across ASGI thread workers, eliminates torn reads and race conditions on built-in collections, and prevents process crashes caused by non-thread-safe native C-extensions. 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 mutable state and unsafe C-extension invocation 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 shared mutable state and unsafe C-extension invocation across free-threaded worker poolsCritical
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 NEVER rely on Python's historical GIL behavior for atomic operations (e.g., in-place addition `count += 1`, dict lookups with updates, list append-and-read operations) when deployed on Python 3.13t.

Engineers MUST NEVER rely on Python's historical GIL behavior for atomic operations (e.g., in-place addition `count += 1`, dict lookups with updates, list append-and-read operations) when deployed on Python 3.13t.

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 synchronize access to all in-memory mutable module-level or application-state variables across async thread pools using explicit locks (`threading.Lock`, `threading.RLock`, or `anyio.Lock`).

Engineers MUST synchronize access to all in-memory mutable module-level or application-state variables across async thread pools using explicit locks (`threading.Lock`, `threading.RLock`, or `anyio.Lock`).

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

Services running sync dependencies or sync route handlers via `run_in_threadpool` or AnyIO MUST guarantee that injected state dependencies are thread-safe or encapsulated per-request.

Services running sync dependencies or sync route handlers via `run_in_threadpool` or AnyIO MUST guarantee that injected state dependencies are thread-safe or encapsulated per-request.

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

Components NEVER invoke unverified, legacy native C-extensions directly across concurrent threads

Components NEVER invoke unverified, legacy native C-extensions directly across concurrent threads; native extensions without free-threaded validation (Py_MOD_GIL_NOT_USED) MUST be isolated behind dedicated worker processes or serialized through a dedicated single-threaded executor.

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

Code SHOULD enforce immutability for application-level read-only configurations and global caches using `dataclasses.dataclass(frozen=True)` or `types.MappingProxyType`.

Code SHOULD enforce immutability for application-level read-only configurations and global caches using `dataclasses.dataclass(frozen=True)` or `types.MappingProxyType`.

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

Engineers MUST verify runtime GIL status at service startup via `sys._is_gil_enabled()` and fail closed or emit diagnostic telemetry if thread-unsafe paths are detected in a free-threaded runtime.

Engineers MUST verify runtime GIL status at service startup via `sys._is_gil_enabled()` and fail closed or emit diagnostic telemetry if thread-unsafe paths are detected in a free-threaded runtime.

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.13t Free-Threaded Memory Concurrency Standard critical for AI coding assistants?

Enforces strict memory safety and thread synchronization across FastAPI applications executing on Python 3.13t free-threaded (PEP 703) runtimes without the Global Interpreter Lock (GIL). Prohibits naked mutable shared state across ASGI thread workers, eliminates torn reads and race conditions on built-in collections, and prevents process crashes caused by non-thread-safe native C-extensions. 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 mutable state and unsafe C-extension invocation 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