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

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.1
File Size Uploaded
fastapi_loopguard-0.6.1.tar.gz 1.4 MB Details

Built distribution (wheel)

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

Total release size: 1.5 MB

Release files / fastapi_loopguard-0.6.1.tar.gz

Download URL fastapi_loopguard-0.6.1.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
c20dcad293a191fa595d49dbe3e91b38636f7268fe63cecb8ca7a1f36bf8ed4d
BLAKE2b-256 checksum
How to use checksums
07b8bacebb80ac31cdb662f5add8689d916e128b1dde1bb7057714c38c977cf5
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.1-py3-none-any.whl

Download URL fastapi_loopguard-0.6.1-py3-none-any.whl
Size 27.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2b2a43c806130c7e6c6a681f1f83f2178bbfc4b397025797c19d74297347921
BLAKE2b-256 checksum
How to use checksums
1eb89479acbc05e6ae9f42a33fdfeed9bc23f3ecc91fdc4e1fdd8fdc60fd79b7
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

This release

0.6.1 This release

2 release files

0.6.0

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