Detect event-loop blocking in FastAPI/Starlette with per-request attribution
Project description
Detect event-loop blocking in FastAPI/Starlette with per-request attribution.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72691b14a3143fcaa026ff26dfceda5191dc9d346c9de5f28fda9bd2fc8998e2
|
|
| MD5 |
ee9f027d58152a90d1faafa7a0c29e86
|
|
| BLAKE2b-256 |
64b2161815a2243711903261284173c5749fdaabe82fe353b3c7457a19e2b36f
|
Provenance
The following attestation bundles were made for fastapi_loopguard-0.3.1.tar.gz:
Publisher:
publish.yml on parhamdavari/fastapi-loopguard
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_loopguard-0.3.1.tar.gz -
Subject digest:
72691b14a3143fcaa026ff26dfceda5191dc9d346c9de5f28fda9bd2fc8998e2 - Sigstore transparency entry: 781100307
- Sigstore integration time:
-
Permalink:
parhamdavari/fastapi-loopguard@b97d09e7110edb5126713b9f044c145da1108131 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/parhamdavari
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b97d09e7110edb5126713b9f044c145da1108131 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_loopguard-0.3.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_loopguard-0.3.1-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98c911c72e7ded62b31ebf8e7e00427bce054b80d1cea5b83a43aa8a0d86e500
|
|
| MD5 |
594226dd1cf4421f3a480e523ce7f1d7
|
|
| BLAKE2b-256 |
dbc4dc556530a3ba2e89006e0d2f929f582540533c41b6772f3a4b1c35f9c11b
|
Provenance
The following attestation bundles were made for fastapi_loopguard-0.3.1-py3-none-any.whl:
Publisher:
publish.yml on parhamdavari/fastapi-loopguard
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_loopguard-0.3.1-py3-none-any.whl -
Subject digest:
98c911c72e7ded62b31ebf8e7e00427bce054b80d1cea5b83a43aa8a0d86e500 - Sigstore transparency entry: 781100308
- Sigstore integration time:
-
Permalink:
parhamdavari/fastapi-loopguard@b97d09e7110edb5126713b9f044c145da1108131 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/parhamdavari
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b97d09e7110edb5126713b9f044c145da1108131 -
Trigger Event:
push
-
Statement type: