@@ -360,7 +360,7 @@ class Http2Stream internal constructor(
360
360
readBytesDelivered = readBuffer.read(sink, minOf(byteCount, readBuffer.size))
361
361
readBytesTotal + = readBytesDelivered
362
362
363
- val unacknowledgedBytesRead = ( readBytesTotal - readBytesAcknowledged)
363
+ val unacknowledgedBytesRead = readBytesTotal - readBytesAcknowledged
364
364
if (errorExceptionToDeliver == null &&
365
365
unacknowledgedBytesRead >= connection.okHttpSettings.initialWindowSize / 2 ) {
366
366
// Flow control: notify the peer that we're ready for more data! Only send a
@@ -407,6 +407,10 @@ class Http2Stream internal constructor(
407
407
connection.updateConnectionFlowControl(read)
408
408
}
409
409
410
+ /* *
411
+ * Accept bytes on the connection's reader thread. This function avoids holding locks while it
412
+ * performs blocking reads for the incoming bytes.
413
+ */
410
414
@Throws(IOException ::class )
411
415
internal fun receive (source : BufferedSource , byteCount : Long ) {
412
416
var byteCount = byteCount
@@ -438,14 +442,25 @@ class Http2Stream internal constructor(
438
442
if (read == - 1L ) throw EOFException ()
439
443
byteCount - = read
440
444
441
- // Move the received data to the read buffer to the reader can read it.
445
+ // Move the received data to the read buffer to the reader can read it. If this source has
446
+ // been closed since this read began we must discard the incoming data and tell the
447
+ // connection we've done so.
448
+ var bytesDiscarded = 0L
442
449
synchronized(this @Http2Stream) {
443
- val wasEmpty = readBuffer.size == 0L
444
- readBuffer.writeAll(receiveBuffer)
445
- if (wasEmpty) {
446
- this @Http2Stream.notifyAll()
450
+ if (closed) {
451
+ bytesDiscarded = receiveBuffer.size
452
+ receiveBuffer.clear()
453
+ } else {
454
+ val wasEmpty = readBuffer.size == 0L
455
+ readBuffer.writeAll(receiveBuffer)
456
+ if (wasEmpty) {
457
+ this @Http2Stream.notifyAll()
458
+ }
447
459
}
448
460
}
461
+ if (bytesDiscarded > 0L ) {
462
+ updateConnectionFlowControl(bytesDiscarded)
463
+ }
449
464
}
450
465
}
451
466
0 commit comments