Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware is preventing background task to execute in background #33

Open
Colin-b opened this issue Dec 8, 2021 · 1 comment
Open

Comments

@Colin-b
Copy link

Colin-b commented Dec 8, 2021

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
Copy link
Owner

perdy commented Jan 12, 2022

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:

  • Run the background tasks using multiprocessing to avoid blocking the main process.
  • Someone mentioned in encode/starlete#919 that using hypercorn the problem isn't happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants