Skip to content

Commit 79fe925

Browse files
dryganetsfacebook-github-bot
authored andcommitted
Android. OkHttp crash fixed on upload re-try. (#20802)
Summary: This change fixes this issue: #10423 Pull Request resolved: #20802 Differential Revision: D9478422 Pulled By: hramos fbshipit-source-id: ce3a098a71c8f50a62b011a2a277004ab7a7b655
1 parent 43cd6f7 commit 79fe925

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressRequestBody.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class ProgressRequestBody extends RequestBody {
1919

2020
private final RequestBody mRequestBody;
2121
private final ProgressListener mProgressListener;
22-
private BufferedSink mBufferedSink;
2322
private long mContentLength = 0L;
2423

2524
public ProgressRequestBody(RequestBody requestBody, ProgressListener progressListener) {
@@ -42,16 +41,17 @@ public long contentLength() throws IOException {
4241

4342
@Override
4443
public void writeTo(BufferedSink sink) throws IOException {
45-
if (mBufferedSink == null) {
46-
mBufferedSink = Okio.buffer(outputStreamSink(sink));
47-
}
44+
// In 99% of cases, this method is called strictly once.
45+
// The only case when it is called more than once is internal okhttp upload re-try.
46+
// We need to re-create CountingOutputStream in this case as progress should be re-evaluated.
47+
BufferedSink sinkWrapper = Okio.buffer(outputStreamSink(sink));
4848

4949
// contentLength changes for input streams, since we're using inputStream.available(),
5050
// so get the length before writing to the sink
5151
contentLength();
5252

53-
mRequestBody.writeTo(mBufferedSink);
54-
mBufferedSink.flush();
53+
mRequestBody.writeTo(sinkWrapper);
54+
sinkWrapper.flush();
5555
}
5656

5757
private Sink outputStreamSink(BufferedSink sink) {

0 commit comments

Comments
 (0)