Skip to content

fix(okhttp): Don't block on response bodies by reading the body upfront - #6231

Merged
markushi merged 31 commits into
mainfrom
fix/okhttp-unknown-length-response-body
Nov 6, 2026
Merged

markushi merged 31 commits into
mainfrom
fix/okhttp-unknown-length-response-body

Conversation

@markushi

@markushi markushi commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

📜 Description

SentryOkHttpInterceptor captured the response body with peekBody(MAX_NETWORK_BODY_SIZE + 1). A peek
is request(n), which waits for n bytes or for the end of the stream — a body with no
Content-Length delivers neither, so the interceptor never returned and the application never
received the response.

Every response body now goes through the new CapturedResponseBody, which copies the bytes the
application reads into a capped buffer. Nothing is read for the capture itself. The status code and
the headers are recorded before anything is consumed, so a stream that stays open still produces a
usable breadcrumb.

💡 Motivation and Context

With Session Replay network details enabled for a URL, every Server-Sent Events, long-poll or open
chunked endpoint hung forever or failed with a SocketTimeoutException. Gzipped responses were
affected too: OkHttp strips Content-Length when it decompresses, so an application interceptor sees
-1.

Continues carlonzo#1 by @carlonzo

Fixes #6138

💚 How did you test it?

Added unit tests, including a regression test that hangs without the fix.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

carlonzo and others added 14 commits October 6, 2026 13:32
Capturing a response body for session replay peeked it up front with
`Response.peekBody(MAX_NETWORK_BODY_SIZE + 1)`. OkHttp implements a peek as
`request(byteCount)`, which keeps reading until that many bytes are buffered or
the stream ends. A response with no Content-Length never satisfies either
condition, so for server-sent events, long-poll and any chunked endpoint that
stays open the interceptor never returned and the caller never received the
response at all.

Bodies with a known length still end on their own, so they keep the existing
up-front capture. Bodies of unknown length are now wrapped in a body that
copies what the application consumes into a capped buffer and reports it once
no more bytes can arrive: when the stream ends, when the application closes
the body, or when the cap is reached.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Split the capture wrapping into its own function to keep a single return, and
suppress TooManyFunctions on the interceptor as done elsewhere in this module.
A response body has a single consumer, and neither Http1ExchangeCodec.cancel()
nor Http2ExchangeCodec.cancel() closes the body, so nothing reaches this class
from another thread. The AtomicBoolean already guarantees the capture is
reported exactly once.
A streamed body is only known once it has been consumed, which can be after the
NetworkRequestData carrying it was handed to the scope, so the replay thread can
read it while it is still being written. Keeping the three response values
behind one volatile reference means a reader sees either nothing or the
complete set.
@sentry

sentry Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.63.0 (1) release

⚙️ sentry-android Build Distribution Settings

@markushi
markushi marked this pull request as ready for review October 22, 2026 07:43
@markushi markushi changed the title fix(okhttp): Don't block on response bodies with no known length fix(okhttp): Don't block on response bodies by reading the body upfront Oct 22, 2026
@markushi markushi added the sanity-check PR needs a lightweight review for obvious issues label Oct 22, 2026

@0xadam-brown 0xadam-brown left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a bunch for this 💯 ! A few comments from my end below.

Also, out of scope for now, but it looks like there might be a request-side version of this bug, in that the request body is still read into memory before the request is sent (link). Presumably that means streaming, duplex, or large uploads to an allowlisted URL can hang / lose data / trigger an OOM.

Let me know if you want me to open an issue 👍

Comment thread CHANGELOG.md Resolved

@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.

Stale Bugbot comment from a previous run.

@getsentry getsentry deleted a comment from sentry-junior Bot Oct 23, 2026
@getsentry getsentry deleted a comment from sentry-junior Bot Oct 23, 2026
…getsentry/sentry-java into fix/okhttp-unknown-length-response-body

@0xadam-brown 0xadam-brown left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the updates - looking great!

A few possibilities I noticed on a second pass. Otherwise nice all around 👍

@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.

Stale Bugbot comment from a previous run.

@markushi
markushi requested a review from a team as a code owner November 5, 2026 08:43

@runningcode runningcode 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.

LGTM!

@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.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit affe3d6. Configure here.

@markushi
markushi merged commit 1a746d7 into main Nov 6, 2026
49 of 50 checks passed
@markushi
markushi deleted the fix/okhttp-unknown-length-response-body branch November 6, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sanity-check PR needs a lightweight review for obvious issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gate OkHttp body capture on active Session Replay

4 participants