perdy / starlette-prometheus

Prometheus integration for Starlette.
GNU General Public License v3.0
274 stars 31 forks source link

Middleware is preventing background task to execute in background #33

Open Colin-b opened 2 years ago

Colin-b commented 2 years ago

Hello,

Just letting you know that your middleware is impacted by this issue in Starlette.

I did not investigate much but it seems that you could fix it without waiting for a fix in Starlette (if you feel like it of course).

Best Regards

perdy commented 2 years ago

I'm afraid it's not possible to avoid this bug in this library because of the intrinsic nature of the Middleware logic.

Summarizing a bit the issue: it's causing that HTTP responses are not sent until all background tasks are finished due to responses are awaited in middlewares (there is a stack of middlewares so everyone will await the next one) and background tasks are executed in the same thread and process.

For our case it's not possible to do any other thing because we need the response itself to extract some data, such as execution time, status code, etc.

We have to wait how the issue is resolved in Starlette and meanwhile: