question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] A running background task will block new requests from the OpenAPI view when using a metrics exporter middleware

See original GitHub issue

Describe the bug

To Reproduce

Steps to reproduce the behavior with a minimum self-contained file.

Replace each part with your own scenario:

  1. 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!"
  1. Open the OpenAPI docs view and click “Try It Now”.
  2. Send a request with 10 seconds (or something else but make it long enough).
  3. The response will return immediately.
  4. 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:open
  • Created 3 years ago
  • Reactions:15
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
sebr74commented, May 7, 2020

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.

2reactions
jacopofarcommented, Jul 24, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >
Ecosystem - Fastify
Fast and low overhead web framework, for Node.js.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found