Skip to content

Commit 1a746d7

Browse files
markushicarlonzoclaudesentry-junior[bot]
authored
fix(okhttp): Don't block on response bodies by reading the body upfront (#6231)
* fix(okhttp): Don't block on response bodies of unknown length 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> * changelog: reference the pull request * fix(okhttp): Satisfy detekt for the streaming body capture 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. * refactor(okhttp): Drop unnecessary locking from the capturing source 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. * fix: Publish late response details safely 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. * test: Cover late-filled network request data * fix(okhttp): Harden the capture of response bodies with no known length * test(okhttp): Cover async, cancelled and HTTP/2 bodies of unknown length * ref: Publish both sides of NetworkRequestData the same way * fix(okhttp): Keep the bytes received before a stream breaks * ref: Read the response details of a network breadcrumb as one snapshot * ref(okhttp): Capture every response body through one path * changelog * test: Silence R8 for the okhttp types the new response body extends * ref(okhttp): Rename NetworkBodyCapturingResponseBody to CapturedResponseBody * ref(okhttp): Capture only the response bytes the application reads * test(okhttp): Wait for the capture, not for the source handing bytes over * ref: Keep the status code inside the response details * ref(okhttp): Restore the original response flow and rethrow fatal capture errors * ref(okhttp): Drop comments that restate the code * ref: Drop the constructor comment on ReplayNetworkRequestOrResponse * Address PR comments * address PR comments * test(replay): Pin the response body a late capture leaves out of the replay --------- Co-authored-by: Carlo Marinangeli <carlo.marinangeli@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
1 parent c1967f5 commit 1a746d7

16 files changed

Lines changed: 1355 additions & 80 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
### Fixes
1717

18+
- Session Replay: Fix `SentryOkHttpInterceptor` hanging forever on responses whose body has no known length, such as Server-Sent Events or a gzipped response, when network details are enabled for the URL ([#6231](https://github.com/getsentry/sentry-java/pull/6231))
1819
- Stop marking an OkHttp call span as failed when a connection attempt to one of the host's addresses fails but OkHttp connects on another ([#6472](https://github.com/getsentry/sentry-java/pull/6472))
1920

2021
### Dependencies

‎sentry-android-integration-tests/test-app-size/proguard-rules.pro‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@
4747
-dontwarn kotlin.math.MathKt
4848
-dontwarn okhttp3.EventListener
4949
-dontwarn okhttp3.Interceptor
50+
-dontwarn okhttp3.ResponseBody
51+
-dontwarn okio.ForwardingSource
5052
# Assume all classes are used to not strip them out, e.g. integrations like Compose or Sqlite
5153
-keep class io.sentry.**

‎sentry-android-replay/src/main/java/io/sentry/android/replay/DefaultReplayBreadcrumbConverter.kt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,12 @@ public open class DefaultReplayBreadcrumbConverter() : ReplayBreadcrumbConverter
233233

234234
// Add Network Details data when available
235235
networkDetailData?.let { networkData ->
236+
val responseDetails = networkData.response
237+
236238
networkData.method?.let { breadcrumbData["method"] = it }
237-
networkData.statusCode?.let { breadcrumbData["statusCode"] = it }
239+
responseDetails?.statusCode?.let { breadcrumbData["statusCode"] = it }
238240
networkData.requestBodySize?.let { breadcrumbData["requestBodySize"] = it }
239-
networkData.responseBodySize?.let { breadcrumbData["responseBodySize"] = it }
241+
responseDetails?.size?.let { breadcrumbData["responseBodySize"] = it }
240242

241243
networkData.request?.let { request ->
242244
val requestData = mutableMapOf<String, Any?>()
@@ -257,7 +259,7 @@ public open class DefaultReplayBreadcrumbConverter() : ReplayBreadcrumbConverter
257259
}
258260
}
259261

260-
networkData.response?.let { response ->
262+
responseDetails?.let { response ->
261263
val responseData = mutableMapOf<String, Any?>()
262264
response.size?.let { responseData["size"] = it }
263265
response.body?.let {

‎sentry-android-replay/src/test/java/io/sentry/android/replay/DefaultReplayBreadcrumbConverterTest.kt‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,12 @@ class DefaultReplayBreadcrumbConverterTest {
429429
)
430430
)
431431
fakeOkHttpNetworkDetails.setResponseDetails(
432-
200,
433432
ReplayNetworkRequestOrResponse(
433+
200,
434434
500L,
435435
NetworkBody(mapOf("status" to "success", "message" to "OK")),
436436
mapOf("Content-Type" to "text/plain"),
437-
),
437+
)
438438
)
439439
val hintWithFakeOKHttpNetworkDetails = Hint()
440440
hintWithFakeOKHttpNetworkDetails.set(SENTRY_REPLAY_NETWORK_DETAILS, fakeOkHttpNetworkDetails)
@@ -491,12 +491,12 @@ class DefaultReplayBreadcrumbConverterTest {
491491
)
492492
)
493493
fakeOkHttpNetworkDetails.setResponseDetails(
494-
404,
495494
ReplayNetworkRequestOrResponse(
495+
404,
496496
550L,
497497
NetworkBody(mapOf("status" to "success", "message" to "OK")),
498498
mapOf("Content-Type" to "text/plain"),
499-
),
499+
)
500500
)
501501
val hintWithFakeOKHttpNetworkDetails = Hint()
502502
hintWithFakeOKHttpNetworkDetails.set(SENTRY_REPLAY_NETWORK_DETAILS, fakeOkHttpNetworkDetails)
@@ -528,6 +528,47 @@ class DefaultReplayBreadcrumbConverterTest {
528528
assertEquals(mapOf("Content-Type" to "text/plain"), responseData["headers"])
529529
}
530530

531+
@Test
532+
fun `a response body captured after the breadcrumb was converted does not reach the replay`() {
533+
// Accepted gap: a segment can be cut between the call returning and the app reading the body.
534+
val options = SentryOptions.empty()
535+
val converter = fixture.getSut(options)
536+
val httpBreadcrumb =
537+
Breadcrumb(Date(123L)).apply {
538+
type = "http"
539+
category = "http"
540+
data["url"] = "https://example.com"
541+
data[SpanDataConvention.HTTP_START_TIMESTAMP] = 1000L
542+
data[SpanDataConvention.HTTP_END_TIMESTAMP] = 2000L
543+
}
544+
val networkRequestData = NetworkRequestData("GET")
545+
networkRequestData.setResponseDetails(
546+
ReplayNetworkRequestOrResponse(200, null, null, mapOf("Content-Type" to "application/json"))
547+
)
548+
// Simulates Scopes.addBreadcrumb, which hands the network details to the converter
549+
options.beforeBreadcrumb?.execute(
550+
httpBreadcrumb,
551+
Hint().apply { set(SENTRY_REPLAY_NETWORK_DETAILS, networkRequestData) },
552+
)
553+
554+
val rrwebEvent = converter.convert(httpBreadcrumb)
555+
networkRequestData.setResponseDetails(
556+
ReplayNetworkRequestOrResponse(
557+
200,
558+
21L,
559+
NetworkBody("response body content"),
560+
mapOf("Content-Type" to "application/json"),
561+
)
562+
)
563+
564+
check(rrwebEvent is RRWebSpanEvent)
565+
assertEquals(200, rrwebEvent.data!!["statusCode"])
566+
val responseData = rrwebEvent.data!!["response"] as Map<*, *>
567+
assertEquals(mapOf("Content-Type" to "application/json"), responseData["headers"])
568+
assertNull(responseData["body"])
569+
assertNull(rrwebEvent.data!!["responseBodySize"])
570+
}
571+
531572
@Test
532573
fun `does not convert network details data for non-http breadcrumbs`() {
533574
val navigationBreadcrumb =
@@ -546,12 +587,12 @@ class DefaultReplayBreadcrumbConverterTest {
546587
)
547588
)
548589
networkRequestData.setResponseDetails(
549-
200,
550590
ReplayNetworkRequestOrResponse(
591+
200,
551592
100L,
552593
NetworkBody("response body content"),
553594
mapOf("Content-Type" to "application/json"),
554-
),
595+
)
555596
)
556597
hint.set(SENTRY_REPLAY_NETWORK_DETAILS, networkRequestData)
557598

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package io.sentry.okhttp
2+
3+
import java.io.IOException
4+
import java.util.concurrent.atomic.AtomicBoolean
5+
import okhttp3.MediaType
6+
import okhttp3.ResponseBody
7+
import okio.Buffer
8+
import okio.BufferedSource
9+
import okio.ForwardingSource
10+
import okio.Source
11+
import okio.buffer
12+
13+
/**
14+
* A [ResponseBody] that copies the bytes the application reads into a capped buffer.
15+
*
16+
* @param delegate the body to capture from.
17+
* @param maxBytes the maximum number of bytes to retain; capture stops once it is reached.
18+
* @param onCaptured invoked at most once, as soon as the capture can no longer grow: the cap is
19+
* reached, the stream ends, a read fails, or the body is closed. Runs synchronously on the thread
20+
* that read or closed the body, so it must not block. A body abandoned without any of those never
21+
* reaches it. `isComplete` is false when the bytes are only a prefix of the body.
22+
*/
23+
internal class CapturedResponseBody(
24+
private val delegate: ResponseBody,
25+
private val maxBytes: Long,
26+
private val onCaptured: (bytes: ByteArray, isComplete: Boolean) -> Unit,
27+
) : ResponseBody() {
28+
29+
private val captured = Buffer()
30+
private val capturedOut = captured.outputStream()
31+
32+
private val reported = AtomicBoolean(false)
33+
@Volatile private var bytesRead = 0L
34+
@Volatile private var endOfStream = false
35+
36+
private val capturingSourceLazy = lazy { CapturingSource(delegate.source()).buffer() }
37+
private val capturingSource: BufferedSource by capturingSourceLazy
38+
39+
override fun contentType(): MediaType? = delegate.contentType()
40+
41+
override fun contentLength(): Long = delegate.contentLength()
42+
43+
override fun source(): BufferedSource = capturingSource
44+
45+
override fun close() {
46+
// Closing the delegate notifies listeners, which may serialize the breadcrumb this capture
47+
// belongs to, so report first.
48+
delegate.use {
49+
try {
50+
reportCaptured()
51+
} finally {
52+
// the application may still hold the source, and must not read buffered bytes from it
53+
if (capturingSourceLazy.isInitialized()) {
54+
capturingSource.close()
55+
}
56+
}
57+
}
58+
}
59+
60+
private inner class CapturingSource(source: Source) : ForwardingSource(source) {
61+
override fun read(sink: Buffer, byteCount: Long): Long {
62+
val sinkBefore = sink.size
63+
val read =
64+
try {
65+
super.read(sink, byteCount)
66+
} catch (e: IOException) {
67+
// What arrived is the evidence for this very failure, and a caller handling it is not
68+
// obliged to close the body.
69+
reportCaptured()
70+
throw e
71+
}
72+
73+
if (read > 0L) {
74+
bytesRead += read
75+
if (!reported.get()) {
76+
val capFull =
77+
synchronized(captured) {
78+
// checked again under the lock, so nothing is copied into a drained capture
79+
if (reported.get()) {
80+
return@synchronized false
81+
}
82+
// the application may not have taken everything in the sink yet, hence the offset
83+
val toTake = minOf(maxBytes - captured.size, sink.size - sinkBefore)
84+
if (toTake > 0L) {
85+
sink.copyTo(capturedOut, sinkBefore, toTake)
86+
}
87+
captured.size >= maxBytes
88+
}
89+
if (capFull) {
90+
reportCaptured()
91+
}
92+
}
93+
} else if (read == -1L) {
94+
endOfStream = true
95+
reportCaptured()
96+
}
97+
return read
98+
}
99+
100+
override fun close() {
101+
try {
102+
reportCaptured()
103+
} finally {
104+
super.close()
105+
}
106+
}
107+
}
108+
109+
private fun reportCaptured() {
110+
if (reported.compareAndSet(false, true)) {
111+
val length = delegate.contentLength()
112+
val isComplete = endOfStream || (length >= 0L && bytesRead >= length)
113+
onCaptured(synchronized(captured) { captured.readByteArray() }, isComplete)
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)