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 {