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

Wait for closeFuture instead of close promise in NIOAsyncChannel's executeThenClose #3032

Merged
merged 5 commits into from
Dec 17, 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
22 changes: 16 additions & 6 deletions Sources/NIOCore/AsyncChannel/AsyncChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,25 @@ public struct NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
}
}

self._outbound.finish()
// We ignore errors from close, since all we care about is that the channel has been closed
// at this point.
self.channel.close(promise: nil)
// `closeFuture` should never be failed, so we could ignore the error. However, do an
// assertionFailure to guide bad Channel implementations that are incorrectly failing this
// future to stop failing it.
do {
self._outbound.finish()
try await self.channel.close().get()
try await self.channel.closeFuture.get()
} catch {
if let error = error as? ChannelError, error == .alreadyClosed {
return result
}
throw error
assertionFailure(
"""
The channel's closeFuture should never be failed, but it was failed with error: \(error).
This is an error in the channel's implementation.
Refer to `Channel/closeFuture`'s documentation for more information.
"""
)
}

return result
}
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/NIOCore/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public protocol Channel: AnyObject, ChannelOutboundInvoker, _NIOPreconcurrencySe
var allocator: ByteBufferAllocator { get }

/// The `closeFuture` will fire when the `Channel` has been closed.
///
/// - Important: This future should never be failed: it signals when the channel has been closed, and this action should not fail,
/// regardless of whether the close happenned cleanly or not.
/// If you are interested in any errors thrown during `close` to diagnose any unclean channel closures, you
/// should instead use the future returned from ``ChannelOutboundInvoker/close(mode:file:line:)-7hlgf``
/// or pass a promise via ``ChannelOutboundInvoker/close(mode:promise:)``.
var closeFuture: EventLoopFuture<Void> { get }

/// The `ChannelPipeline` which handles all I/O events and requests associated with this `Channel`.
Expand Down
Loading