Skip to content

fix(fastapi): Stop eagerly consuming request bodies for streamed spans#6286

Open
alexander-alderman-webb wants to merge 7 commits into
webb/fastapi/request-extractor-testsfrom
webb/fastapi/request-body-async
Open

fix(fastapi): Stop eagerly consuming request bodies for streamed spans#6286
alexander-alderman-webb wants to merge 7 commits into
webb/fastapi/request-extractor-testsfrom
webb/fastapi/request-body-async

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented May 18, 2026

Copy link
Copy Markdown
Contributor

Note: Depends on #6287

Description

Only attach cached request bodies to streamed spans to avoid relying on an eagerly consumed request body. Use the _json and _form attributes instead of json() and form() accessors to ensure that the request body is not consumed by the SDK.

Note: by using _json and _form directly we can no longer distinguish between raw data payloads and payloads that were not cached on the Request object. The attribute is therefore omitted if the request body is not cached, since the endpoint may not have accessed it.

The integration can still cause application hangs if middleware or handlers deprive the receive() callable instead of using Starlette accessors. Reliance on the overly ambitious request body access is only eliminated for streamed spans (and only persists for errors with the change).

  • Remove test_request_body_data_does_not_scrub_pii_span_streaming() since test_formdata_request_body() already asserts that fields in the request body are not scrubbed.
  • Remove test_request_body_data_annotated_value_top_level_span_streaming() because the attribute is no longer set if the request body is not JSON or FormData.

Issues

Reminders

@github-actions

github-actions Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

88711 passed | ⏭️ 6013 skipped | Total: 94724 | Pass Rate: 93.65% | Execution Time: 301m 8s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -77
Passed Tests 📉 -65
Failed Tests
Skipped Tests 📉 -12

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 2365 uncovered lines.
❌ Project coverage is 89.87%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    89.90%    89.87%    -0.03%
==========================================
  Files          192       192         —
  Lines        23324     23352       +28
  Branches      8020      8024        +4
==========================================
+ Hits         20969     20987       +18
- Misses        2355      2365       +10
- Partials      1327      1326        -1

Generated by Codecov Action

@alexander-alderman-webb alexander-alderman-webb changed the base branch from webb/starlette/request-body-async to webb/fastapi/request-extractor-tests May 19, 2026 07:03
@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review May 19, 2026 07:38
@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner May 19, 2026 07:38

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cd39b28. Configure here.

Comment thread sentry_sdk/integrations/fastapi.py
Comment thread sentry_sdk/integrations/fastapi.py
Comment thread sentry_sdk/integrations/fastapi.py

@sentrivana sentrivana left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

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.

2 participants