diff --git a/Sources/GRPC/GRPCIdleHandler.swift b/Sources/GRPC/GRPCIdleHandler.swift index 26f44de18..35c500435 100644 --- a/Sources/GRPC/GRPCIdleHandler.swift +++ b/Sources/GRPC/GRPCIdleHandler.swift @@ -157,8 +157,12 @@ internal final class GRPCIdleHandler: ChannelInboundHandler { } // Close the channel, if necessary. - if operations.shouldCloseChannel { - self.context?.close(mode: .all, promise: nil) + if operations.shouldCloseChannel, let context = self.context { + // Close on the next event-loop tick so we don't drop any events which are + // currently being processed. + context.eventLoop.execute { + context.close(mode: .all, promise: nil) + } } } diff --git a/Tests/GRPCTests/ConnectionManagerTests.swift b/Tests/GRPCTests/ConnectionManagerTests.swift index e1bd49b24..bcc93b8ff 100644 --- a/Tests/GRPCTests/ConnectionManagerTests.swift +++ b/Tests/GRPCTests/ConnectionManagerTests.swift @@ -800,6 +800,7 @@ extension ConnectionManagerTests { payload: .goAway(lastStreamID: 1, errorCode: .noError, opaqueData: nil) ) XCTAssertNoThrow(try channel.writeInbound(goAway)) + self.loop.run() } self.loop.run()