Skip to main content

LoopGuard

Catch event-loop blocking in FastAPI and see which requests were in flight.

PyPI version Python 3.12+ License: MIT

LoopGuard demo: a fast endpoint passes, a blocking endpoint fails with a 503, and the console banner explains why


When something blocks your event loop (via time.sleep(), blocking I/O, or CPU work), LoopGuard detects it and narrows it down to the requests that were in flight when the loop stalled. The sentinel measures loop lag, so it cannot name the single guilty handler — it reports every request that was active during the stall.

Install

pip install fastapi-loopguard

Quick Start

from fastapi import FastAPI
from fastapi_loopguard import LoopGuardMiddleware

app = FastAPI()
app.add_middleware(LoopGuardMiddleware)

Enforcement Modes

Mode Behavior Use Case
"warn" Console warnings + headers Default
"strict" HTTP 503 + error page Development / CI
"log" Silent logging Production
from fastapi_loopguard import LoopGuardConfig

# Development: diagnostic headers on every response
config = LoopGuardConfig(dev_mode=True)

# Development / CI: fail loudly with an educational 503
config = LoopGuardConfig(enforcement_mode="strict")

# Production: silent logging
config = LoopGuardConfig(enforcement_mode="log")

app.add_middleware(LoopGuardMiddleware, config=config)

What You Get

Strict Mode

Returns an educational 503 page that explains what went wrong and how to fix it:

Strict mode error page


Warn Mode

Adds diagnostic headers to every response for debugging:

Warn mode headers


Log Mode

Writes structured logs listing the requests that were in flight:

Console output


Testing AI-Generated Code

AI agents write plausible async code that quietly blocks the event loop. The bundled pytest plugin turns that into a red test and a machine-readable report the agent can fix from — no per-test annotations:

# pytest.ini
[pytest]
loopguard_all_async = true          # every async test fails on blocking
loopguard_report = loopguard.json   # verdicts + fix hints for the agent

See docs/AI-HARNESS.md for the report schema, the no_blocking / allow_blocking markers, and a drop-in snippet for your project's agent instructions.


Full Configuration Reference

Release files for fastapi-loopguard 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-loopguard 0.6.0
File Size Uploaded
fastapi_loopguard-0.6.0.tar.gz 1.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-loopguard 0.6.0
File Interpreter ABI Platform
fastapi_loopguard-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.4 MB

Release files / fastapi_loopguard-0.6.0.tar.gz

Download URL fastapi_loopguard-0.6.0.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
afa892eb19a4943a2541ad34ec484bde2a4ffc6f5860b6559f0f956b10fe004f
BLAKE2b-256 checksum
How to use checksums
54bb11d5a0bb263df200fc9085d1ed03fa1bf987236fdc50fd447f5780b44f4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.

Transparency log

Release files / fastapi_loopguard-0.6.0-py3-none-any.whl

Download URL fastapi_loopguard-0.6.0-py3-none-any.whl
Size 27.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a4c032e0eb6e4fcb3b71833c64276c15ee3e97436c002eb911797e9a162b37b
BLAKE2b-256 checksum
How to use checksums
5ab083e341566446fcf5eaabd111473db5bee7e3466f74af6a79466aa00c4703
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.

Transparency log

Release history Release notifications | RSS feed

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page