[BUG] A running background task will block new requests from the OpenAPI view when using a metrics exporter middleware
See original GitHub issueDescribe the bug
To Reproduce
Steps to reproduce the behavior with a minimum self-contained file.
Replace each part with your own scenario:
- Create a file with:
from time import sleep
from fastapi import FastAPI, BackgroundTasks
from starlette_exporter import PrometheusMiddleware, handle_metrics
app = FastAPI()
app.add_middleware(PrometheusMiddleware)
app.add_route("/metrics", handle_metrics)
def task(seconds):
print("Starting task, sleep", seconds)
sleep(seconds)
print("Done task, sleep", seconds)
@app.get("/{seconds}")
async def test(seconds: int, background_tasks: BackgroundTasks):
print(f"In route with seconds={seconds}")
background_tasks.add_task(task, seconds=seconds)
return f"{seconds} hello world!"
- Open the OpenAPI docs view and click “Try It Now”.
- Send a request with 10 seconds (or something else but make it long enough).
- The response will return immediately.
- Immediately send another request and the request will block until the backgroud task will finish.
Expected behavior
Requests shouldn’t be blocked by background tasks
Screenshots
If applicable, add screenshots to help explain your problem.
Environment
- OS: Linux
- FastAPI Version 0.52.0
- Python version, 3.7.7
Additional context
At first, I thought about opening this issue in starlette-prometheus package but then I tried it with starlette-exporter as you can see in the example and it still happens. I don’t know why is it reproducing with the OpenAPI view but not via curl
and I don’t have the knowledge to investigate this issue.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:15
- Comments:15 (2 by maintainers)
Top Results From Across the Web
Bountysource
[BUG] A running background task will block new requests from the OpenAPI view when using a metrics exporter middleware.
Read more >Prometheus Metrics: A Practical Guide - Tigera
Learn about Prometheus metrics types, using metrics in common use cases, understanding function and operators, and how to work with exporters.
Read more >What's New for Oracle Integration Generation 2
This user does not have access to Integrations, B2B, File Server, Visual Builder, adapters, recipes, or accelerators. In Processes, a user with the ......
Read more >OpenShift Container Platform 4.8 release notes
Built on Red Hat Enterprise Linux (RHEL) and Kubernetes, OpenShift Container Platform provides a more secure and scalable multi-tenant operating system for ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have hit a similar issue, but I think this bug is actually a specific case of https://github.com/encode/starlette/issues/919. So a Starlette bug, not a FastAPI bug.
Same problem here, with the GZipMiddleware enabled the request is blocked until the background task finishes. If I disable it it works as expected.
The curious thing is that the log shows that the request was correctly processed, so I initially assumed it was a reverse proxy problem.