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

FastAPI Free-Threaded Python 3.13 Asyncio Thread-Safety Guardrails

In Python 3.13 free-threaded builds (PEP 703), the removal of the Global Interpreter Lock (GIL) enables simultaneous execution of Python bytecode across OS threads. Mutating module-level state, misusing asyncio loop primitives across worker threads, or relying on implicit GIL atomicity in FastAPI endpoints introduces critical data races, silent memory corruption, and undefined behavior.

Prohibited Anti-Pattern: Unsynchronized Global Mutations and Non-Thread-Safe Async Loop Sharing Under Free-Threaded Python 3.13

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Free-Threaded Python 3.13 Asyncio Thread-Safety Guardrails - In Python 3.13 free-threaded builds (PEP 703), the removal of the Global Interpreter Lock (GIL) enables simultaneous execution of Python bytecode across OS threads. Mutating module-level state, misusing asyncio loop primitives across worker threads, or relying on implicit GIL atomicity in FastAPI endpoints introduces critical data races, silent memory corruption, and undefined behavior.
globs: **/*.py, app/**/*.py, src/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-free-threaded-python-313-thread-safety-invariants"
---

# FastAPI Free-Threaded Python 3.13 Asyncio Thread-Safety Guardrails

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Global Mutations and Non-Thread-Safe Async Loop Sharing Under Free-Threaded Python 3.13

## System Constraints
You are a Principal Engineer enforcing thread-safety invariants for FastAPI applications running on free-threaded Python 3.13 (GIL disabled). Mandate strict partitioning of request-scoped context, explicit mutual exclusion for shared in-process state, and proper cross-thread asyncio synchronization.

## Strict Guidelines
- Engineers MUST ALWAYS use `contextvars.ContextVar` instead of mutable module globals or `threading.local()` for request-scoped state to guarantee isolation across asynchronous tasks and worker threads.
- Engineers MUST NOT rely on implicit bytecode atomicity for standard Python collections (`dict`, `list`, `set`); all concurrent reads/writes to shared state MUST be protected by explicit synchronization primitives like `threading.Lock` or `threading.RWLock`.
- Code MUST NEVER access or invoke methods on an `asyncio.AbstractEventLoop` instance from a separate OS thread without wrapping the call in `loop.call_soon_threadsafe()`.
- Services MUST verify third-party native C-extensions explicitly declare free-threading support via `Py_mod_gil` to prevent silent GIL re-enablement or hard segmentation faults.
- Components NEVER store connection handles, HTTP clients, or session instances in global module scope without thread-safe connection pooling.

## Bad vs Good Implementations
### Shared In-Memory State and Request Context Isolation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Python 3.13 free-threaded runtimes, bytecode evaluation is fully parallel across OS threads. Mutating `GLOBAL_METRICS` without locks produces lost updates and data race exceptions. Assigning `CURRENT_USER` to a module global bleeds request identities across concurrent execution threads.
```python
from fastapi import FastAPI, Request

app = FastAPI()

# Anti-pattern: Global mutable state relies on legacy GIL for pseudo-atomicity
# In Python 3.13t (free-threaded), concurrent access causes race conditions and memory corruption
GLOBAL_METRICS = {"request_count": 0}
CURRENT_USER = None

@app.middleware("http")
async def set_context(request: Request, call_next):
    global CURRENT_USER
    CURRENT_USER = request.headers.get("X-User-ID")
    response = await call_next(request)
    return response

@app.get("/metrics")
async def increment_metrics():
    # Unsynchronized concurrent read-modify-write across OS threads
    GLOBAL_METRICS["request_count"] += 1
    return {"count": GLOBAL_METRICS["request_count"], "user": CURRENT_USER}
```

#### Production Standard (Correct)
> **Standard**: Uses `ContextVar` to partition request state per coroutine/thread context and encapsulates shared metrics behind a `threading.Lock`, eliminating data races under free-threaded execution.
```python
from contextvars import ContextVar
from threading import Lock
from typing import Optional
from fastapi import FastAPI, Request

app = FastAPI()

# Request-isolated context variable safe across concurrent async tasks and threads
current_user_ctx: ContextVar[Optional[str]] = ContextVar("current_user_ctx", default=None)

# Explicit synchronization lock protecting shared in-memory data structures in 3.13t
class ThreadSafeMetrics:
    def __init__(self) -> None:
        self._lock = Lock()
        self._count = 0

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

metrics = ThreadSafeMetrics()

@app.middleware("http")
async def set_context(request: Request, call_next):
    token = current_user_ctx.set(request.headers.get("X-User-ID"))
    try:
        return await call_next(request)
    finally:
        current_user_ctx.reset(token)

@app.get("/metrics")
async def increment_metrics():
    count = metrics.increment()
    user = current_user_ctx.get()
    return {"count": count, "user": user}
```

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 Free-Threaded Python 3.13 Asyncio Thread-Safety Guardrails

FastAPI Free-Threaded Python 3.13 Asyncio Thread-Safety Guardrails defines an industrial-grade behavioral standard for backend engineering. In Python 3.13 free-threaded builds (PEP 703), the removal of the Global Interpreter Lock (GIL) enables simultaneous execution of Python bytecode across OS threads. Mutating module-level state, misusing asyncio loop primitives across worker threads, or relying on implicit GIL atomicity in FastAPI endpoints introduces critical data races, silent memory corruption, and undefined behavior. 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 Mutations and Non-Thread-Safe Async Loop Sharing Under Free-Threaded Python 3.13 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 Mutations and Non-Thread-Safe Async Loop Sharing Under Free-Threaded Python 3.13Critical
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 use `contextvars.ContextVar` instead of mutable module globals or `threading.local()` for request-scoped state to guarantee isolation across asynchronous tasks and worker threads.

Engineers MUST ALWAYS use `contextvars.ContextVar` instead of mutable module globals or `threading.local()` for request-scoped state to guarantee isolation across asynchronous tasks and 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.
2

Engineers MUST NOT rely on implicit bytecode atomicity for standard Python collections (`dict`, `list`, `set`)

Engineers MUST NOT rely on implicit bytecode atomicity for standard Python collections (`dict`, `list`, `set`); all concurrent reads/writes to shared state MUST be protected by explicit synchronization primitives like `threading.Lock` or `threading.RWLock`.

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 NEVER access or invoke methods on an `asyncio.AbstractEventLoop` instance from a separate OS thread without wrapping the call in `loop.call_soon_threadsafe()`.

Code MUST NEVER access or invoke methods on an `asyncio.AbstractEventLoop` instance from a separate OS thread without wrapping the call in `loop.call_soon_threadsafe()`.

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 third-party native C-extensions explicitly declare free-threading support via `Py_mod_gil` to prevent silent GIL re-enablement or hard segmentation faults.

Services MUST verify third-party native C-extensions explicitly declare free-threading support via `Py_mod_gil` to prevent silent GIL re-enablement or hard segmentation faults.

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 connection handles, HTTP clients, or session instances in global module scope without thread-safe connection pooling.

Components NEVER store connection handles, HTTP clients, or session instances in global module scope without thread-safe connection pooling.

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 Free-Threaded Python 3.13 Asyncio Thread-Safety Guardrails critical for AI coding assistants?

In Python 3.13 free-threaded builds (PEP 703), the removal of the Global Interpreter Lock (GIL) enables simultaneous execution of Python bytecode across OS threads. Mutating module-level state, misusing asyncio loop primitives across worker threads, or relying on implicit GIL atomicity in FastAPI endpoints introduces critical data races, silent memory corruption, and undefined behavior. 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 Mutations and Non-Thread-Safe Async Loop Sharing Under Free-Threaded Python 3.13) 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