Skip to content

Conversation

alexander-alderman-webb
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Sep 22, 2025

Description

Ensure endpoint handler runs before the FastAPI or Starlette integrations read the request body.

As the integrations previously called high-level Starlette Request methods early, the underlying ASGI receive stream was consumed before the user's handler or middleware ran. While the Request object caches the request body, any handler or middleware that relies on low-level stream access still failed.


The event handler that sets request.data is now registered after the HTTP request is handled. The request.data attribute may no longer be attached to events emitted while the request is processed. In particular, if

  • The event is not an exception; or
  • The event is an exception that is caught before reaching the routing layer we patch; or
  • The event is an exception that is not caught but FastAPI/Starlette request body accessors are not invoked before the exception was thrown; or
  • The event is the transaction generated by the FastAPI/Starlette integration for the HTTP request, and the user's request handler never calls FastAPI/Starlette request body accessors.

Note that pydantic also relies on FastAPI/Starlette accessors so request.data may still be available even if the user only implicitly uses the high-level accessors.

Issues

Closes #4764
Closes #4827
Closes PY-1851
Closes PY-1831

Reminders

@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner September 22, 2025 06:56
Copy link

codecov bot commented Sep 22, 2025

Codecov Report

❌ Patch coverage is 95.23810% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.83%. Comparing base (7ae6866) to head (f66bcd6).
⚠️ Report is 31 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/integrations/starlette.py 90.62% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4832      +/-   ##
==========================================
+ Coverage   84.58%   84.83%   +0.25%     
==========================================
  Files         158      158              
  Lines       16521    16595      +74     
  Branches     2867     2877      +10     
==========================================
+ Hits        13974    14079     +105     
+ Misses       1703     1670      -33     
- Partials      844      846       +2     
Files with missing lines Coverage Δ
sentry_sdk/integrations/fastapi.py 89.41% <100.00%> (+0.38%) ⬆️
sentry_sdk/integrations/graphene.py 90.12% <100.00%> (+0.37%) ⬆️
sentry_sdk/integrations/starlette.py 85.37% <90.62%> (+0.35%) ⬆️

... and 39 files with indirect coverage changes

@alexander-alderman-webb alexander-alderman-webb marked this pull request as draft September 22, 2025 07:03
@alexander-alderman-webb
Copy link
Contributor Author

Closing for the reasons in #4764 (comment)

@alexander-alderman-webb
Copy link
Contributor Author

Reopening because there may be the option of changing data fields on transactions in a minor version.

Users with our FastAPI integration are facing empty receive streams when using fastapi_mcp or a custom middleware.

@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review October 6, 2025 13:50
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

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

Successfully merging this pull request may close these issues.

Sentry FastAPI middleware hangs if user middleware raises an exception Requests to fastapi-mcp hangs if Sentry is enabled
1 participant