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.
This commit is contained in:
Joakim Hassila authored and GitHub committed 2021-04-07 16:08:12 +01:00
1 parent 4220c7a16a
commit 9a224ae35f
2 files changed
+29 -5

No files matched your search

+17 -5
View File
@@ -72,6 +72,20 @@ class BaseStreamSocketChannel<Socket: SocketProtocol>: BaseSocketChannel<Socket>
}
}
// 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<Socket: SocketProtocol>: BaseSocketChannel<Socket>
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<Socket: SocketProtocol>: BaseSocketChannel<Socket>
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(())
+12
View File
@@ -92,6 +92,18 @@ final class PipeChannel: BaseStreamSocketChannel<PipePair> {
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 {