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

Free-Threaded Python 3.13 Concurrency Isolation and Safe Shared Memory Standard

Enforces strict thread-safe request context isolation and synchronization primitives for FastAPI microservices running under free-threaded Python 3.13 (PEP 703). Prevents data races, torn reads, and cross-thread memory corruption caused by concurrent mutations to unsynchronized module-level state, standard collections, and shared object pools.

Prohibited Anti-Pattern: Unsynchronized Global State Mutation and Context Bleed Under Free-Threaded Execution

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Free-Threaded Python 3.13 Concurrency Isolation and Safe Shared Memory Standard - Enforces strict thread-safe request context isolation and synchronization primitives for FastAPI microservices running under free-threaded Python 3.13 (PEP 703). Prevents data races, torn reads, and cross-thread memory corruption caused by concurrent mutations to unsynchronized module-level state, standard collections, and shared object pools.
globs: **/*.py, 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-concurrency-isolation"
---

# Free-Threaded Python 3.13 Concurrency Isolation and Safe Shared Memory Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Global State Mutation and Context Bleed Under Free-Threaded Execution

## System Constraints
You are a Principal Software Architect enforcing Python 3.13 free-threaded (GIL-less) concurrency isolation across all FastAPI services. Mandate the complete elimination of unsynchronized mutable global state, unsafe in-memory singletons, and thread-unsafe caching patterns. Without the Global Interpreter Lock, standard operations like dict mutations and in-place list updates are subject to concurrent race conditions. You must enforce the use of contextvars for request contexts, explicit threading synchronization primitives (Lock, RLock) for shared memory, thread-safe concurrent data structures, and frozen immutable data models for cross-thread messaging.

## Strict Guidelines
- Engineers MUST ALWAYS isolate request-scoped state using `contextvars.ContextVar` instead of module-level variables or threading.local across async thread pools.
- Engineers MUST NOT mutate raw Python collections (`dict`, `set`, `list`) concurrently across threads without explicit synchronization via `threading.Lock` or `threading.RLock`.
- Services MUST utilize immutable data structures (`dataclasses.dataclass(frozen=True)` or `pydantic.ConfigDict(frozen=True)`) when broadcasting or sharing data payloads between concurrent worker threads.
- Code SHOULD leverage non-blocking thread-safe concurrent queues (`queue.Queue` or `queue.SimpleQueue`) instead of shared list-based buffers for inter-thread task distribution.
- Components NEVER rely on Python's legacy GIL assumptions for thread-safe read-modify-write operations such as `counter += 1` or `cache.setdefault()`.

## Bad vs Good Implementations
### Request Context Isolation and Shared Counter Synchronization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In free-threaded Python 3.13, parallel OS threads executing pure Python code simultaneously mutate the global variable `current_tenant` and the standard dictionary `request_metrics`. This causes context bleed across concurrent client requests and non-deterministic memory/state corruption during concurrent read-modify-write operations on unshielded dictionaries.
```python
from fastapi import FastAPI, Request

app = FastAPI()

# Anti-pattern: Unsynchronized global state mutated concurrently in GIL-less Python 3.13
request_metrics = {"active_requests": 0}
current_tenant = None

@app.middleware("http")
async def trace_tenant(request: Request, call_next):
    global current_tenant
    # Race condition: Overwrites tenant context across concurrent OS threads
    current_tenant = request.headers.get("X-Tenant-ID", "default")
    
    # Race condition: Non-atomic in-place dictionary modification
    request_metrics["active_requests"] += 1
    try:
        return await call_next(request)
    finally:
        request_metrics["active_requests"] -= 1

```

#### Production Standard (Correct)
> **Standard**: Uses `contextvars.ContextVar` to guarantee request context isolation across concurrent thread executions, eliminating cross-tenant leakage. Encapsulates mutable shared state within a synchronized class protected by `threading.Lock`, ensuring thread-safe reads and writes under free-threaded Python 3.13.
```python
from contextvars import ContextVar
from threading import Lock
from dataclasses import dataclass
from fastapi import FastAPI, Request, Response

app = FastAPI()

# Immutable request context isolation
tenant_context: ContextVar[str] = ContextVar("tenant_context", default="default")

@dataclass
class ThreadSafeMetrics:
    _lock: Lock = Lock()
    _active_requests: int = 0

    def increment(self) -> int:
        with self._lock:
            self._active_requests += 1
            return self._active_requests

    def decrement(self) -> int:
        with self._lock:
            self._active_requests -= 1
            return self._active_requests

metrics = ThreadSafeMetrics()

@app.middleware("http")
async def trace_tenant(request: Request, call_next) -> Response:
    # Isolated context copy per async task / thread execution frame
    tenant_token = tenant_context.set(request.headers.get("X-Tenant-ID", "default"))
    metrics.increment()
    try:
        return await call_next(request)
    finally:
        metrics.decrement()
        tenant_context.reset(tenant_token)

```

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: Free-Threaded Python 3.13 Concurrency Isolation and Safe Shared Memory Standard

Free-Threaded Python 3.13 Concurrency Isolation and Safe Shared Memory Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict thread-safe request context isolation and synchronization primitives for FastAPI microservices running under free-threaded Python 3.13 (PEP 703). Prevents data races, torn reads, and cross-thread memory corruption caused by concurrent mutations to unsynchronized module-level state, standard collections, and shared object 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 State Mutation and Context Bleed Under Free-Threaded Execution 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 State Mutation and Context Bleed Under Free-Threaded ExecutionCritical
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 isolate request-scoped state using `contextvars.ContextVar` instead of module-level variables or threading.local across async thread pools.

Engineers MUST ALWAYS isolate request-scoped state using `contextvars.ContextVar` instead of module-level variables or threading.local across async thread pools.

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 mutate raw Python collections (`dict`, `set`, `list`) concurrently across threads without explicit synchronization via `threading.Lock` or `threading.RLock`.

Engineers MUST NOT mutate raw Python collections (`dict`, `set`, `list`) concurrently across threads without explicit synchronization via `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

Services MUST utilize immutable data structures (`dataclasses.dataclass(frozen=True)` or `pydantic.ConfigDict(frozen=True)`) when broadcasting or sharing data payloads between concurrent worker threads.

Services MUST utilize immutable data structures (`dataclasses.dataclass(frozen=True)` or `pydantic.ConfigDict(frozen=True)`) when broadcasting or sharing data payloads between concurrent 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

Code SHOULD leverage non-blocking thread-safe concurrent queues (`queue.Queue` or `queue.SimpleQueue`) instead of shared list-based buffers for inter-thread task distribution.

Code SHOULD leverage non-blocking thread-safe concurrent queues (`queue.Queue` or `queue.SimpleQueue`) instead of shared list-based buffers for inter-thread task distribution.

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 rely on Python's legacy GIL assumptions for thread-safe read-modify-write operations such as `counter += 1` or `cache.setdefault()`.

Components NEVER rely on Python's legacy GIL assumptions for thread-safe read-modify-write operations such as `counter += 1` or `cache.setdefault()`.

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 Free-Threaded Python 3.13 Concurrency Isolation and Safe Shared Memory Standard critical for AI coding assistants?

Enforces strict thread-safe request context isolation and synchronization primitives for FastAPI microservices running under free-threaded Python 3.13 (PEP 703). Prevents data races, torn reads, and cross-thread memory corruption caused by concurrent mutations to unsynchronized module-level state, standard collections, and shared object 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 State Mutation and Context Bleed Under Free-Threaded Execution) 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

Free-Threaded