From 9a224ae35fe1dec95fef23fac478a6783d895c99 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Wed, 7 Apr 2021 17:08:12 +0200 Subject: [PATCH] Add custom cleanup hook to allow PipeChannel to properly deregister during halfclose (#1793) Allow PipeChannel to perform custom cleanup to drop references to fds. Motivation: Currently PipeChannel doesn't properly deregister from the selector during half-closes. This means that some backend implementations will keep a reference on the kernel side which means that the POLLHUP will not be delivered for the read end of a pipe in e.g. NIOTests.StreamChannelTest/testHalfCloseOwnOutput - this has the test failing in #1761 / #1788. Modification: Add a generic cleanup hook to allow PipeChannel to properly deregister. Result: The final reference to the fd is dropped resulting in a POLLHUP being delivered as needed - making NIOTests.StreamChannelTest/testHalfCloseOwnOutput to pass for io_uring. --- Sources/NIO/BaseStreamSocketChannel.swift | 22 +++++++++++++++++----- Sources/NIO/PipeChannel.swift | 12 ++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Sources/NIO/BaseStreamSocketChannel.swift b/Sources/NIO/BaseStreamSocketChannel.swift index 70d7076ad..426af2044 100644 --- a/Sources/NIO/BaseStreamSocketChannel.swift +++ b/Sources/NIO/BaseStreamSocketChannel.swift @@ -72,6 +72,20 @@ class BaseStreamSocketChannel: BaseSocketChannel } } + // Hook for customizable socket shutdown processing for subclasses, e.g. PipeChannel + func shutdownSocket(mode: CloseMode) throws { + switch mode { + case .output: + try self.socket.shutdown(how: .WR) + self.outputShutdown = true + case .input: + try socket.shutdown(how: .RD) + self.inputShutdown = true + case .all: + break + } + } + // MARK: BaseSocketChannel's must override API that cannot be further refined by subclasses // This is `Channel` API so must be thread-safe. final override public var isWritable: Bool { @@ -161,15 +175,13 @@ class BaseStreamSocketChannel: BaseSocketChannel promise?.fail(ChannelError.outputClosed) return } - try self.socket.shutdown(how: .WR) - self.outputShutdown = true + try self.shutdownSocket(mode: mode) // Fail all pending writes and so ensure all pending promises are notified self.pendingWrites.failAll(error: error, close: false) self.unregisterForWritable() promise?.succeed(()) self.pipeline.fireUserInboundEventTriggered(ChannelEvent.outputClosed) - case .input: if self.inputShutdown { promise?.fail(ChannelError.inputClosed) @@ -179,11 +191,11 @@ class BaseStreamSocketChannel: BaseSocketChannel case ChannelError.eof: // No need to explicit call socket.shutdown(...) as we received an EOF and the call would only cause // ENOTCON + self.inputShutdown = true break default: - try socket.shutdown(how: .RD) + try self.shutdownSocket(mode: mode) } - self.inputShutdown = true self.unregisterForReadable() promise?.succeed(()) diff --git a/Sources/NIO/PipeChannel.swift b/Sources/NIO/PipeChannel.swift index d629f8607..04a022854 100644 --- a/Sources/NIO/PipeChannel.swift +++ b/Sources/NIO/PipeChannel.swift @@ -92,6 +92,18 @@ final class PipeChannel: BaseStreamSocketChannel { try! self.selectableEventLoop.deregister(channel: self, mode: .output) try! self.pipePair.outputFD.close() } + + override func shutdownSocket(mode: CloseMode) throws { + switch mode { + case .input: + try! self.selectableEventLoop.deregister(channel: self, mode: .input) + case .output: + try! self.selectableEventLoop.deregister(channel: self, mode: .output) + case .all: + break + } + try super.shutdownSocket(mode: mode) + } } extension PipeChannel: CustomStringConvertible {