Ensure that we fire channelActive and channelInactive in order (#373)

Motivation:

channelActive must always precede channelInactive. Unfortunately, this
isn't guaranteed right now: if we happen to get channelInactive as a
result of our writing the preamble we'll fire them in the wrong order.
Many systems can tolerate this, but we should avoid doing this anyway.

Modifications:

- Added a state enum to keep track of the state we're in
- Use that state enum to ensure we deliver active/inactive in the right
  order
- Additionally police other unexpected active/inactive behaviours

Result:

We'll appropriately police the state.
This commit is contained in:
Cory Benfield authored and GitHub committed 2023-02-15 15:52:34 +00:00
1 parent 5fcdf62ae8
commit 8dcda68288
6 files changed
+370 -6

No files matched your search

+143 -4
View File
@@ -92,6 +92,13 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
/// which can cause an infinite recursion.
private var isUnbufferingAndFlushingAutomaticFrames = false
/// In some cases channelInactive can be fired while channelActive is still running.
private var activationState = ActivationState.idle
/// Whether we should tolerate "impossible" state transitions in debug mode. Only true in tests specifically trying to
/// trigger them.
private let tolerateImpossibleStateTransitionsInDebugMode: Bool
/// The mode for this parser to operate in: client or server.
public enum ParserMode {
/// Client mode
@@ -152,6 +159,36 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
self.initialSettings = initialSettings
self.outboundBuffer = CompoundOutboundBuffer(mode: mode, initialMaxOutboundStreams: 100, maxBufferedControlFrames: maximumBufferedControlFrames)
self.denialOfServiceValidator = DOSHeuristics(maximumSequentialEmptyDataFrames: maximumSequentialEmptyDataFrames)
self.tolerateImpossibleStateTransitionsInDebugMode = false
}
/// Constructs a ``NIOHTTP2Handler``.
///
/// - parameters:
/// - mode: The mode for this handler, client or server.
/// - initialSettings: The settings that will be advertised to the peer in the preamble. Defaults to ``nioDefaultSettings``.
/// - headerBlockValidation: Whether to validate sent and received HTTP/2 header blocks. Defaults to ``ValidationState/enabled``.
/// - contentLengthValidation: Whether to validate the content length of sent and received streams. Defaults to ``ValidationState/enabled``.
/// - maximumSequentialEmptyDataFrames: Controls the number of empty data frames this handler will tolerate receiving in a row before DoS protection
/// is triggered and the connection is terminated. Defaults to 1.
/// - maximumBufferedControlFrames: Controls the maximum buffer size of buffered outbound control frames. If we are unable to send control frames as
/// fast as we produce them we risk building up an unbounded buffer and exhausting our memory. To protect against this DoS vector, we put an
/// upper limit on the depth of this queue. Defaults to 10,000.
/// - tolerateImpossibleStateTransitionsInDebugMode: Whether impossible state transitions should be tolerated
/// in debug mode.
internal init(mode: ParserMode,
initialSettings: HTTP2Settings = nioDefaultSettings,
headerBlockValidation: ValidationState = .enabled,
contentLengthValidation: ValidationState = .enabled,
maximumSequentialEmptyDataFrames: Int = 1,
maximumBufferedControlFrames: Int = 10000,
tolerateImpossibleStateTransitionsInDebugMode: Bool = false) {
self.stateMachine = HTTP2ConnectionStateMachine(role: .init(mode), headerBlockValidation: .init(headerBlockValidation), contentLengthValidation: .init(contentLengthValidation))
self.mode = mode
self.initialSettings = initialSettings
self.outboundBuffer = CompoundOutboundBuffer(mode: mode, initialMaxOutboundStreams: 100, maxBufferedControlFrames: maximumBufferedControlFrames)
self.denialOfServiceValidator = DOSHeuristics(maximumSequentialEmptyDataFrames: maximumSequentialEmptyDataFrames)
self.tolerateImpossibleStateTransitionsInDebugMode = tolerateImpossibleStateTransitionsInDebugMode
}
public func handlerAdded(context: ChannelHandlerContext) {
@@ -160,6 +197,11 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
self.writeBuffer = context.channel.allocator.buffer(capacity: 128)
if context.channel.isActive {
// We jump immediately to activated here, as channelActive has probably already passed.
if self.activationState != .idle {
self.impossibleActivationStateTransition(state: self.activationState, activating: true, context: context)
}
self.activationState = .activated
self.writeAndFlushPreamble(context: context)
}
}
@@ -170,13 +212,80 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
}
public func channelActive(context: ChannelHandlerContext) {
// Check our activation state.
switch self.activationState {
case .idle:
// This is our first channelActive. We're now activating.
self.activationState = .activating
case .activated:
// This is a weird one, but it implies we "beat" the channelActive
// call down the pipeline when we added in handlerAdded. That's ok!
// We can safely ignore this.
return
case .activating, .inactiveWhileActivating, .inactive:
// All of these states are channel pipeline invariant violations, but conceptually possible.
//
// - .activating implies we got another channelActive while we were handling one. That would be
// a violation of pipeline invariants.
// - .inactiveWhileActivating implies a sequence of channelActive, channelInactive, channelActive
// synchronously. This is not just unlikely, but also misuse of the handler or violation of
// channel pipeline invariants.
// - .inactive implies we received channelInactive and then got another active. This is almost certainly
// misuse of the handler.
//
// We'll throw an error and then close. In debug builds, we crash.
self.impossibleActivationStateTransition(
state: self.activationState, activating: true, context: context
)
return
}
self.writeAndFlushPreamble(context: context)
context.fireChannelActive()
// Ok, we progressed. Now we check our state again.
switch self.activationState {
case .activating:
// This is the easy case: nothing exciting happened. We can activate and notify the pipeline.
self.activationState = .activated
context.fireChannelActive()
case .inactiveWhileActivating:
// This is awkward: we got a channelInactive during the above operation. We need to fire channelActive
// and then re-issue the channelInactive call.
self.activationState = .activated
context.fireChannelActive()
self.channelInactive(context: context)
case .idle, .activated, .inactive:
// These three states should be impossible.
//
// - .idle somehow implies we didn't execute the code above.
// - .activated implies that the above code didn't prevent us re-entrantly getting to this point.
// - .inactive implies that somehow we hit channelInactive but didn't enter .inactiveWhileActivating.
self.impossibleActivationStateTransition(
state: self.activationState, activating: true, context: context
)
}
}
public func channelInactive(context: ChannelHandlerContext) {
self.channelClosed = true
context.fireChannelInactive()
switch self.activationState {
case .activated:
// This is the easy one. We were active, now we aren't.
self.activationState = .inactive
self.channelClosed = true
context.fireChannelInactive()
case .activating:
// Huh, we got channelInactive during activation. We need to maintain
// ordering, so we'll temporarily delay this.
self.activationState = .inactiveWhileActivating
case .idle, .inactiveWhileActivating, .inactive:
// This is weird.
//
// .idle implies that somehow we got channelInactive before channelActive, which is probably an error.
// .inactiveWhileActivating and .inactive make this a second channelInactive call, which is also probably
// an error.
self.impossibleActivationStateTransition(state: self.activationState, activating: false, context: context)
}
}
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
@@ -699,8 +808,38 @@ NIOHTTP2Handler(
mode: \(String(describing: self.mode)),
initialSettings: \(String(describing: self.initialSettings)),
channelClosed: \(String(describing: self.channelClosed)),
channelWritable: \(String(describing: self.channelWritable))
channelWritable: \(String(describing: self.channelWritable)),
activationState: \(String(describing: self.activationState))
)
"""
}
}
extension NIOHTTP2Handler {
/// Tracks the state of activation of the handler.
enum ActivationState {
/// The handler hasn't been activated yet.
case idle
/// The handler has received channelActive, but hasn't yet fired it on.
case activating
/// The handler was activating when it received channelInactive. The channel
/// must go inactive after firing channel active.
case inactiveWhileActivating
/// The channel has received and fired channelActive.
case activated
/// The channel has received and fired channelActive and channelInactive.
case inactive
}
fileprivate func impossibleActivationStateTransition(
state: ActivationState, activating: Bool, context: ChannelHandlerContext
) {
assert(self.tolerateImpossibleStateTransitionsInDebugMode, "Unexpected channelActive in state \(state)")
context.fireErrorCaught(NIOHTTP2Errors.ActivationError(state: state, activating: activating))
context.close(promise: nil)
}
}
+19
View File
@@ -1622,6 +1622,25 @@ public enum NIOHTTP2Errors {
self.storage = .init(streamID: streamID, baseError: baseError)
}
}
public struct ActivationError: NIOHTTP2Error, CustomStringConvertible {
private let state: NIOHTTP2Handler.ActivationState
private var activating: Bool
init(state: NIOHTTP2Handler.ActivationState, activating: Bool) {
self.state = state
self.activating = activating
}
public var description: String {
if self.activating {
return "Error during activation: in state \(self.state)"
} else {
return "Error during inactivation: in state \(self.state)"
}
}
}
}
@@ -2067,7 +2067,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase {
}
}
private final class ErrorRecorder: ChannelInboundHandler {
final class ErrorRecorder: ChannelInboundHandler {
typealias InboundIn = Any
var errors: [Error] = []
@@ -149,7 +149,6 @@ final class ReentrancyTests: XCTestCase {
try self.serverChannel.assertReceivedFrame().assertFrameMatches(this: settingsFrame)
XCTAssertNoThrow(try self.clientChannel.finish())
XCTAssertNoThrow(try self.serverChannel.finish())
}
func testReenterReadEOFOnRead() throws {
@@ -33,6 +33,10 @@ extension SimpleClientServerTests {
("testNoStreamWindowUpdateOnEndStreamFrameFromServer", testNoStreamWindowUpdateOnEndStreamFrameFromServer),
("testNoStreamWindowUpdateOnEndStreamFrameFromClient", testNoStreamWindowUpdateOnEndStreamFrameFromClient),
("testStreamCreationOrder", testStreamCreationOrder),
("testHandlingChannelInactiveDuringActive", testHandlingChannelInactiveDuringActive),
("testWritingFromChannelActiveIsntReordered", testWritingFromChannelActiveIsntReordered),
("testChannelActiveAfterAddingToActivePipelineDoesntError", testChannelActiveAfterAddingToActivePipelineDoesntError),
("testImpossibleStateTransitionsThrowErrors", testImpossibleStateTransitionsThrowErrors),
]
}
}
@@ -360,4 +360,207 @@ class SimpleClientServerTests: XCTestCase {
XCTAssert(error is NIOHTTP2Errors.StreamIDTooSmall)
}
}
func testHandlingChannelInactiveDuringActive() throws {
let channel = EmbeddedChannel()
let orderingHandler = ActiveInactiveOrderingHandler()
try channel.pipeline.syncOperations.addHandlers(
[
ActionOnFlushHandler { $0.close(promise: nil) },
NIOHTTP2Handler(mode: .client),
orderingHandler,
]
)
try channel.connect(to: SocketAddress(unixDomainSocketPath: "/tmp/ignored"), promise: nil)
XCTAssertEqual(orderingHandler.events, [.active, .inactive])
}
func testWritingFromChannelActiveIsntReordered() throws {
let channel = EmbeddedChannel()
try channel.pipeline.syncOperations.addHandlers(
[
NIOHTTP2Handler(mode: .client),
WriteOnChannelActiveHandler(),
]
)
try channel.connect(to: SocketAddress(unixDomainSocketPath: "/tmp/ignored"), promise: nil)
var writes = [ByteBuffer]()
while let nextWrite = try channel.readOutbound(as: ByteBuffer.self) {
writes.append(nextWrite)
}
XCTAssertEqual(writes.count, 3)
// This is a PING frame.
XCTAssertEqual(
writes.last,
ByteBuffer(bytes: [
0x00, 0x00, 0x08, // length, 8 bytes
0x06, // frame type, ping
0x00, // flags, zeros
0x00, 0x00, 0x00, 0x00, // stream ID, 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // 8 byte payload, all zeros
])
)
}
func testChannelActiveAfterAddingToActivePipelineDoesntError() throws {
let channel = EmbeddedChannel()
try channel.connect(to: SocketAddress(unixDomainSocketPath: "/tmp/ignored"), promise: nil)
XCTAssertNil(try channel.readOutbound(as: ByteBuffer.self))
try channel.pipeline.syncOperations.addHandler(
NIOHTTP2Handler(mode: .client)
)
// Two non-nil writes issued when the handler is added to an active channel.
XCTAssertNotNil(try channel.readOutbound(as: ByteBuffer.self))
XCTAssertNotNil(try channel.readOutbound(as: ByteBuffer.self))
XCTAssertNil(try channel.readOutbound(as: ByteBuffer.self))
// Delayed channel active.
channel.pipeline.fireChannelActive()
// No further writes.
XCTAssertNil(try channel.readOutbound(as: ByteBuffer.self))
}
func testImpossibleStateTransitionsThrowErrors() throws {
func setUpChannel() throws -> (EmbeddedChannel, ErrorRecorder) {
let channel = EmbeddedChannel()
let recorder = ErrorRecorder()
try channel.pipeline.syncOperations.addHandlers(
[
NIOHTTP2Handler(mode: .client, tolerateImpossibleStateTransitionsInDebugMode: true),
recorder
]
)
XCTAssertEqual(recorder.errors.count, 0)
return (channel, recorder)
}
// First impossible state transition: channelInactive during idle.
// Doesn't transition state, just errors. Becuase we close during this, we hit
// the error twice!
var (channel, recorder) = try setUpChannel()
channel.pipeline.fireChannelInactive()
XCTAssertEqual(recorder.errors.count, 2)
XCTAssertTrue(recorder.errors.allSatisfy { $0 is NIOHTTP2Errors.ActivationError })
// Second impossible state transition: channelActive on channelActive.
(channel, recorder) = try setUpChannel()
try channel.pipeline.syncOperations.addHandler(ActionOnFlushHandler { $0.pipeline.fireChannelActive() }, position: .first)
channel.pipeline.fireChannelActive()
XCTAssertEqual(recorder.errors.count, 1)
XCTAssertTrue(recorder.errors.allSatisfy { $0 is NIOHTTP2Errors.ActivationError })
// Third impossible state transition. Synchronous active/inactive/active. The error causes a close,
// so we error twice!
(channel, recorder) = try setUpChannel()
try channel.pipeline.syncOperations.addHandler(
ActionOnFlushHandler {
$0.pipeline.fireChannelInactive()
$0.pipeline.fireChannelActive()
},
position: .first
)
channel.pipeline.fireChannelActive()
XCTAssertEqual(recorder.errors.count, 2)
XCTAssertTrue(recorder.errors.allSatisfy { $0 is NIOHTTP2Errors.ActivationError })
// Fourth impossible state transition: active/inactive/active asynchronously. The error causes a close,
// so we error twice!
(channel, recorder) = try setUpChannel()
channel.pipeline.fireChannelActive()
channel.pipeline.fireChannelInactive()
XCTAssertEqual(recorder.errors.count, 0)
channel.pipeline.fireChannelActive()
XCTAssertEqual(recorder.errors.count, 2)
XCTAssertTrue(recorder.errors.allSatisfy { $0 is NIOHTTP2Errors.ActivationError })
// Fifth impossible state transition: active/inactive/inactive synchronously. The error causes a close,
// so we error twice!
(channel, recorder) = try setUpChannel()
try channel.pipeline.syncOperations.addHandler(
ActionOnFlushHandler {
$0.pipeline.fireChannelInactive()
$0.pipeline.fireChannelInactive()
},
position: .first
)
channel.pipeline.fireChannelActive()
XCTAssertEqual(recorder.errors.count, 2)
XCTAssertTrue(recorder.errors.allSatisfy { $0 is NIOHTTP2Errors.ActivationError })
// Sixth impossible state transition: active/inactive/inactive asynchronously. The error causes a close,
// so we error twice!
(channel, recorder) = try setUpChannel()
channel.pipeline.fireChannelActive()
channel.pipeline.fireChannelInactive()
XCTAssertEqual(recorder.errors.count, 0)
channel.pipeline.fireChannelInactive()
XCTAssertEqual(recorder.errors.count, 2)
XCTAssertTrue(recorder.errors.allSatisfy { $0 is NIOHTTP2Errors.ActivationError })
// Seventh impossible state transition: adding the handler twice. The error causes a close, so we
// error twice!
(channel, recorder) = try setUpChannel()
try channel.connect(to: SocketAddress(unixDomainSocketPath: "/tmp/ignored"), promise: nil)
let handler = try channel.pipeline.syncOperations.handler(type: NIOHTTP2Handler.self)
try channel.pipeline.syncOperations.addHandler(handler, position: .after(handler))
XCTAssertEqual(recorder.errors.count, 2)
XCTAssertTrue(recorder.errors.allSatisfy { $0 is NIOHTTP2Errors.ActivationError })
}
}
final class ActionOnFlushHandler: ChannelOutboundHandler {
typealias OutboundIn = Any
typealias OutboundOut = Any
private let action: (ChannelHandlerContext) -> Void
init(_ action: @escaping (ChannelHandlerContext) -> Void) {
self.action = action
}
func flush(context: ChannelHandlerContext) {
self.action(context)
}
}
final class ActiveInactiveOrderingHandler: ChannelInboundHandler {
typealias InboundIn = Any
typealias InboundOut = Any
enum Event {
case active
case inactive
}
var events: [Event] = []
func channelActive(context: ChannelHandlerContext) {
self.events.append(.active)
context.fireChannelActive()
}
func channelInactive(context: ChannelHandlerContext) {
self.events.append(.inactive)
context.fireChannelInactive()
}
}
final class WriteOnChannelActiveHandler: ChannelInboundHandler {
typealias InboundIn = Any
typealias OutboundOut = HTTP2Frame
func channelActive(context: ChannelHandlerContext) {
let frame = HTTP2Frame(streamID: 0, payload: .ping(HTTP2PingData(), ack: false))
context.writeAndFlush(self.wrapOutboundOut(frame), promise: nil)
context.fireChannelActive()
}
}