-
Notifications
You must be signed in to change notification settings - Fork 39
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
supertokens FastAPI middleware uses BaseHTTPMiddleware which breaks async/background tasks #343
Comments
Thanks for opening this issue @atambo . We will have a look at this in the coming week. |
Seems like starlette was thinking about deprecating However, it still seems like the recommended approach is to use a pure ASGI middleware. |
Hi @atambo, Thanks for pointing this out. I quickly tried to replace Keeping this issue open since it's valid. We will try to get resolve this as soon as we have time. In the meantime, I can help you with finding a workaround for your use case if you provide more details :) |
Hi, this is causing issues for me currently, I'd like to run an async function in a BackgroundTask but having the SuperTokens middleware is blocking my main event loop. I need something like the following:
|
@AngusParsonson I tried your code, it seems to be working for me and is able to run the async tasks. What's the error that your're seeing on your machine? |
Hi @KShivendu, thanks for the response. There is no error, it is just that the main event loop is blocked whilst the task is running. If you try to hit a simple get endpoint whilst the background task is running, then it will not complete until the background task is finished. This is an issue with starlette, the way I got around it was by using asyncio.create_task() instead of the background task. |
Ahh yes. I'll try to dive into this and find a clean solution sometime soon. Good to know that you already found a workaround in the meantime. I believe it's sufficient to solve your problem for now? |
See the starlette issue as well as the limitations section of the documentation.
It seems like using
BaseHTTPMiddleware
breaks some things with async/background tasks in FastAPI and people generally recommend switching your middleware to be pure ASGI middleware for maximum compatibility.Unfortunately, I've been noticing this issue ever since I integrated the supertokens middleware into my application.
The text was updated successfully, but these errors were encountered: