Skip to main content

Detect event-loop blocking in FastAPI/Starlette with per-request attribution

Project description

LoopGuard logo

Detect event-loop blocking in FastAPI/Starlette with per-request attribution.


PyPI version Python 3.12+ License: MIT

fastapi-loopguard monitors your event loop and tells you exactly which request caused the blocking. It handles high-concurrency environments with adaptive thresholds and integrates seamlessly with Prometheus.

Quick Start

pip install fastapi-loopguard
from fastapi import FastAPI
from fastapi_loopguard import LoopGuardMiddleware

app = FastAPI()
app.add_middleware(LoopGuardMiddleware)

Features

  • Per-Request Attribution: Pinpoint the exact endpoint causing latency.
  • Cumulative Blocking Detection: Catch "death by a thousand cuts" where frequent small blocks freeze the loop.
  • Adaptive Thresholds: Smart baselines that adjust to server load.
  • Zero-Overhead Sentinel: Cooperative monitoring with negligible CPU usage.
  • Observability Ready: Built-in support for Prometheus and structured logging.

Configuration

LoopGuard is highly configurable to suit your environment:

from fastapi import FastAPI
from fastapi_loopguard import LoopGuardMiddleware, LoopGuardConfig

config = LoopGuardConfig(
    # Basic settings
    monitor_interval_ms=10.0,
    fallback_threshold_ms=50.0,
    
    # Enable Cumulative Blocking Detection
    # Detects frequent small blocks (e.g., 20ms) that sum up to a large freeze
    cumulative_blocking_enabled=True,
    cumulative_blocking_threshold_ms=200.0, # Alert if total block > 200ms...
    cumulative_window_ms=1000.0,            # ...within any 1-second window
)

app = FastAPI()
app.add_middleware(LoopGuardMiddleware, config=config)

Request/Response flows

sequenceDiagram
    autonumber
    actor Client
    participant MW as Middleware<br/>(middleware.py)
    participant Reg as Context Registry<br/>(context.py)
    participant Mon as Sentinel Monitor<br/>(monitor.py)
    participant App as FastAPI App<br/>(Your Code)

    Note over MW, App: Request Flow
    
    Client->>MW: HTTP Request
    activate MW
    
    MW->>Reg: Register Context
    activate Reg
    Reg-->>MW: Done
    deactivate Reg

    MW->>App: Forward Request
    activate App
    
    par Async Monitoring
        Mon->>Reg: Detect Blocking &<br/>Update Contexts
        activate Mon
        activate Reg
        Reg-->>Mon: Updated
        deactivate Reg
        deactivate Mon
    and App Processing
        App-->>MW: Process Complete
        deactivate App
    end

    MW->>Reg: Unregister Context
    activate Reg
    Reg-->>MW: Done
    deactivate Reg

    MW->>Client: Response + Headers
    deactivate MW

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_loopguard-0.3.1.tar.gz (143.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_loopguard-0.3.1-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_loopguard-0.3.1.tar.gz.

File metadata

  • Download URL: fastapi_loopguard-0.3.1.tar.gz
  • Upload date:
  • Size: 143.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_loopguard-0.3.1.tar.gz
Algorithm Hash digest
SHA256 72691b14a3143fcaa026ff26dfceda5191dc9d346c9de5f28fda9bd2fc8998e2
MD5 ee9f027d58152a90d1faafa7a0c29e86
BLAKE2b-256 64b2161815a2243711903261284173c5749fdaabe82fe353b3c7457a19e2b36f

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_loopguard-0.3.1.tar.gz:

Publisher: publish.yml on parhamdavari/fastapi-loopguard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_loopguard-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_loopguard-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98c911c72e7ded62b31ebf8e7e00427bce054b80d1cea5b83a43aa8a0d86e500
MD5 594226dd1cf4421f3a480e523ce7f1d7
BLAKE2b-256 dbc4dc556530a3ba2e89006e0d2f929f582540533c41b6772f3a4b1c35f9c11b

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_loopguard-0.3.1-py3-none-any.whl:

Publisher: publish.yml on parhamdavari/fastapi-loopguard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page