Skip to content

fix(sentry): Bind hub to response bodies via middleware#526

Open
jan-auer wants to merge 2 commits into
mainfrom
fix/response-stream-hub-body
Open

fix(sentry): Bind hub to response bodies via middleware#526
jan-auer wants to merge 2 commits into
mainfrom
fix/response-stream-hub-body

Conversation

@jan-auer

@jan-auer jan-auer commented Jun 26, 2026

Copy link
Copy Markdown
Member

When an axum handler returns a streaming body via Body::from_stream(), the IntoResponse conversion runs inside the handler (within the sentry middleware), but the body's frames are polled by hyper after middleware has unwound. This means Hub::current() inside a stream producer resolves to whatever hub the tokio worker thread happens to have — not the request's hub.

Three endpoints are affected: object_get, complete (multipart), and batch.

Instead of wrapping individual streams per-endpoint, this PR introduces a middleware that wraps every response body in a SentryBody that re-activates the request's hub on each poll_frame call. This still allows for plain bodies to provide a content-length. It effectively double-boxes every body, but the overhead of the indirection is negligible.

Alternative to #524.

jan-auer and others added 2 commits June 26, 2026 13:54
Axum stream bodies are polled by hyper after the middleware stack
unwinds, so Hub::current() inside a stream producer resolves to the
tokio worker thread's ambient hub rather than the request's hub.

Introduce SentryStream<S>, a pin-projected stream wrapper that
re-activates a captured Arc<Hub> via HubSwitchGuard on every
poll_next call, mirroring how SentryFuture works for futures.

Bind the hub at stream construction time in the two affected
endpoints: complete (multipart) and batch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace per-endpoint SentryStream wrapping with a centralized
middleware that operates at the http_body::Body level instead of
the Stream level, preserving size hints for buffered responses.
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.38%. Comparing base (824c05a) to head (b1c897f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #526      +/-   ##
==========================================
+ Coverage   87.35%   87.38%   +0.02%     
==========================================
  Files          87       88       +1     
  Lines       14086    14111      +25     
==========================================
+ Hits        12305    12331      +26     
+ Misses       1781     1780       -1     
Components Coverage Δ
Rust Backend 92.16% <100.00%> (+0.01%) ⬆️
Rust Client 79.89% <ø> (ø)
Python Client 88.70% <ø> (ø)

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant