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 2 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
36 changes: 36 additions & 0 deletions Tests/NIOHTTP2Tests/SimpleClientServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,42 @@ class SimpleClientServerTests: XCTestCase {
XCTAssertTrue(error is NIOHTTP2Errors.UnableToParseFrame)
}
}

@available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings")
func testSuccessfullyReceiveAndSendPingEvenWhenConnectionIsFullyQuiesced() throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid you've fallen into one of the many deprecated-API traps 🪤

This whole test class is deprecated because it's testing deprecated APIs. There's an equivalent set of tests in SimpleClientServerInlineStreamMultiplexerTests where this test should live. I think in this instance your test should just work without modifications.


An aside on the deprecated APIs. The original APIs for multiplexing created child channels for streams where you'd read and write HTTP2Frames.

Each HTTP2Frame holds a stream ID and its payload (headers, data etc.) However, when you open a stream the connection state machine wouldn't know about the stream until you did the first write on that stream.

This means that a client could create stream 1 and then stream 3 but write on stream 3 first. A subsequent write on stream 1 would be a protocol violation (new streams must have higher IDs than previously created streams).

To fix this we added new stream creation APIs where the child channels operate on HTTP2Frame.FramePayloads and only when they do their first write do they get assigned a stream ID.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense. Thanks.

let serverHandler = FrameRecorderHandler()
try self.basicHTTP2Connection() { channel, streamID in
return channel.pipeline.addHandler(serverHandler)
}

let goAwayFrame = HTTP2Frame(streamID: .rootStream, payload: .goAway(lastStreamID: .rootStream, errorCode: .noError, opaqueData: nil))

// Fully quiesce the connection on the server.
serverChannel.writeAndFlush(goAwayFrame, promise: nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The go away frame is strongly coupled with this write so it makes sense to group it with it and the comment.

Suggested change
let goAwayFrame = HTTP2Frame(streamID: .rootStream, payload: .goAway(lastStreamID: .rootStream, errorCode: .noError, opaqueData: nil))
// Fully quiesce the connection on the server.
serverChannel.writeAndFlush(goAwayFrame, promise: nil)
// 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)


let pingFrame = HTTP2Frame(streamID: .rootStream, payload: .ping(HTTP2PingData(), ack: false))

// Send PING frame to the server.
clientChannel.writeAndFlush(pingFrame, promise: nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here:

Suggested change
let pingFrame = HTTP2Frame(streamID: .rootStream, payload: .ping(HTTP2PingData(), ack: false))
// Send PING frame to the server.
clientChannel.writeAndFlush(pingFrame, 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())
}
}

final class ActionOnFlushHandler: ChannelOutboundHandler {
Expand Down