Repository navigation
fix(okhttp): Don't block on response bodies by reading the body upfront #6231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
markushi
wants to merge
26
commits into
main
Choose a base branch
from
fix/okhttp-unknown-length-response-body
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,203
โ79
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
62cff9b
fix(okhttp): Don't block on response bodies of unknown length
carlonzo 4d7c686
changelog: reference the pull request
carlonzo 554d121
fix(okhttp): Satisfy detekt for the streaming body capture
carlonzo 0d5e6df
refactor(okhttp): Drop unnecessary locking from the capturing source
carlonzo a982ceb
fix: Publish late response details safely
carlonzo 1c6174f
test: Cover late-filled network request data
carlonzo 9377594
fix(okhttp): Harden the capture of response bodies with no known length
markushi a93c694
test(okhttp): Cover async, cancelled and HTTP/2 bodies of unknown length
markushi b77f96a
ref: Publish both sides of NetworkRequestData the same way
markushi 17d5134
fix(okhttp): Keep the bytes received before a stream breaks
markushi f9e79c0
ref: Read the response details of a network breadcrumb as one snapshot
markushi 5b2bf58
ref(okhttp): Capture every response body through one path
markushi 8879de2
changelog
markushi 09687ef
test: Silence R8 for the okhttp types the new response body extends
markushi ead659c
ref(okhttp): Rename NetworkBodyCapturingResponseBody to CapturedRespoโฆ
markushi 8193c36
ref(okhttp): Capture only the response bytes the application reads
markushi 406bd4e
Merge branch 'main' into fix/okhttp-unknown-length-response-body [pr-โฆ
sentry-junior[bot] d490996
Merge branch 'main' into fix/okhttp-unknown-length-response-body [pr-โฆ
sentry-junior[bot] 6a5a086
test(okhttp): Wait for the capture, not for the source handing bytes โฆ
markushi b406d8b
ref: Keep the status code inside the response details
markushi ab94b11
ref(okhttp): Restore the original response flow and rethrow fatal capโฆ
markushi 7c50f57
ref(okhttp): Drop comments that restate the code
markushi fa85e61
ref: Drop the constructor comment on ReplayNetworkRequestOrResponse
markushi 4dbcbfa
Merge branch 'main' into fix/okhttp-unknown-length-response-body [pr-โฆ
sentry-junior[bot] c875cd0
Address PR comments
markushi 2bc6c4f
Merge branch 'fix/okhttp-unknown-length-response-body' of github.com:โฆ
markushi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
sentry-okhttp/src/main/java/io/sentry/okhttp/CapturedResponseBody.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package io.sentry.okhttp | ||
|
|
||
| import java.io.IOException | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
| import okhttp3.MediaType | ||
| import okhttp3.ResponseBody | ||
| import okio.Buffer | ||
| import okio.BufferedSource | ||
| import okio.ForwardingSource | ||
| import okio.Source | ||
| import okio.buffer | ||
|
|
||
| /** | ||
| * A [ResponseBody] that copies the bytes the application reads into a capped buffer. | ||
| * | ||
| * @param delegate the body to capture from. | ||
| * @param maxBytes the maximum number of bytes to retain; capture stops once it is reached. | ||
| * @param onCaptured invoked at most once, as soon as the capture can no longer grow: the cap is | ||
| * reached, the stream ends, a read fails, or the body is closed. Runs synchronously on the thread | ||
| * that read or closed the body, so it must not block. A body abandoned without any of those never | ||
| * reaches it. | ||
| */ | ||
| internal class CapturedResponseBody( | ||
| private val delegate: ResponseBody, | ||
| private val maxBytes: Long, | ||
| private val onCaptured: (ByteArray) -> Unit, | ||
| ) : ResponseBody() { | ||
|
|
||
| private val captured = Buffer() | ||
| private val capturedOut = captured.outputStream() | ||
|
|
||
| private val reported = AtomicBoolean(false) | ||
|
|
||
| private val capturingSource: BufferedSource by lazy { | ||
| CapturingSource(delegate.source()).buffer() | ||
| } | ||
|
|
||
| override fun contentType(): MediaType? = delegate.contentType() | ||
|
|
||
| override fun contentLength(): Long = delegate.contentLength() | ||
|
|
||
| override fun source(): BufferedSource = capturingSource | ||
|
|
||
| override fun close() { | ||
| // Closing the delegate notifies listeners, which may serialize the breadcrumb this capture | ||
| // belongs to, so report first. | ||
| delegate.use { reportCaptured() } | ||
| } | ||
|
|
||
| private inner class CapturingSource(source: Source) : ForwardingSource(source) { | ||
| override fun read(sink: Buffer, byteCount: Long): Long { | ||
| val sinkBefore = sink.size | ||
| val read = | ||
| try { | ||
| super.read(sink, byteCount) | ||
| } catch (e: IOException) { | ||
| // What arrived is the evidence for this very failure, and a caller handling it is not | ||
| // obliged to close the body. | ||
| reportCaptured() | ||
| throw e | ||
| } | ||
|
|
||
| if (read > 0L) { | ||
| val capFull = | ||
| synchronized(captured) { | ||
| // the application may not have taken everything in the sink yet, hence the offset | ||
| val toTake = minOf(maxBytes - captured.size, sink.size - sinkBefore) | ||
| if (toTake > 0L) { | ||
| sink.copyTo(capturedOut, sinkBefore, toTake) | ||
| } | ||
| captured.size >= maxBytes | ||
| } | ||
| if (capFull) { | ||
| reportCaptured() | ||
| } | ||
| } else if (read == -1L) { | ||
| reportCaptured() | ||
| } | ||
| return read | ||
| } | ||
|
|
||
| override fun close() { | ||
| try { | ||
| reportCaptured() | ||
| } finally { | ||
| super.close() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun reportCaptured() { | ||
| if (reported.compareAndSet(false, true)) { | ||
| onCaptured(synchronized(captured) { captured.clone().readByteArray() }) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.