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

FastAPI Python 3.13 Free-Threaded Shared Memory and Subinterpreter Isolation

Enforces thread-safe in-memory state synchronization and subinterpreter isolation guarantees for FastAPI services running on Python 3.13 free-threaded (no-GIL) runtimes. Eliminates data races, memory tearing, and lost updates caused by legacy assumptions of Global Interpreter Lock atomicity.

Prohibited Anti-Pattern: Unsynchronized Mutable Global State and Composite Operations in Python 3.13 Free-Threaded Runtimes

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Python 3.13 Free-Threaded Shared Memory and Subinterpreter Isolation - Enforces thread-safe in-memory state synchronization and subinterpreter isolation guarantees for FastAPI services running on Python 3.13 free-threaded (no-GIL) runtimes. Eliminates data races, memory tearing, and lost updates caused by legacy assumptions of Global Interpreter Lock atomicity.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-py313-free-threaded-memory-isolation"
---

# FastAPI Python 3.13 Free-Threaded Shared Memory and Subinterpreter Isolation

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Mutable Global State and Composite Operations in Python 3.13 Free-Threaded Runtimes

## System Constraints
You are a Principal Systems Architect enforcing rigorous memory safety, subinterpreter boundaries, and concurrency correctness for FastAPI applications deployed on Python 3.13 free-threaded runtimes (PEP 703). You must reject any code relying on GIL-enforced atomicity and mandate explicit synchronization primitives, copy-on-write immutability, or isolated actor queues.

## Strict Guidelines
- Engineers MUST ALWAYS protect mutable in-memory structures (dictionaries, sets, lists, custom objects) with explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or read-write locks) when executed under Python 3.13t (free-threaded).
- Engineers MUST NOT rely on historically 'atomic' CPython dictionary or list operations (e.g., `dict[k] = v`, `list.append()`, `counter += 1`) for thread safety in free-threaded mode.
- Code SHOULD enforce immutability for cross-thread shared state using `@dataclass(frozen=True, slots=True)` and atomic reference swaps (`threading.Lock` guarded pointers or atomic reference containers).
- Services MUST isolate subinterpreter or worker-thread communication using thread-safe channels (`queue.Queue`, shared memory buffers with explicit semaphores, or external state stores like Redis) rather than cross-interpreter raw pointer sharing.
- Components NEVER store request-scoped mutable state on the FastAPI `app.state` or module-level singletons without thread-local scoping (`threading.local`) or context variables (`contextvars.ContextVar`).

## Bad vs Good Implementations
### Thread-Safe Metrics Aggregation in Python 3.13 Free-Threaded FastAPI

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Python 3.13 free-threaded runtimes without the GIL, concurrent threads execute bytecodes simultaneously. In-place increment/decrement is a multi-step read-modify-write bytecode sequence (`LOAD_FAST`, `BINARY_OP`, `STORE_FAST`) resulting in lost updates, torn reads, and potential collection memory corruption.
```python
from fastapi import FastAPI

app = FastAPI()

# ANTI-PATTERN: Bare global mutable state relying on legacy GIL atomicity.
# In Python 3.13 free-threaded mode, concurrent requests execute in true parallel OS threads.
# Operations like `metrics['active_sessions'] += 1` create severe data races and lost updates.
metrics: dict[str, int] = {"total_requests": 0, "active_sessions": 0}

@app.post("/session/start")
async def start_session():
    metrics["total_requests"] += 1
    metrics["active_sessions"] += 1
    return {"active": metrics["active_sessions"]}

@app.post("/session/end")
async def end_session():
    metrics["active_sessions"] -= 1
    return {"active": metrics["active_sessions"]}
```

#### Production Standard (Correct)
> **Standard**: Encapsulates shared mutable state within a structured class guarded by an explicit `threading.Lock`. All read-modify-write cycles are atomic, guaranteeing safety across parallel OS threads under Python 3.13 free-threaded runtimes without relying on GIL side effects.
```python
import threading
from dataclasses import dataclass, field
from fastapi import FastAPI

app = FastAPI()

@dataclass(slots=True)
class ThreadSafeMetrics:
    _lock: threading.Lock = field(default_factory=threading.Lock)
    total_requests: int = 0
    active_sessions: int = 0

    def record_session_start(self) -> int:
        with self._lock:
            self.total_requests += 1
            self.active_sessions += 1
            return self.active_sessions

    def record_session_end(self) -> int:
        with self._lock:
            self.active_sessions = max(0, self.active_sessions - 1)
            return self.active_sessions

metrics_registry = ThreadSafeMetrics()

@app.post("/session/start")
async def start_session():
    # PRODUCTION FIX: Coarse or fine-grained explicit synchronization ensures
    # true thread safety across non-GIL execution threads in Python 3.13t.
    active = metrics_registry.record_session_start()
    return {"active": active}

@app.post("/session/end")
async def end_session():
    active = metrics_registry.record_session_end()
    return {"active": active}
```

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 Python 3.13 Free-Threaded Shared Memory and Subinterpreter Isolation

FastAPI Python 3.13 Free-Threaded Shared Memory and Subinterpreter Isolation defines an industrial-grade behavioral standard for backend engineering. Enforces thread-safe in-memory state synchronization and subinterpreter isolation guarantees for FastAPI services running on Python 3.13 free-threaded (no-GIL) runtimes. Eliminates data races, memory tearing, and lost updates caused by legacy assumptions of Global Interpreter Lock atomicity. 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 Global State and Composite Operations in Python 3.13 Free-Threaded 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 Global State and Composite Operations in Python 3.13 Free-Threaded 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 protect mutable in-memory structures (dictionaries, sets, lists, custom objects) with explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or read-write locks) when executed under Python 3.13t (free-threaded).

Engineers MUST ALWAYS protect mutable in-memory structures (dictionaries, sets, lists, custom objects) with explicit synchronization primitives (`threading.Lock`, `threading.RLock`, or read-write locks) when executed under 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 historically 'atomic' CPython dictionary or list operations (e.g., `dict[k] = v`, `list.append()`, `counter += 1`) for thread safety in free-threaded mode.

Engineers MUST NOT rely on historically 'atomic' CPython dictionary or list operations (e.g., `dict[k] = v`, `list.append()`, `counter += 1`) for thread safety in free-threaded mode.

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 for cross-thread shared state using `@dataclass(frozen=True, slots=True)` and atomic reference swaps (`threading.Lock` guarded pointers or atomic reference containers).

Code SHOULD enforce immutability for cross-thread shared state using `@dataclass(frozen=True, slots=True)` and atomic reference swaps (`threading.Lock` guarded pointers or atomic reference containers).

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 subinterpreter or worker-thread communication using thread-safe channels (`queue.Queue`, shared memory buffers with explicit semaphores, or external state stores like Redis) rather than cross-interpreter raw pointer sharing.

Services MUST isolate subinterpreter or worker-thread communication using thread-safe channels (`queue.Queue`, shared memory buffers with explicit semaphores, or external state stores like Redis) rather than cross-interpreter raw pointer 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 state on the FastAPI `app.state` or module-level singletons without thread-local scoping (`threading.local`) or context variables (`contextvars.ContextVar`).

Components NEVER store request-scoped mutable state on the FastAPI `app.state` or module-level singletons without thread-local scoping (`threading.local`) or context variables (`contextvars.ContextVar`).

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 Shared Memory and Subinterpreter Isolation critical for AI coding assistants?

Enforces thread-safe in-memory state synchronization and subinterpreter isolation guarantees for FastAPI services running on Python 3.13 free-threaded (no-GIL) runtimes. Eliminates data races, memory tearing, and lost updates caused by legacy assumptions of Global Interpreter Lock atomicity. 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 Global State and Composite Operations in Python 3.13 Free-Threaded 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

FastAPI