-
Notifications
You must be signed in to change notification settings - Fork 533
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
Problem accessing to the request body after configure Sentry in FastAPI #1573
Comments
Routing to @getsentry/team-web-sdk-backend for triage. ⏲️ |
Thanks for submitting this issue @LuisMlopez. Our FastAPI integration is still in alpha so this feedback is very valuable! We will look into this in about two weeks time. |
Hey @LuisMlopez ! This is because you can only read the request body once, because in Starlette the request body is a stream. This is why Starlette is caching the body in a local variable But the I do not yet have an idea how to fix this. Let me thing about it (and ask in the Starlette repo) |
Hello again @LuisMlopez I have now found out, that the application hanging when one tries to read the request body twice (for example once with sentry and then again witha in a middleware, as in our example above) is a known limitation. See: encode/starlette#1729 The middleware in our example is base upon Would Starlettes But I guess right now, there is nothing we can do about. There are discussions going on in the Starlette project on how to fix this limitation with |
I have now asked for help in the Starlette community, maybe somebody has an idea on how to solve this: |
This is an issue on Sentry's side. We'll explain, and propose some solution(s). cc @adriangb EDIT: @adriangb explained the possible solutions: encode/starlette#1729 (reply in thread) |
Thanks for the feedback @Kludex. I will have a look! |
Hi @antonpirker Thanks a lot for all the information. I have found a temporal solution to use sentry and the logging middleware together. I know that is not the best approach but it works and can be used while the problem reported here is not solved:
All the exceptions will be logged in Sentry "manually", so the Sentry setup described here https://docs.sentry.io/platforms/python/guides/fastapi/ is not required. I hope this solution can help others with the same problem. |
@antonpirker I just gave a look at the Sentry middleware again to try to decipher exactly what's going on. I originally though that Sentry was reading the request/response in a middleware, but I'm realizing now it's doing that at the endpoint level. I think this means the order of execution is |
I think this issue is also solved by fixing this one: #1631 |
(We read the request body for determining the size of it. (Which is really a stupid thing to do). Now we read the content length from the request headers and therefore do not touch the request body twice) |
And if the request doesn't have a |
In that case we just ignore the request body, because we can not be sure to not break the users code when accessing it. |
Steps to Reproduce
Expected Result
Simplified middleware example
Actual Result
When the Sentry implementation is active, the
body = await request.body()
line hangs and never response.Additional Info
This answer has more information related to the problem https://stackoverflow.com/a/71811390/11030205
The text was updated successfully, but these errors were encountered: