Skip to main content

🔍 FastAPI Block Hunter

This is a tool to help you debug blocking calls in your FastAPI application.

It works by:

  • 🔄 Adding a middleware to the FastAPI application that will store the current request URL in a context variable.
  • 🛠️ Patching the _format_handle function from the asyncio.base_events module with a custom formatter to print a custom message when a blocking call is detected.

This allows you to identify the blocking call that has blocked the event loop and print the stack trace of the blocking call.

Sample logs showing blocking call detection

📦 Installation

pip install fastapi-block-hunter

🚀 Usage

To use the package, you need to add the middleware to your FastAPI application and set up the asyncio debug mode.

from fastapi import FastAPI
from fastapi_block_hunter import add_block_hunter_middleware, setup_asyncio_debug_mode

# You may set up the asyncio debug mode by defining ASYNgCIO_DEBUG_MODE=1 as an environment variable.
# Or by calling the setup_asyncio_debug_mode function in your app lifespan

@asynccontextmanager
async def lifespan(app: FastAPI):
    await setup_asyncio_debug_mode()
    yield {}


app = FastAPI(lifespan=lifespan)
app = add_block_hunter_middleware(app, verbose=True)

You also need to make sure you use asyncio as your FastAPI event loop for the package to work. If you have uvloop installed, you will need to manually tell your web server to use asyncio instead of uvloop.

For uvicorn, set the --loop option to asyncio.

From the uvicorn command line:

uvicorn --loop asyncio # rest of your startup command

Or from your launch script:

from fastapi import FastAPI
    import uvicorn

app = FastAPI()

# ... your FastAPI app setup

if __name__ == "__main__":
    uvicorn.run(app, loop="asyncio")

Or from the code:

from fastapi import FastAPI
from fastapi_block_hunter import add_custom_slow_callback_logger

app = FastAPI()
app = add_custom_slow_callback_logger(app)

📚 Documentation

A complete example of how to use the package can be found in the example/app.py file.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for fastapi-block-hunter 0.0.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-block-hunter 0.0.1
File Size Uploaded Provenance Actions
fastapi_block_hunter-0.0.1.tar.gz 3.9 kB Without provenance Details for fastapi_block_hunter-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-block-hunter 0.0.1
File Interpreter ABI Platform Actions
fastapi_block_hunter-0.0.1-py3-none-any.whl Python 3 none any Details for fastapi_block_hunter-0.0.1-py3-none-any.whl

Total release size: 8.8 kB

Release files / fastapi_block_hunter-0.0.1.tar.gz

Download URL fastapi_block_hunter-0.0.1.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e93a08de335cbfa6be7653d38ffcf1c1672ccc8a5a377e5780974cd0804d14db
BLAKE2b-256 checksum
How to use checksums
3558be9ebc908b6c374569c23193232f003a4a79cda98e9f4ea237fbfa293997
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / fastapi_block_hunter-0.0.1-py3-none-any.whl

Download URL fastapi_block_hunter-0.0.1-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f680613aa1b039179b4cab4d723a37f4620990be53d34866b375b0a3f4b1751e
BLAKE2b-256 checksum
How to use checksums
8879f2622ae81c0205e4ab8270bb9654fe588c8fbf8ab1c469454f914a1c5a46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Security for fastapi-block-hunter 0.0.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

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