This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: python-313-nogil-fastapi-state-invariant

Python 3.13 Free-Threaded FastAPI Thread-Safe State & C-Extension Invariant

Enforces strict concurrency boundaries and thread synchronization primitives for FastAPI services running on Python 3.13 free-threaded (PEP 703, PYTHON_GIL=0) runtimes. Prevents silent memory corruption, race conditions in standard collection mutations, and segmentation faults originating from non-thread-safe C-extensions and shared in-memory state across parallel ASGI worker threads.

Prohibited Anti-Pattern: Unsynchronized Global Mutations and Non-Reentrant C-Extension Invocations in Free-Threaded FastAPI

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Python 3.13 Free-Threaded FastAPI Thread-Safe State & C-Extension Invariant - Enforces strict concurrency boundaries and thread synchronization primitives for FastAPI services running on Python 3.13 free-threaded (PEP 703, PYTHON_GIL=0) runtimes. Prevents silent memory corruption, race conditions in standard collection mutations, and segmentation faults originating from non-thread-safe C-extensions and shared in-memory state across parallel ASGI worker threads.
globs: **/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/python-313-nogil-fastapi-state-invariant"
---

# Python 3.13 Free-Threaded FastAPI Thread-Safe State & C-Extension Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Global Mutations and Non-Reentrant C-Extension Invocations in Free-Threaded FastAPI

## System Constraints
You are a Principal Software Architect enforcing thread safety under Python 3.13 free-threaded mode (PEP 703). With the Global Interpreter Lock disabled, built-in dicts, lists, sets, and C-extensions without explicit PEP 703 support (Py_MOD_GIL_NOT_USED) can experience data races, broken internal invariants, and fatal segmentation faults when accessed concurrently across worker threads. Mandate explicit mutual exclusion, immutable data structures, thread-local isolation, or external cache engines for all shared state.

## Strict Guidelines
- Engineers MUST ALWAYS protect shared mutable state (in-memory caches, metrics accumulators, connection registries) using explicit synchronization primitives such as threading.Lock, threading.RLock, or concurrent.futures abstractions.
- Engineers MUST NOT rely on dict or list operations being atomic; while reference counting is thread-safe in free-threaded 3.13, compound read-modify-write sequences (e.g., dict.setdefault, += on lists) are non-atomic and subject to race conditions.
- Services MUST verify and explicitly mark C-extension dependencies as thread-safe before deployment under PYTHON_GIL=0, wrapping legacy or non-reentrant C/Cython extensions in a process-isolated worker or a dedicated serial execution thread.
- Engineers MUST use threading.local() or contextvars.ContextVar for request-scoped metadata rather than module-level global variables or shared singleton service instances with mutable fields.
- ASGI lifespan dependencies in FastAPI MUST use frozen dataclasses, Pydantic models with frozen=True, or explicit thread-safe queue channels (queue.Queue) when exchanging state across concurrent endpoints.

## Bad vs Good Implementations
### Shared In-Memory State in FastAPI Endpoints

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Without the GIL, two threads concurrently running `track_request` for the same `user_id` can interleave execution of `.get()` and assignment, dropping increments or corrupting dictionary bucket links during concurrent rehashing.
```python
from fastapi import FastAPI

app = FastAPI()

# ANTI-PATTERN: Under Python 3.13 free-threaded, concurrent requests
# mutate standard dict simultaneously without GIL serialization, leading to data races.
global_request_cache: dict[str, int] = {}

@app.post("/track/{user_id}")
async def track_request(user_id: str) -> dict[str, int]:
    current = global_request_cache.get(user_id, 0)
    # Race condition: non-atomic read-modify-write across parallel threads
    global_request_cache[user_id] = current + 1
    return {"user_id": user_id, "count": global_request_cache[user_id]}
```

#### Production Standard (Correct)
> **Standard**: Encapsulating the shared collection inside a thread-safe wrapper guarded by `threading.Lock()` guarantees atomic read-modify-write cycles and memory consistency across parallel CPU threads in Python 3.13.
```python
import threading
from fastapi import FastAPI

app = FastAPI()

class SynchronizedUserCounter:
    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._counts: dict[str, int] = {}

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

counter = SynchronizedUserCounter()

@app.post("/track/{user_id}")
async def track_request(user_id: str) -> dict[str, int]:
    # Invariant preserved: lock serializes parallel thread access
    count = counter.increment(user_id)
    return {"user_id": user_id, "count": count}
```

### Non-Reentrant C-Extension / Legacy Driver Invocation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: C-extensions built for legacy GIL runtimes assume single-thread execution guarantees. Invoking them concurrently across multiple threads in Python 3.13 causes memory corruption and instant segmentation faults.
```python
from fastapi import FastAPI
# Legacy C-extension compiled without PEP 703 Py_MOD_GIL_NOT_USED
import legacy_c_crypto_driver

app = FastAPI()

@app.post("/decrypt")
def decrypt_payload(payload: bytes) -> bytes:
    # ANTI-PATTERN: Calling non-thread-safe C-extension directly from
    # concurrent ASGI threads causes internal state corruption and segfaults
    return legacy_c_crypto_driver.transform(payload)
```

#### Production Standard (Correct)
> **Standard**: Routing non-reentrant C-extension calls through a single-worker ThreadPoolExecutor guarantees serial execution without concurrency conflicts, shielding the free-threaded runtime from segfaults.
```python
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI
import legacy_c_crypto_driver

app = FastAPI()

# Pin non-thread-safe C-extension execution to a single dedicated thread
_single_thread_executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="legacy_c_driver")

@app.post("/decrypt")
async def decrypt_payload(payload: bytes) -> bytes:
    import asyncio
    loop = asyncio.get_running_loop()
    # Invariant preserved: single-worker executor ensures strictly serialized execution
    result = await loop.run_in_executor(
        _single_thread_executor, 
        legacy_c_crypto_driver.transform, 
        payload
    )
    return result
```

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: Python 3.13 Free-Threaded FastAPI Thread-Safe State & C-Extension Invariant

Python 3.13 Free-Threaded FastAPI Thread-Safe State & C-Extension Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces strict concurrency boundaries and thread synchronization primitives for FastAPI services running on Python 3.13 free-threaded (PEP 703, PYTHON_GIL=0) runtimes. Prevents silent memory corruption, race conditions in standard collection mutations, and segmentation faults originating from non-thread-safe C-extensions and shared in-memory state across parallel ASGI worker threads. 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-Reentrant C-Extension Invocations in Free-Threaded FastAPI 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-Reentrant C-Extension Invocations in Free-Threaded FastAPICritical
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 shared mutable state (in-memory caches, metrics accumulators, connection registries) using explicit synchronization primitives such as threading.Lock, threading.RLock, or concurrent.futures abstractions.

Engineers MUST ALWAYS protect shared mutable state (in-memory caches, metrics accumulators, connection registries) using explicit synchronization primitives such as threading.Lock, threading.RLock, or concurrent.futures abstractions.

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 dict or list operations being atomic

Engineers MUST NOT rely on dict or list operations being atomic; while reference counting is thread-safe in free-threaded 3.13, compound read-modify-write sequences (e.g., dict.setdefault, += on lists) are non-atomic and subject to race conditions.

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 verify and explicitly mark C-extension dependencies as thread-safe before deployment under PYTHON_GIL=0, wrapping legacy or non-reentrant C/Cython extensions in a process-isolated worker or a dedicated serial execution thread.

Services MUST verify and explicitly mark C-extension dependencies as thread-safe before deployment under PYTHON_GIL=0, wrapping legacy or non-reentrant C/Cython extensions in a process-isolated worker or a dedicated serial execution thread.

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

Engineers MUST use threading.local() or contextvars.ContextVar for request-scoped metadata rather than module-level global variables or shared singleton service instances with mutable fields.

Engineers MUST use threading.local() or contextvars.ContextVar for request-scoped metadata rather than module-level global variables or shared singleton service instances with mutable fields.

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

ASGI lifespan dependencies in FastAPI MUST use frozen dataclasses, Pydantic models with frozen=True, or explicit thread-safe queue channels (queue.Queue) when exchanging state across concurrent endpoints.

ASGI lifespan dependencies in FastAPI MUST use frozen dataclasses, Pydantic models with frozen=True, or explicit thread-safe queue channels (queue.Queue) when exchanging state across concurrent endpoints.

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 Python 3.13 Free-Threaded FastAPI Thread-Safe State & C-Extension Invariant critical for AI coding assistants?

Enforces strict concurrency boundaries and thread synchronization primitives for FastAPI services running on Python 3.13 free-threaded (PEP 703, PYTHON_GIL=0) runtimes. Prevents silent memory corruption, race conditions in standard collection mutations, and segmentation faults originating from non-thread-safe C-extensions and shared in-memory state across parallel ASGI worker threads. 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-Reentrant C-Extension Invocations in Free-Threaded FastAPI) 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

Python