From 2bec00f11c17ada2bd20ec270d8a036d87f8c2a5 Mon Sep 17 00:00:00 2001 From: George Barnett Date: Fri, 15 Mar 2019 12:27:22 +0000 Subject: [PATCH] Remove flushability of streams when stream window size is set to zero. (#55) Motivation: When setting the stream window size for a stream which had data but couldn't write it (because of a zero window size) the stream would be marked as flushable. Setting the window size to zero again would not mark it as unflushable. On the following window (non-zero) resize it would be marked as flushable again. However, since this is preceeded by an assertion that it wasn't already marked as flushable it would cause a crash. Modifications: - Mark the stream as unflushable if it previously had data which it couldn't write. Result: Fewer crashes --- .../NIOHTTP2/OutboundFlowControlBuffer.swift | 3 +++ ...utboundFlowControlBufferTests+XCTest.swift | 1 + .../OutboundFlowControlBufferTests.swift | 25 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/Sources/NIOHTTP2/OutboundFlowControlBuffer.swift b/Sources/NIOHTTP2/OutboundFlowControlBuffer.swift index d493666..35132f8 100644 --- a/Sources/NIOHTTP2/OutboundFlowControlBuffer.swift +++ b/Sources/NIOHTTP2/OutboundFlowControlBuffer.swift @@ -103,6 +103,9 @@ internal struct OutboundFlowControlBuffer { if $0.hasPendingData && !hadData { assert(!self.flushableStreams.contains($0.streamID)) self.flushableStreams.insert($0.streamID) + } else if !$0.hasPendingData && hadData { + assert(self.flushableStreams.contains($0.streamID)) + self.flushableStreams.remove($0.streamID) } } } diff --git a/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests+XCTest.swift b/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests+XCTest.swift index 6483ef2..ea441d1 100644 --- a/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests+XCTest.swift @@ -37,6 +37,7 @@ extension OutboundFlowControlBufferTests { ("testWritesForUnknownStreamsFail", testWritesForUnknownStreamsFail), ("testOverlargeFramesAreSplitOnMaxFrameSizeByteBuffer", testOverlargeFramesAreSplitOnMaxFrameSizeByteBuffer), ("testOverlargeFramesAreSplitOnMaxFrameSizeFileRegion", testOverlargeFramesAreSplitOnMaxFrameSizeFileRegion), + ("testChangingStreamWindowSizeToZeroAndBack", testChangingStreamWindowSizeToZeroAndBack), ] } } diff --git a/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests.swift b/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests.swift index d96cbc2..7d4119a 100644 --- a/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests.swift +++ b/Tests/NIOHTTP2Tests/OutboundFlowControlBufferTests.swift @@ -312,6 +312,31 @@ class OutboundFlowControlBufferTests: XCTestCase { self.buffer.updateWindowOfStream(streamOne, newSize: 20) self.receivedFrames().assertFramesMatch([dataFrame], dataFileRegionToByteBuffer: false) } + + func testChangingStreamWindowSizeToZeroAndBack() { + // This test checks that updating the window size to zero on a flushable stream makes it no longer flushable. + let streamOne = HTTP2StreamID(1) + self.buffer.streamCreated(streamOne, initialWindowSize: 0) + + let dataFrame = self.createDataFrame(streamOne, byteBufferSize: 15) + XCTAssertNoThrow(try self.buffer.processOutboundFrame(dataFrame, promise: nil).assertNothing()) + XCTAssertEqual(0, self.receivedFrames().count) + + // The stream has data to write but the window size is zero, so it won't write. + self.buffer.flushReceived() + + self.buffer.updateWindowOfStream(streamOne, newSize: 15) + self.buffer.updateWindowOfStream(streamOne, newSize: 0) + + // Check the stream has been marked unflushable. + self.buffer.flushReceived() + XCTAssertEqual(0, self.receivedFrames().count) + + self.buffer.updateWindowOfStream(streamOne, newSize: 15) + self.buffer.flushReceived() + + self.receivedFrames().assertFramesMatch([dataFrame], dataFileRegionToByteBuffer: false) + } }