Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept PING frames even when fully quiesced #455

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1169,16 +1169,13 @@ extension HTTP2ConnectionStateMachine {
// RFC 7540 that says that receiving PINGs with ACK flags set when no PING ACKs are expected is forbidden. This is
// very strange, but we allow it.
switch self.state {
case .prefaceReceived, .active, .locallyQuiesced, .remotelyQuiesced, .bothQuiescing, .quiescingPrefaceReceived:
case .prefaceReceived, .active, .locallyQuiesced, .remotelyQuiesced, .bothQuiescing, .quiescingPrefaceReceived, .fullyQuiesced:
return (.init(result: .succeed, effect: nil), ackFlagSet ? .nothing : .sendAck)

case .idle, .prefaceSent, .quiescingPrefaceSent:
// We're waiting for the remote preface.
return (.init(result: .connectionError(underlyingError: NIOHTTP2Errors.missingPreface(), type: .protocolError), effect: nil), .nothing)

case .fullyQuiesced:
return (.init(result: .connectionError(underlyingError: NIOHTTP2Errors.ioOnClosedConnection(), type: .protocolError), effect: nil), .nothing)

case .modifying:
preconditionFailure("Must not be left in modifying state")
}
Expand All @@ -1190,16 +1187,13 @@ extension HTTP2ConnectionStateMachine {
// RFC 7540 that says that sending PINGs with ACK flags set when no PING ACKs are expected is forbidden. This is
// very strange, but we allow it.
switch self.state {
case .prefaceSent, .active, .locallyQuiesced, .remotelyQuiesced, .bothQuiescing, .quiescingPrefaceSent:
case .prefaceSent, .active, .locallyQuiesced, .remotelyQuiesced, .bothQuiescing, .quiescingPrefaceSent, .fullyQuiesced:
return .init(result: .succeed, effect: nil)

case .idle, .prefaceReceived, .quiescingPrefaceReceived:
// We're waiting for the local preface.
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.missingPreface(), type: .protocolError), effect: nil)

case .fullyQuiesced:
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.ioOnClosedConnection(), type: .protocolError), effect: nil)

case .modifying:
preconditionFailure("Must not be left in modifying state")
}
Expand Down
7 changes: 5 additions & 2 deletions Tests/NIOHTTP2Tests/ConnectionStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1064,8 +1064,6 @@ class ConnectionStateMachineTests: XCTestCase {
assertConnectionError(type: .protocolError, self.client.receivePushPromise(originalStreamID: streamOne, childStreamID: streamTwo, headers: ConnectionStateMachineTests.requestHeaders))

// Connectiony things don't work either.
assertConnectionError(type: .protocolError, self.client.sendPing())
assertConnectionError(type: .protocolError, self.server.receivePing(ackFlagSet: false))
assertConnectionError(type: .protocolError, self.client.sendPriority())
assertConnectionError(type: .protocolError, self.server.receivePriority())
assertConnectionError(type: .protocolError, self.client.sendSettings([]))
Expand All @@ -1074,6 +1072,11 @@ class ConnectionStateMachineTests: XCTestCase {
// Duplicate goaway is cool though.
assertSucceeds(self.client.sendGoaway(lastStreamID: .rootStream))
assertSucceeds(self.server.receiveGoaway(lastStreamID: .rootStream))

// PINGing is cool too.
assertSucceeds(self.client.sendPing())
assertSucceeds(self.server.receivePing(ackFlagSet: false))
assertSucceeds(self.client.receivePing(ackFlagSet: true))
}

func testPushesAfterSendingPrefaceAreInvalid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,37 @@ class SimpleClientServerInlineStreamMultiplexerTests: XCTestCase {
promise.succeed(())
stream.writeAndFlush(headerPayload, promise: promise)
}

func testSuccessfullyReceiveAndSendPingEvenWhenConnectionIsFullyQuiesced() throws {
let serverHandler = InboundFramePayloadRecorder()
try self.basicHTTP2Connection() { channel in
return channel.pipeline.addHandler(serverHandler)
}

// Fully quiesce the connection on the server.
let goAwayFrame = HTTP2Frame(streamID: .rootStream, payload: .goAway(lastStreamID: .rootStream, errorCode: .noError, opaqueData: nil))
serverChannel.writeAndFlush(goAwayFrame, promise: nil)

// Send PING frame to the server.
let pingFrame = HTTP2Frame(streamID: .rootStream, payload: .ping(HTTP2PingData(), ack: false))
clientChannel.writeAndFlush(pingFrame, promise: nil)

self.interactInMemory(self.clientChannel, self.serverChannel)

// The client receives the GOAWAY frame and fully quiesces the connection.
try self.clientChannel.assertReceivedFrame().assertGoAwayFrame(lastStreamID: .rootStream, errorCode: 0, opaqueData: nil)

// The server receives and responds to the PING.
try self.serverChannel.assertReceivedFrame().assertPingFrame(ack: false, opaqueData: HTTP2PingData())

// The client receives the PING response.
try self.clientChannel.assertReceivedFrame().assertPingFrame(ack: true, opaqueData: HTTP2PingData())

// No frames left.
self.clientChannel.assertNoFramesReceived()
self.serverChannel.assertNoFramesReceived()

XCTAssertNoThrow(try self.clientChannel.finish())
XCTAssertNoThrow(try self.serverChannel.finish())
}
}