Skip to content

Commit

Permalink
Merge pull request square#5436 from square/jwilson.0909.cherry_pick_h…
Browse files Browse the repository at this point in the history
…ttp2

Don't leak incoming bytes when we race incoming data and close (3.14.x branch)
  • Loading branch information
squarejesse authored Sep 10, 2019
2 parents 8af53d5 + eb053df commit 55f9d53
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions okhttp/src/main/java/okhttp3/internal/http2/Http2Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ private void updateConnectionFlowControl(long read) {
connection.updateConnectionFlowControl(read);
}

/**
* Accept bytes on the connection's reader thread. This function avoids holding locks while it
* performs blocking reads for the incoming bytes.
*/
void receive(BufferedSource in, long byteCount) throws IOException {
assert (!Thread.holdsLock(Http2Stream.this));

Expand Down Expand Up @@ -464,14 +468,25 @@ void receive(BufferedSource in, long byteCount) throws IOException {
if (read == -1) throw new EOFException();
byteCount -= read;

// Move the received data to the read buffer to the reader can read it.
// Move the received data to the read buffer to the reader can read it. If this source has
// been closed since this read began we must discard the incoming data and tell the
// connection we've done so.
long bytesDiscarded = 0L;
synchronized (Http2Stream.this) {
boolean wasEmpty = readBuffer.size() == 0;
readBuffer.writeAll(receiveBuffer);
if (wasEmpty) {
Http2Stream.this.notifyAll();
if (closed) {
bytesDiscarded = receiveBuffer.size();
receiveBuffer.clear();
} else {
boolean wasEmpty = readBuffer.size() == 0;
readBuffer.writeAll(receiveBuffer);
if (wasEmpty) {
Http2Stream.this.notifyAll();
}
}
}
if (bytesDiscarded > 0L) {
updateConnectionFlowControl(bytesDiscarded);
}
}
}

Expand Down

0 comments on commit 55f9d53

Please sign in to comment.