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

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

Enforces strict memory safety and isolation invariants for FastAPI running on Python 3.13 free-threaded builds (nogil). Direct access to legacy, non-thread-safe C-extensions and unsynchronized shared mutable state within free-threaded workers induces silent memory corruption and SIGSEGV faults.

Prohibited Anti-Pattern: Executing non-nogil-verified C-extensions and unsynchronized shared mutable state in free-threaded FastAPI threadpools

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Free-Threaded (Python 3.13 nogil) Concurrency & Extension Isolation - Enforces strict memory safety and isolation invariants for FastAPI running on Python 3.13 free-threaded builds (nogil). Direct access to legacy, non-thread-safe C-extensions and unsynchronized shared mutable state within free-threaded workers induces silent memory corruption and SIGSEGV faults.
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-nogil-extension-isolation"
---

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

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Executing non-nogil-verified C-extensions and unsynchronized shared mutable state in free-threaded FastAPI threadpools

## System Constraints
You are a Principal Software Architect enforcing production safety for FastAPI services running under Python 3.13 free-threading (nogil / python3.13t). Free-threaded Python eliminates the Global Interpreter Lock, enabling parallel CPU execution across OS threads. You must strictly mandate that all shared mutable in-memory state is protected with explicit synchronization primitives (or replaced with concurrent/immutable structures), and all native C/C++ extensions lacking verified Py_MOD_PER_INTERPRETER_GIL_SUPPORTED or nogil ABI compatibility are strictly isolated to out-of-process workers (ProcessPoolExecutor or IPC microservices).

## Strict Guidelines
- Engineers MUST ALWAYS isolate unverified or legacy C-extensions (e.g., non-free-threaded compiled modules) into a dedicated ProcessPoolExecutor to prevent JVM/C-runtime state corruption or fatal SIGSEGV crashes.
- Engineers MUST NOT rely on single-bytecode atomicity (e.g., dict mutations, list operations, counter increments) for thread safety, as Python 3.13 nogil eliminates atomic GIL-based assumptions.
- Services MUST verify sys._is_gil_enabled() at startup and explicitly log the GIL runtime operational state to prevent undetected fallback to GIL-enabled execution.
- Engineers MUST wrap all shared in-memory state (caches, stateful counters, session trackers) with explicit synchronization primitives such as threading.Lock, threading.RLock, or thread-safe atomic data structures.
- FastAPI synchronous route handlers (plain 'def') MUST be evaluated for thread-safety, as AnyIO dispatches them to a ThreadPoolExecutor that executes with true multi-core parallel concurrency under nogil.
- Components NEVER execute long-running GIL-dependent native routines directly inside AnyIO worker threads, as doing so forces the runtime to re-enable the GIL globally or crash when threads race on unshielded C structs.

## Bad vs Good Implementations
### Legacy C-Extension Isolation and Shared State Synchronization in FastAPI nogil

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Python 3.13 free-threaded builds, dictionary mutations are no longer atomic without the GIL, causing silent state corruption under concurrent requests. Calling legacy C-extensions in-thread either globally forces the GIL back on, destroying concurrency, or triggers memory corruption and SIGSEGV segfaults when native structs are accessed simultaneously.
```python
import sys
from fastapi import FastAPI

app = FastAPI()

# Anti-pattern 1: Unsynchronized shared state assuming GIL atomicity
global_stats = {"request_count": 0, "last_user": None}

# Anti-pattern 2: Legacy C-extension compiled without Py_GIL_DISABLED
import legacy_c_image_processor  # type: ignore

@app.post("/process")
def process_image(image_bytes: bytes, user_id: str):
    # CRITICAL: In Python 3.13 nogil, plain 'def' runs on ThreadPoolExecutor
    # Concurrent modifications to global_stats cause race conditions and data corruption
    global_stats["request_count"] += 1
    global_stats["last_user"] = user_id

    # CRITICAL: Invoking legacy C-extension directly in a free-threaded worker.
    # C-extensions not explicitly built for Py_GIL_DISABLED will either cause the runtime
    # to reactivate the GIL globally (ruining parallelism) or crash with SIGSEGV due to races.
    result = legacy_c_image_processor.transform(image_bytes)
    return {"status": "ok", "size": len(result)}
```

#### Production Standard (Correct)
> **Standard**: Shared mutable state is safely synchronized using an explicit threading.Lock, preventing race conditions under nogil execution. Legacy native C-extensions are safely dispatched to a dedicated ProcessPoolExecutor, preserving free-threaded Python 3.13 scalability in the main process while preventing native memory corruption and segfaults.
```python
import asyncio
import sys
import threading
from concurrent.futures import ProcessPoolExecutor
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException

# Guard: Verify runtime GIL status on startup
IS_NOGIL = not getattr(sys, "_is_gil_enabled", lambda: True)()

class SynchronizedMetrics:
    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._request_count: int = 0
        self._last_user: str | None = None

    def record(self, user_id: str) -> int:
        with self._lock:
            self._request_count += 1
            self._last_user = user_id
            return self._request_count

metrics = SynchronizedMetrics()
process_pool: ProcessPoolExecutor | None = None

def _isolated_legacy_transform(image_bytes: bytes) -> bytes:
    # Isolated execution within a dedicated process boundary
    import legacy_c_image_processor  # type: ignore
    return legacy_c_image_processor.transform(image_bytes)

@asynccontextmanager
async def lifespan(app: FastAPI):
    global process_pool
    # Isolate legacy C-extensions across process boundaries to guarantee memory safety
    process_pool = ProcessPoolExecutor(max_workers=4)
    yield
    if process_pool:
        process_pool.shutdown(wait=True)

app = FastAPI(lifespan=lifespan)

@app.post("/process")
async def process_image(image_bytes: bytes, user_id: str):
    if process_pool is None:
        raise HTTPException(status_code=500, detail="Executor pool uninitialized")

    # Invariant: Synchronize shared state mutations explicitly
    current_count = metrics.record(user_id)

    # Invariant: Run non-nogil C-extensions outside the free-threaded process space
    loop = asyncio.get_running_loop()
    result = await loop.run_in_executor(process_pool, _isolated_legacy_transform, image_bytes)

    return {"status": "ok", "size": len(result), "total_requests": current_count}
```

Architectural Specification & Invariant Rationale

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

FastAPI Free-Threaded (Python 3.13 nogil) Concurrency & Extension Isolation defines an industrial-grade behavioral standard for backend engineering. Enforces strict memory safety and isolation invariants for FastAPI running on Python 3.13 free-threaded builds (nogil). Direct access to legacy, non-thread-safe C-extensions and unsynchronized shared mutable state within free-threaded workers induces silent memory corruption and SIGSEGV faults. 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: Executing non-nogil-verified C-extensions and unsynchronized shared mutable state in free-threaded FastAPI threadpools 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 DriftExecuting non-nogil-verified C-extensions and unsynchronized shared mutable state in free-threaded FastAPI threadpoolsCritical
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 unverified or legacy C-extensions (e.g., non-free-threaded compiled modules) into a dedicated ProcessPoolExecutor to prevent JVM/C-runtime state corruption or fatal SIGSEGV crashes.

Engineers MUST ALWAYS isolate unverified or legacy C-extensions (e.g., non-free-threaded compiled modules) into a dedicated ProcessPoolExecutor to prevent JVM/C-runtime state corruption or fatal SIGSEGV crashes.

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 single-bytecode atomicity (e.g., dict mutations, list operations, counter increments) for thread safety, as Python 3.13 nogil eliminates atomic GIL-based assumptions.

Engineers MUST NOT rely on single-bytecode atomicity (e.g., dict mutations, list operations, counter increments) for thread safety, as Python 3.13 nogil eliminates atomic GIL-based assumptions.

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 sys._is_gil_enabled() at startup and explicitly log the GIL runtime operational state to prevent undetected fallback to GIL-enabled execution.

Services MUST verify sys._is_gil_enabled() at startup and explicitly log the GIL runtime operational state to prevent undetected fallback to GIL-enabled execution.

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 wrap all shared in-memory state (caches, stateful counters, session trackers) with explicit synchronization primitives such as threading.Lock, threading.RLock, or thread-safe atomic data structures.

Engineers MUST wrap all shared in-memory state (caches, stateful counters, session trackers) with explicit synchronization primitives such as threading.Lock, threading.RLock, or thread-safe atomic data structures.

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

FastAPI synchronous route handlers (plain 'def') MUST be evaluated for thread-safety, as AnyIO dispatches them to a ThreadPoolExecutor that executes with true multi-core parallel concurrency under nogil.

FastAPI synchronous route handlers (plain 'def') MUST be evaluated for thread-safety, as AnyIO dispatches them to a ThreadPoolExecutor that executes with true multi-core parallel concurrency under nogil.

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.
6

Components NEVER execute long-running GIL-dependent native routines directly inside AnyIO worker threads, as doing so forces the runtime to re-enable the GIL globally or crash when threads race on unshielded C structs.

Components NEVER execute long-running GIL-dependent native routines directly inside AnyIO worker threads, as doing so forces the runtime to re-enable the GIL globally or crash when threads race on unshielded C structs.

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 nogil) Concurrency & Extension Isolation critical for AI coding assistants?

Enforces strict memory safety and isolation invariants for FastAPI running on Python 3.13 free-threaded builds (nogil). Direct access to legacy, non-thread-safe C-extensions and unsynchronized shared mutable state within free-threaded workers induces silent memory corruption and SIGSEGV faults. 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 (Executing non-nogil-verified C-extensions and unsynchronized shared mutable state in free-threaded FastAPI threadpools) 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