From bf446775a1683f5dc13240f56de2f9c55c299431 Mon Sep 17 00:00:00 2001 From: Aryan Shah Date: Tue, 17 Nov 2026 10:10:41 +0000 Subject: [PATCH] Wait for writes to drain before closing in NIOHTTP2ServerConnectionManagementHandler (#582) ### Motivation `NIOHTTP2ServerConnectionManagementHandler` was derived from the implementation in [`gRPC-swift-nio-transport`](https://github.com/grpc/grpc-swift-nio-transport/blob/main/Sources/GRPCNIOTransportCore/Server/Connection/ServerConnectionManagementHandler.swift). In that implementation, PR https://github.com/grpc/grpc-swift-nio-transport/pull/183 updated the way how the channel is closed. The channel is now first half-closed and then fully-closed in order to allow all pending writes to drain. This is also applicable in this implementation, so we should apply the same change here. ### Modifications Applied the changes verbatim from PR https://github.com/grpc/grpc-swift-nio-transport/pull/183. ### Result Pending writes are drained when gracefully shutting down. --- ...TP2ServerConnectionManagementHandler.swift | 38 ++++++---- ...rverConnectionManagementHandlerTests.swift | 74 +++++++++++++++++++ 2 files changed, 96 insertions(+), 16 deletions(-) diff --git a/Sources/NIOHTTP2/NIOHTTP2ServerConnectionManagementHandler.swift b/Sources/NIOHTTP2/NIOHTTP2ServerConnectionManagementHandler.swift index bafcf60..8e35f0d 100644 --- a/Sources/NIOHTTP2/NIOHTTP2ServerConnectionManagementHandler.swift +++ b/Sources/NIOHTTP2/NIOHTTP2ServerConnectionManagementHandler.swift @@ -467,22 +467,14 @@ extension NIOHTTP2ServerConnectionManagementHandler { case .startIdleTimer: self.maxIdleTimerHandler?.start() case .close: - // Defer closing until the next tick of the event loop. + // Defer until the next tick so the HTTP/2 handler's pending flush (which carries + // the END_STREAM frame's bytes) propagates first; then half-close to wait for those + // bytes to drain before fully closing. // - // This point is reached because the server is shutting down gracefully and the stream count - // has dropped to zero, meaning the connection is no longer required and can be closed. - // However, the stream would've been closed by writing and flushing a frame with end stream - // set. These are two distinct events in the channel pipeline. The HTTP/2 handler updates the - // state machine when a frame is written, which in this case results in the stream closed - // event which we're reacting to here. - // - // Importantly the HTTP/2 handler hasn't yet seen the flush event, so the bytes of the frame - // with end-stream set - and potentially some other frames - are sitting in a buffer in the - // HTTP/2 handler. If we close on this event loop tick then those frames will be dropped. - // Delaying the close by a loop tick will allow the flush to happen before the close. - let loopBound = NIOLoopBound(context, eventLoop: context.eventLoop) - context.eventLoop.execute { - loopBound.value.close(mode: .all, promise: nil) + // Without the half-close barrier any bytes still pending would be dropped on + // full close. + context.eventLoop.assumeIsolated().execute { + Self.gracefullyClose(context: context) } case .none: @@ -553,7 +545,7 @@ extension NIOHTTP2ServerConnectionManagementHandler { self.maybeFlush(context: context) if close { - context.close(promise: nil) + Self.gracefullyClose(context: context) } else { // We may have a grace period for finishing once the second GOAWAY frame has finished. // If this is set close the connection abruptly once the grace period passes. @@ -565,6 +557,20 @@ extension NIOHTTP2ServerConnectionManagementHandler { } } + /// Closes the connection gracefully. + /// + /// `close(mode: .all)` would drop any writes still queued in the channel's pending-writes + /// buffer or the kernel send buffer. Half-closing the output side first acts as a drain + /// barrier: the close-output promise only succeeds once all preceding writes have been + /// written, after which the full close is safe. + private static func gracefullyClose(context: ChannelHandlerContext) { + let promise = context.eventLoop.makePromise(of: Void.self) + context.close(mode: .output, promise: promise) + promise.futureResult.assumeIsolated().whenComplete { _ in + context.close(mode: .all, promise: nil) + } + } + private func keepaliveTimerFired() { guard let context = self.context else { return } let ping = HTTP2Frame(streamID: .rootStream, payload: .ping(self.keepalivePingData, ack: false)) diff --git a/Tests/NIOHTTP2Tests/NIOHTTP2ServerConnectionManagementHandlerTests.swift b/Tests/NIOHTTP2Tests/NIOHTTP2ServerConnectionManagementHandlerTests.swift index effa6b4..73b243d 100644 --- a/Tests/NIOHTTP2Tests/NIOHTTP2ServerConnectionManagementHandlerTests.swift +++ b/Tests/NIOHTTP2Tests/NIOHTTP2ServerConnectionManagementHandlerTests.swift @@ -149,6 +149,58 @@ struct NIOHTTP2ServerConnectionManagementHandlerTests { try connection.waitUntilClosed() } + @Test("Graceful close half-closes output before full close (no streams)") + func gracefulCloseHalfClosesBeforeFullCloseWithNoStreams() throws { + let connection = try Connection(maxIdleTime: .minutes(1)) + let recorder = CloseRecorder() + try connection.channel.pipeline.syncOperations.addHandler(recorder, position: .first) + try connection.activate() + + connection.advanceTime(by: .minutes(1)) + try self.testGracefulShutdown(connection: connection, lastStreamID: 0) + + // The ping-ack is the trigger for closing output. The recording handler stores the promise + // which will be succeeded in a moment. + #expect(recorder.modes == [.output]) + + // Complete the half-close promise. Doing that triggers the full close. + recorder.completeOutput() + #expect(recorder.modes == [.output, .all]) + + try connection.waitUntilClosed() + } + + @Test("Graceful close half-closes output before full close (last stream closes)") + func gracefulCloseHalfClosesBeforeFullCloseOnLastStreamClose() throws { + let connection = try Connection(maxIdleTime: .minutes(1)) + let recorder = CloseRecorder() + try connection.channel.pipeline.syncOperations.addHandler(recorder, position: .first) + try connection.activate() + + connection.advanceTime(by: .minutes(1)) + try self.testGracefulShutdown( + connection: connection, + lastStreamID: 1, + streamToOpenBeforePingAck: 1 + ) + + // Stream still open after second GOAWAY: no close yet. + #expect(recorder.modes.isEmpty) + + // Closing the last stream schedules a graceful close on the event loop. + connection.streamClosed(1) + connection.loop.run() + + // The recording handler stores the promise which will be succeeded in a moment. + #expect(recorder.modes == [.output]) + + // Complete the half-close promise. Doing that triggers the full close. + recorder.completeOutput() + #expect(recorder.modes == [.output, .all]) + + try connection.waitUntilClosed() + } + @Test("Keepalive works on new connection") func keepaliveOnNewConnection() throws { let connection = try Connection( @@ -387,3 +439,25 @@ extension HTTP2Frame.FramePayload { } } } + +private final class CloseRecorder: ChannelOutboundHandler { + typealias OutboundIn = NIOAny + + private(set) var modes: [CloseMode] = [] + private var halfClosePromise: EventLoopPromise? + + func close(context: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise?) { + self.modes.append(mode) + switch mode { + case .output: + self.halfClosePromise = promise + default: + context.close(mode: mode, promise: promise) + } + } + + func completeOutput() { + self.halfClosePromise?.succeed(()) + self.halfClosePromise = nil + } +}