diff --git a/Sources/NIOHTTP2/HTTP2StreamChannel.swift b/Sources/NIOHTTP2/HTTP2StreamChannel.swift index 160c92c..93759df 100644 --- a/Sources/NIOHTTP2/HTTP2StreamChannel.swift +++ b/Sources/NIOHTTP2/HTTP2StreamChannel.swift @@ -237,9 +237,7 @@ final class HTTP2StreamChannel 0 else { + return + } + // Ok, we're satisfying a read here. self.unsatisfiedRead = false self.deliverPendingReads() - - // If auto-read is turned on, recurse into read0. - // This cannot recurse indefinitely unless frames are being delivered - // by the read stacks, which is generally fairly unlikely to continue unbounded. - if self.autoRead { - self.read0() - } + self.tryToAutoRead() } private func changeWritability(to newWritability: Bool) { self._isWritable.store(newWritability) self.pipeline.fireChannelWritabilityChanged() } + + private func tryToAutoRead() { + if self.autoRead { + // If auto-read is turned on, recurse into channelPipeline.read(). + // This cannot recurse indefinitely unless frames are being delivered + // by the read stacks, which is generally fairly unlikely to continue unbounded. + self.pipeline.read() + } + } } // MARK:- Functions used to manage pending reads and writes. @@ -688,19 +694,13 @@ internal extension HTTP2StreamChannel { let message = Message(http2Frame: frame) - if self.unsatisfiedRead { - // We don't need to account for this frame in the window manager: it's being delivered - // straight into the pipeline. - self.pipeline.fireChannelRead(NIOAny(message)) - } else { - // Record the size of the frame so that when we receive a window update event our - // calculation on whether we emit a WINDOW_UPDATE frame is based on the bytes we have - // actually delivered into the pipeline. - if case .data(let dataPayload) = frame.payload { - self.windowManager.bufferedFrameReceived(size: dataPayload.data.readableBytes) - } - self.pendingReads.append(message) + // Record the size of the frame so that when we receive a window update event our + // calculation on whether we emit a WINDOW_UPDATE frame is based on the bytes we have + // actually delivered into the pipeline. + if case .data(let dataPayload) = frame.payload { + self.windowManager.bufferedFrameReceived(size: dataPayload.data.readableBytes) } + self.pendingReads.append(message) } diff --git a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests+XCTest.swift b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests+XCTest.swift index 78ff300..4f0ad15 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests+XCTest.swift @@ -74,6 +74,7 @@ extension HTTP2FramePayloadStreamMultiplexerTests { ("testStreamChannelToleratesFailingInitializer", testStreamChannelToleratesFailingInitializer), ("testInboundChannelWindowSizeIsCustomisable", testInboundChannelWindowSizeIsCustomisable), ("testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer", testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer), + ("testReadWhenUsingAutoreadOnChildChannel", testReadWhenUsingAutoreadOnChildChannel), ] } } diff --git a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift index fef405a..930f2d5 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FramePayloadStreamMultiplexerTests.swift @@ -1412,7 +1412,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { self.channel.embeddedEventLoop.run() XCTAssertEqual(frameRecorder.receivedFrames.count, 1) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 0) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 1) // Now we're going to send 9 data frames. var requestData = self.channel.allocator.buffer(capacity: 1024) @@ -1423,21 +1423,21 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { self.channel.pipeline.fireChannelRead(NIOAny(frame)) } - // We should have 10 reads, and zero read completes. - XCTAssertEqual(frameRecorder.receivedFrames.count, 10) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 0) + // We should have 1 read (the HEADERS), and one read complete. + XCTAssertEqual(frameRecorder.receivedFrames.count, 1) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 1) - // Fire read complete on the parent and it'll propagate to the child. + // Fire read complete on the parent and it'll propagate to the child. This will trigger the reads. self.channel.pipeline.fireChannelReadComplete() - // We should have 10 reads, and one read complete. + // We should have 10 reads, and two read completes. XCTAssertEqual(frameRecorder.receivedFrames.count, 10) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 1) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 2) // If we fire a new read complete on the parent, the child doesn't see it this time, as it received no frames. self.channel.pipeline.fireChannelReadComplete() XCTAssertEqual(frameRecorder.receivedFrames.count, 10) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 1) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 2) } func testMultiplexerCorrectlyTellsAllStreamsAboutReadComplete() { @@ -1470,9 +1470,9 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { self.channel.embeddedEventLoop.run() XCTAssertEqual(frameRecorder.receivedFrames.count, 3) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 0) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 3) - // Firing in readComplete causes a readComplete for each stream. + // Firing in readComplete does not cause a second readComplete for each stream, as no frames were delivered. self.channel.pipeline.fireChannelReadComplete() XCTAssertEqual(frameRecorder.receivedFrames.count, 3) XCTAssertEqual(readCompleteCounter.readCompleteCount, 3) @@ -1483,8 +1483,8 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { let frame = HTTP2Frame(streamID: 1, payload: .data(.init(data: .byteBuffer(requestData), endStream: false))) self.channel.pipeline.fireChannelRead(NIOAny(frame)) - // We should have 4 reads, and 3 read completes. - XCTAssertEqual(frameRecorder.receivedFrames.count, 4) + // We should have 3 reads, and 3 read completes. The frame is not delivered as we have no frame fast-path. + XCTAssertEqual(frameRecorder.receivedFrames.count, 3) XCTAssertEqual(readCompleteCounter.readCompleteCount, 3) // Fire read complete on the parent and it'll propagate to the child, but only to the one @@ -1762,4 +1762,47 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase { frameRecorder.flushedWrites.assertFramesMatch([HTTP2Frame(streamID: 1, payload: headersPayload), HTTP2Frame(streamID: 3, payload: headersPayload)]) } + + func testReadWhenUsingAutoreadOnChildChannel() throws { + var childChannel: Channel? = nil + let readCounter = ReadCounter() + let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { channel -> EventLoopFuture in + childChannel = channel + + // We're going to _enable_ autoRead on this channel. + return channel.setOption(ChannelOptions.autoRead, value: true).flatMap { + channel.pipeline.addHandler(readCounter) + } + } + XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + + XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil)) + + // Let's open a stream. + let streamID = HTTP2StreamID(1) + let frame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: HPACKHeaders()))) + XCTAssertNoThrow(try self.channel.writeInbound(frame)) + self.activateStream(streamID) + XCTAssertNotNil(childChannel) + + // There should be two calls to read: the first, when the stream was activated, the second after the HEADERS + // frame was delivered. + XCTAssertEqual(readCounter.readCount, 2) + + // Now deliver a data frame. + var buffer = self.channel.allocator.buffer(capacity: 12) + buffer.writeStaticString("Hello, world!") + let dataFrame = HTTP2Frame(streamID: streamID, payload: .data(.init(data: .byteBuffer(buffer)))) + XCTAssertNoThrow(try self.channel.writeInbound(dataFrame)) + + // This frame should have been immediately delivered, _and_ a call to read should have happened. + XCTAssertEqual(readCounter.readCount, 3) + + // Delivering two more frames causes two more calls to read. + XCTAssertNoThrow(try self.channel.writeInbound(dataFrame)) + XCTAssertNoThrow(try self.channel.writeInbound(dataFrame)) + XCTAssertEqual(readCounter.readCount, 5) + + XCTAssertNoThrow(try self.channel.finish()) + } } diff --git a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift index 63d7713..ed1fe77 100644 --- a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift @@ -112,6 +112,7 @@ final class ReadCounter: ChannelOutboundHandler { func read(context: ChannelHandlerContext) { readCount += 1 + context.read() } } @@ -1596,7 +1597,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { self.channel.embeddedEventLoop.run() XCTAssertEqual(frameRecorder.receivedFrames.count, 1) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 0) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 1) // Now we're going to send 9 data frames. var requestData = self.channel.allocator.buffer(capacity: 1024) @@ -1607,21 +1608,21 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { self.channel.pipeline.fireChannelRead(NIOAny(frame)) } - // We should have 10 reads, and zero read completes. - XCTAssertEqual(frameRecorder.receivedFrames.count, 10) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 0) + // We should have 1 read (the HEADERS), and zero read completes. + XCTAssertEqual(frameRecorder.receivedFrames.count, 1) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 1) - // Fire read complete on the parent and it'll propagate to the child. + // Fire read complete on the parent and it'll propagate to the child, also firing the reads.. self.channel.pipeline.fireChannelReadComplete() // We should have 10 reads, and one read complete. XCTAssertEqual(frameRecorder.receivedFrames.count, 10) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 1) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 2) // If we fire a new read complete on the parent, the child doesn't see it this time, as it received no frames. self.channel.pipeline.fireChannelReadComplete() XCTAssertEqual(frameRecorder.receivedFrames.count, 10) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 1) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 2) } @available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings") @@ -1655,9 +1656,9 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { self.channel.embeddedEventLoop.run() XCTAssertEqual(frameRecorder.receivedFrames.count, 3) - XCTAssertEqual(readCompleteCounter.readCompleteCount, 0) + XCTAssertEqual(readCompleteCounter.readCompleteCount, 3) - // Firing in readComplete causes a readComplete for each stream. + // Firing in readComplete does not cause a readComplete, as no new frames were delivered. self.channel.pipeline.fireChannelReadComplete() XCTAssertEqual(frameRecorder.receivedFrames.count, 3) XCTAssertEqual(readCompleteCounter.readCompleteCount, 3) @@ -1668,8 +1669,9 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { let frame = HTTP2Frame(streamID: 1, payload: .data(.init(data: .byteBuffer(requestData), endStream: false))) self.channel.pipeline.fireChannelRead(NIOAny(frame)) - // We should have 4 reads, and 3 read completes. - XCTAssertEqual(frameRecorder.receivedFrames.count, 4) + // We should have 3 reads, and 3 read completes. We don't actually get this frame yet, because + // we do not do a "fast-delivery" path. + XCTAssertEqual(frameRecorder.receivedFrames.count, 3) XCTAssertEqual(readCompleteCounter.readCompleteCount, 3) // Fire read complete on the parent and it'll propagate to the child, but only to the one