Avoid emitting WINDOW_UPDATE frames when receiving stream closed (#233)

Motivation:

In some cases it's possible for a stream to emit a WINDOW_UPDATE frame
after receiving close from the network. This is unfortunate because in
the eyes of the `NIOHTTP2Handler` the stream no longer exists which
results in a connection-level error.

Modifications:

- Add a `close()` to the `InboundWindowManager`, no new frame sizes will
  be produced if close has been called
- Use the above functionality when the stream is closing and when we
  have seen end stream.

Result:

WINDOW_UPDATE frames are no longer emitted from the stream as a result
of delivering pending reads while closing.
This commit is contained in:
George Barnett authored and GitHub committed 2020-08-24 15:13:19 +01:00
1 parent 9f4eff0603
commit 1e68e51752
6 files changed
+82 -2

No files matched your search

+18 -2
View File
@@ -630,11 +630,24 @@ private extension HTTP2StreamChannel {
while self.pendingReads.count > 0 {
let frame = self.pendingReads.removeFirst()
let endStream: Bool
let dataLength: Int?
if case .data(let data) = frame.payload {
switch frame.payload {
case .data(let data):
endStream = data.endStream
dataLength = data.data.readableBytes
} else {
case .headers(let headers):
endStream = headers.endStream
dataLength = nil
default:
endStream = false
dataLength = nil
}
// We've seen end stream: close the window manager to avoid emitting extraneous WINDOW_UPDATE frames.
if endStream {
self.windowManager.closed = true
}
self.pipeline.fireChannelRead(NIOAny(frame))
@@ -724,6 +737,9 @@ internal extension HTTP2StreamChannel {
/// - parameters:
/// - reason: The reason received from the network, if any.
func receiveStreamClosed(_ reason: HTTP2ErrorCode?) {
// Avoid emitting any WINDOW_UPDATE frames now that we're closed.
self.windowManager.closed = true
// The stream is closed, we should aim to deliver any read frames we have for it.
self.tryToRead()
@@ -25,6 +25,9 @@ struct InboundWindowManager {
/// The number of bytes of buffered frames.
private var bufferedBytes: Int = 0
/// Whether the inbound window is closed. If this is true then no window size changes will be emitted.
var closed = false
init(targetSize: Int32) {
assert(targetSize <= HTTP2FlowControlWindow.maxSize)
assert(targetSize >= 0)
@@ -68,6 +71,11 @@ struct InboundWindowManager {
}
private func calculateWindowIncrement(windowSize: Int) -> Int? {
// No point calculating an increment if we're closed.
if self.closed {
return nil
}
// The simplest case is where newSize >= targetWindowSize. In that case, we do nothing.
//
// The next simplest case is where 0 <= newSize < targetWindowSize. In that case,
@@ -75,6 +75,7 @@ extension HTTP2FramePayloadStreamMultiplexerTests {
("testInboundChannelWindowSizeIsCustomisable", testInboundChannelWindowSizeIsCustomisable),
("testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer", testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer),
("testReadWhenUsingAutoreadOnChildChannel", testReadWhenUsingAutoreadOnChildChannel),
("testWindowUpdateIsNotEmittedAfterStreamIsClosed", testWindowUpdateIsNotEmittedAfterStreamIsClosed),
]
}
}
@@ -1805,4 +1805,47 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase {
XCTAssertNoThrow(try self.channel.finish())
}
func testWindowUpdateIsNotEmittedAfterStreamIsClosed() throws {
let targetWindowSize = 1024
let multiplexer = HTTP2StreamMultiplexer(mode: .client,
channel: self.channel,
targetWindowSize: targetWindowSize) { channel in
return channel.eventLoop.makeSucceededFuture(())
}
XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait())
// We need to activate the underlying channel here.
XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 80)).wait())
// Write a headers frame.
let headers = HPACKHeaders([(":path", "/"), (":method", "GET"), (":authority", "localhost"), (":scheme", "https")])
let headersFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: headers)))
self.channel.pipeline.fireChannelRead(NIOAny(headersFrame))
// Activate the stream.
self.activateStream(1)
// Send a window updated event.
let windowUpdated = NIOHTTP2WindowUpdatedEvent(streamID: 1, inboundWindowSize: 128, outboundWindowSize: nil)
self.channel.pipeline.fireUserInboundEventTriggered(windowUpdated)
self.channel.pipeline.fireChannelReadComplete()
// We expect the a WINDOW_UPDATE frame: our inbound window size is 128 but has a target of 1024.
let frame = try assertNoThrowWithValue(try self.channel.readOutbound(as: HTTP2Frame.self))!
frame.assertWindowUpdateFrame(streamID: 1, windowIncrement: 896)
// The inbound window size should now be our target: 1024. Write enough bytes to consume the
// inbound window.
let data = HTTP2Frame.FramePayload.data(.init(data: .byteBuffer(.init(repeating: 0, count: targetWindowSize + 1))))
let dataFrame = HTTP2Frame(streamID: 1, payload: data)
self.channel.pipeline.fireChannelRead(NIOAny(dataFrame))
self.channel.pipeline.fireUserInboundEventTriggered(StreamClosedEvent(streamID: 1, reason: nil))
self.channel.pipeline.fireChannelReadComplete()
// We've consumed the inbound window: normally we'd expect a WINDOW_UPDATE frame but since
// the stream has closed we don't expect to read anything out.
XCTAssertNil(try self.channel.readOutbound(as: HTTP2Frame.self))
}
}
@@ -32,6 +32,7 @@ extension InboundWindowManagerTests {
("testNewWindowSizeWhenNewSizeIsLessThanOrEqualHalfTarget", testNewWindowSizeWhenNewSizeIsLessThanOrEqualHalfTarget),
("testNewWindowSizeWithBufferedBytes", testNewWindowSizeWithBufferedBytes),
("testInitialWindowSizeChanged", testInitialWindowSizeChanged),
("testWindowSizeWhenClosed", testWindowSizeWhenClosed),
]
}
}
@@ -71,4 +71,15 @@ class InboundWindowManagerTests: XCTestCase {
// adjusted size is 8 (new size 5, buffered 3) which is less than half the target (now 20).
XCTAssertEqual(windowManager.newWindowSize(5), 12)
}
func testWindowSizeWhenClosed() {
var windowManager = InboundWindowManager(targetSize: 10)
XCTAssertEqual(windowManager.newWindowSize(5), 5)
windowManager.closed = true
XCTAssertNil(windowManager.newWindowSize(5))
windowManager.bufferedFrameReceived(size: 5)
XCTAssertNil(windowManager.bufferedFrameEmitted(size: 5))
}
}