Skip to content

fix(starlette): Stop eagerly consuming request bodies for streamed spans#6282

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

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

Conversation

@alexander-alderman-webb
Copy link
Copy Markdown
Contributor

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

Note: Depends on #6269

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

@alexander-alderman-webb alexander-alderman-webb changed the title fix(starlette): Do not attach eagerly consumed request bodies on stre… fix(starlette): Do not attach eagerly consumed request bodies on streamed spans in async handlers May 18, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 18, 2026

Codecov Results 📊

282 passed | Total: 282 | Pass Rate: 100% | Execution Time: 42.63s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 6.45%. Project has 14800 uncovered lines.
❌ Project coverage is 33.6%. Comparing base (base) to head (head).

Files with missing lines (2)
File Patch % Lines
starlette.py 5.49% ⚠️ 396 Missing
consts.py 99.49% ⚠️ 2 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    33.62%    33.60%    -0.02%
==========================================
  Files          190       190         —
  Lines        22270     22288       +18
  Branches      7546      7544        -2
==========================================
+ Hits          7487      7488        +1
- Misses       14783     14800       +17
- Partials       747       747         —

Generated by Codecov Action

@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review May 18, 2026 07:50
@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner May 18, 2026 07:50
@alexander-alderman-webb alexander-alderman-webb changed the title fix(starlette): Do not attach eagerly consumed request bodies on streamed spans in async handlers fix(starlette): Do not attach eagerly consumed request bodies for streamed spans in async handlers May 18, 2026
Comment thread sentry_sdk/integrations/starlette.py Outdated
Comment thread sentry_sdk/integrations/starlette.py Outdated
@alexander-alderman-webb alexander-alderman-webb marked this pull request as draft May 18, 2026 08:01
Comment thread sentry_sdk/integrations/starlette.py Outdated
@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review May 18, 2026 08:44
Comment thread tests/integrations/starlette/test_starlette.py Outdated
@alexander-alderman-webb alexander-alderman-webb marked this pull request as draft May 18, 2026 08:47
@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review May 18, 2026 09:08
@alexander-alderman-webb alexander-alderman-webb changed the title fix(starlette): Do not attach eagerly consumed request bodies for streamed spans in async handlers fix(starlette): Stop eagerly consuming request bodies for streamed spans in async handlers May 18, 2026
@alexander-alderman-webb alexander-alderman-webb changed the title fix(starlette): Stop eagerly consuming request bodies for streamed spans in async handlers fix(starlette): Stop eagerly consuming request bodies for streamed spans May 18, 2026
Comment thread sentry_sdk/integrations/starlette.py Outdated
Copy link
Copy Markdown

@cursor cursor Bot left a comment

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 9d2157e. Configure here.

Comment thread sentry_sdk/integrations/starlette.py
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