diff --git a/Sources/NIOHTTP2/HTTP2StreamChannel.swift b/Sources/NIOHTTP2/HTTP2StreamChannel.swift index 93759df..3f7aa8c 100644 --- a/Sources/NIOHTTP2/HTTP2StreamChannel.swift +++ b/Sources/NIOHTTP2/HTTP2StreamChannel.swift @@ -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() diff --git a/Sources/NIOHTTP2/InboundWindowManager.swift b/Sources/NIOHTTP2/InboundWindowManager.swift index 0a19c42..4298989 100644 --- a/Sources/NIOHTTP2/InboundWindowManager.swift +++ b/Sources/NIOHTTP2/InboundWindowManager.swift @@ -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, diff --git a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests+XCTest.swift b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests+XCTest.swift index 4f0ad15..2fcf15c 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests+XCTest.swift @@ -75,6 +75,7 @@ extension HTTP2FramePayloadStreamMultiplexerTests { ("testInboundChannelWindowSizeIsCustomisable", testInboundChannelWindowSizeIsCustomisable), ("testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer", testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer), ("testReadWhenUsingAutoreadOnChildChannel", testReadWhenUsingAutoreadOnChildChannel), + ("testWindowUpdateIsNotEmittedAfterStreamIsClosed", testWindowUpdateIsNotEmittedAfterStreamIsClosed), ] } } diff --git a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift index 930f2d5..7dfff8d 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift @@ -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)) + } } diff --git a/Tests/NIOHTTP2Tests/InboundWindowManagerTests+XCTest.swift b/Tests/NIOHTTP2Tests/InboundWindowManagerTests+XCTest.swift index 61498c0..19b50aa 100644 --- a/Tests/NIOHTTP2Tests/InboundWindowManagerTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/InboundWindowManagerTests+XCTest.swift @@ -32,6 +32,7 @@ extension InboundWindowManagerTests { ("testNewWindowSizeWhenNewSizeIsLessThanOrEqualHalfTarget", testNewWindowSizeWhenNewSizeIsLessThanOrEqualHalfTarget), ("testNewWindowSizeWithBufferedBytes", testNewWindowSizeWithBufferedBytes), ("testInitialWindowSizeChanged", testInitialWindowSizeChanged), + ("testWindowSizeWhenClosed", testWindowSizeWhenClosed), ] } } diff --git a/Tests/NIOHTTP2Tests/InboundWindowManagerTests.swift b/Tests/NIOHTTP2Tests/InboundWindowManagerTests.swift index c23892b..7fb1ee3 100644 --- a/Tests/NIOHTTP2Tests/InboundWindowManagerTests.swift +++ b/Tests/NIOHTTP2Tests/InboundWindowManagerTests.swift @@ -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)) + } }