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 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
16 changes: 7 additions & 9 deletions Sources/NIOCore/AsyncChannel/AsyncChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,13 @@ public struct NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
}
}

do {
self._outbound.finish()
try await self.channel.close().get()
} catch {
if let error = error as? ChannelError, error == .alreadyClosed {
return result
}
throw error
}
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` is never failed, so we can ignore the error
try? await self.channel.closeFuture.get()
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure whether we decided to ignore closeFuture errors entirely or not – it seems completely reasonable to ignore them though.

Suspect @FranzBusch has opinions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This future should never be failed; if we're considering a failing closeFuture the result of a bad implementation of a Channel but not something that should happen in any other scenario, it seemed reasonable to me to ignore all errors.

Copy link
Member

Choose a reason for hiding this comment

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

The original goals were:

  1. Make sure we only excited from executeThenClose when the channel is actually closed
  2. Surface any errors if closing fails

Now 1. is still being upheld here since we wait for the closeFuture. It also turns out that closing can't fail and a call to close() must always result in the closeFuture completing. Strictly speaking succeeding actually.

Now we have two options here. We can just assume that the "strict" part is true so there will never be any errors. However, there might be some code out there that actually fails the close future. What if we catch the error and add an assert that triggers if this close future actually fails to help us spot incorrect implementations?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, it just depends whether we want to help flush out the different implementations or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough, adding an assertion sounds useful.


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

/// The `closeFuture` will fire when the `Channel` has been closed.
///
/// - Important: This future will never be failed, as it signals when the channel has been closed, and this action cannot fail.
gjcairo marked this conversation as resolved.
Show resolved Hide resolved
/// If you are interested in any errors thrown during `close` to diagnose any unclean channel closures, you
/// should instead use the future returned from ``Channel/close(mode:file:line:)`` or pass a promise via
/// ``Channel/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