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

FastAPI Python 3.13 Free-Threaded (nogil) Concurrency & C-Extension Isolation

Enforces strict thread-safe state synchronization and process isolation for non-reentrant C-extension bindings when running FastAPI under Python 3.13 free-threaded execution (nogil). Eliminates silent data corruption, torn reads, and SIGSEGV crashes caused by concurrent OS thread execution.

Prohibited Anti-Pattern: Unsynchronized Shared State Access and Unisolated Non-Reentrant C-Extensions Under Free-Threaded Python

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Python 3.13 Free-Threaded (nogil) Concurrency & C-Extension Isolation - Enforces strict thread-safe state synchronization and process isolation for non-reentrant C-extension bindings when running FastAPI under Python 3.13 free-threaded execution (nogil). Eliminates silent data corruption, torn reads, and SIGSEGV crashes caused by concurrent OS thread execution.
globs: 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-fastapi-on-pytho-178"
---

# FastAPI Python 3.13 Free-Threaded (nogil) Concurrency & C-Extension Isolation

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Shared State Access and Unisolated Non-Reentrant C-Extensions Under Free-Threaded Python

## System Constraints
You are a Principal Software Architect enforcing Python 3.13 Free-Threaded (nogil) concurrency guardrails for FastAPI. In a free-threaded runtime, the Global Interpreter Lock is disabled, enabling true parallel execution of bytecode across multiple OS threads. You must assume all module-level and app-level mutable state can be accessed concurrently by multiple threads. Mandate explicit synchronization via thread-safe primitives, isolate non-reentrant C-extensions to dedicated processes or strictly serialized single-thread runners, and prevent thread-unsafe cross-context sharing.

## Strict Guidelines
- Engineers MUST ALWAYS protect shared in-memory collections and mutable service state with explicit OS thread synchronization primitives (e.g., threading.Lock, threading.RLock) instead of relying on historically atomic bytecode operations.
- Engineers MUST NOT execute legacy or non-reentrant C-extensions lacking Py_MOD_PER_INTERPRETER_GIL_SUPPORTED or documented free-threading safety within default anyio/FastAPI threadpools.
- Code MUST isolate non-reentrant C-extension invocations within a dedicated concurrent.futures.ProcessPoolExecutor or a pinned, single-worker serialization queue to avoid memory corruption and segfaults.
- Services MUST verify thread safety of all third-party dependencies before executing them concurrently in asyncio.to_thread or standard def endpoint pools.
- Components NEVER store mutable request state on shared dependency singletons or global module namespaces without thread-local or context-local isolation (e.g., contextvars.ContextVar).

## Bad vs Good Implementations
### Shared Counter & Non-Reentrant C-Library Isolation in FastAPI

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Under Python 3.13 free-threaded execution, multiple worker threads running def endpoints execute simultaneously on real CPU cores without a GIL. In-place dictionary read-modify-write causes torn data structures, while concurrent invocations into legacy C-extensions that use global C static state result in heap corruption and instant SIGSEGV segmentation faults.
```python
from fastapi import FastAPI
import legacy_c_geom  # Legacy non-reentrant C-extension without nogil support

app = FastAPI()

# ANTI-PATTERN: Unsynchronized global dictionary under Python 3.13 nogil
# Causes torn reads, race conditions, or internal bucket corruption under parallel OS threads
request_metrics: dict[str, int] = {}

@app.post("/process")
def process_data(payload: dict):
    # Data race: dict mutation without GIL is not thread-safe for compound ops
    client_id = payload.get("client_id", "anonymous")
    request_metrics[client_id] = request_metrics.get(client_id, 0) + 1

    # FATAL: Calling non-reentrant C-binding directly across parallel threadpool threads.
    # Concurrently invoking un-synchronized legacy C code triggers SIGSEGV/memory corruption.
    result = legacy_c_geom.compute_convex_hull(payload["points"])
    return {"status": "ok", "hull": result}
```

#### Production Standard (Correct)
> **Standard**: Protects shared in-memory collections with an explicit threading.Lock for deterministic parallel mutation, and isolates non-reentrant C-extension computations inside a spawn-based ProcessPoolExecutor so unverified native bindings never run concurrently on shared address-space threads.
```python
from fastapi import FastAPI, Depends
from concurrent.futures import ProcessPoolExecutor
from threading import Lock
from contextlib import asynccontextmanager
import multiprocessing

# Guarded shared state using explicit thread locking
_metrics_lock = Lock()
_request_metrics: dict[str, int] = {}

def record_metric(client_id: str) -> None:
    with _metrics_lock:
        _request_metrics[client_id] = _request_metrics.get(client_id, 0) + 1

# Offload non-reentrant C-extension tasks to dedicated isolated worker processes
_process_pool: ProcessPoolExecutor | None = None

@asynccontextmanager
async def lifespan(app: FastAPI):
    global _process_pool
    mp_context = multiprocessing.get_context("spawn")
    _process_pool = ProcessPoolExecutor(max_workers=4, mp_context=mp_context)
    yield
    if _process_pool:
        _process_pool.shutdown(wait=True)

app = FastAPI(lifespan=lifespan)

def run_legacy_c_task(points: list[list[float]]) -> list[float]:
    # Imported and executed inside the child process exclusively
    import legacy_c_geom
    return legacy_c_geom.compute_convex_hull(points)

@app.post("/process")
async def process_data(payload: dict):
    client_id = payload.get("client_id", "anonymous")
    record_metric(client_id)

    # Safe: Isolated in separate OS process memory space, immune to nogil thread races
    import asyncio
    loop = asyncio.get_running_loop()
    result = await loop.run_in_executor(_process_pool, run_legacy_c_task, payload["points"])
    return {"status": "ok", "hull": result}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Python 3.13 Free-Threaded (nogil) Concurrency & C-Extension Isolation

FastAPI Python 3.13 Free-Threaded (nogil) Concurrency & C-Extension Isolation defines an industrial-grade behavioral standard for backend engineering. Enforces strict thread-safe state synchronization and process isolation for non-reentrant C-extension bindings when running FastAPI under Python 3.13 free-threaded execution (nogil). Eliminates silent data corruption, torn reads, and SIGSEGV crashes caused by concurrent OS thread execution. 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 State Access and Unisolated Non-Reentrant C-Extensions Under Free-Threaded Python 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 State Access and Unisolated Non-Reentrant C-Extensions Under Free-Threaded PythonCritical
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 in-memory collections and mutable service state with explicit OS thread synchronization primitives (e.g., threading.Lock, threading.RLock) instead of relying on historically atomic bytecode operations.

Engineers MUST ALWAYS protect shared in-memory collections and mutable service state with explicit OS thread synchronization primitives (e.g., threading.Lock, threading.RLock) instead of relying on historically atomic bytecode operations.

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 execute legacy or non-reentrant C-extensions lacking Py_MOD_PER_INTERPRETER_GIL_SUPPORTED or documented free-threading safety within default anyio/FastAPI threadpools.

Engineers MUST NOT execute legacy or non-reentrant C-extensions lacking Py_MOD_PER_INTERPRETER_GIL_SUPPORTED or documented free-threading safety within default anyio/FastAPI threadpools.

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 isolate non-reentrant C-extension invocations within a dedicated concurrent.futures.ProcessPoolExecutor or a pinned, single-worker serialization queue to avoid memory corruption and segfaults.

Code MUST isolate non-reentrant C-extension invocations within a dedicated concurrent.futures.ProcessPoolExecutor or a pinned, single-worker serialization queue to avoid memory corruption and segfaults.

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 thread safety of all third-party dependencies before executing them concurrently in asyncio.to_thread or standard def endpoint pools.

Services MUST verify thread safety of all third-party dependencies before executing them concurrently in asyncio.to_thread or standard def endpoint 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.
5

Components NEVER store mutable request state on shared dependency singletons or global module namespaces without thread-local or context-local isolation (e.g., contextvars.ContextVar).

Components NEVER store mutable request state on shared dependency singletons or global module namespaces without thread-local or context-local isolation (e.g., 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 (nogil) Concurrency & C-Extension Isolation critical for AI coding assistants?

Enforces strict thread-safe state synchronization and process isolation for non-reentrant C-extension bindings when running FastAPI under Python 3.13 free-threaded execution (nogil). Eliminates silent data corruption, torn reads, and SIGSEGV crashes caused by concurrent OS thread execution. 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 State Access and Unisolated Non-Reentrant C-Extensions Under Free-Threaded Python) 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