Repository: apple/swift-nio
File: Sources/NIOPosix/BaseStreamSocketChannel.swift (Swift)

Code before:
                try self.socket.writev(iovecs: ptrs)
            },
            scalarFileWriteOperation: { descriptor, index, endIndex in
                try self.socket.sendFile(fd: descriptor, offset: index, count: endIndex - index)
            }
        )
        return result
    }

    final override func close0(error: Error, mode: CloseMode, promise: EventLoopPromise<Void>?) {
        do {
            switch mode {
            case .output:
                if self.outputShutdown {
                    promise?.fail(ChannelError._outputClosed)
                    return
                }
                if self.inputShutdown {
                    // Escalate to full closure
                    self.close0(error: error, mode: .all, promise: promise)
                    return
                }
                self.unregisterForWritable()

                let writesCloseResult = self.pendingWrites.close(promise)
                switch writesCloseResult {
                case .pending:
                    ()  // promise is stored in `pendingWrites` state for completing on later call to `closeComplete`
                case .readyForClose:
                    // Shutdown the socket only when the pending writes are dealt with
                    do {
                        try self.shutdownSocket(mode: mode)
                        self.pendingWrites.closeComplete()
                    } catch let err {
                        self.pendingWrites.closeComplete(err)
                    }
                    self.pipeline.fireUserInboundEventTriggered(ChannelEvent.outputClosed)
                case .closed:
                    promise?.succeed(())
                case .open:
                    promise?.fail(ChannelError.inappropriateOperationForState)
                    assertionFailure("Close resulted in an open state, this should never happen")
                }

            case .input:
                if self.inputShutdown {
                    promise?.fail(ChannelError._inputClosed)
                    return
                }
                if self.outputShutdown {
                    // Escalate to full closure

Review comment (suggestion):
A clearer design pattern here is probably to have the close result return the promise in cases where it needs to be completed (as associated data). That avoids the user needing to remember in which cases they have a promise to complete: if they were given it back, they have to complete it.

Code after:
                try self.socket.writev(iovecs: ptrs)
            },
            scalarFileWriteOperation: { descriptor, index, endIndex in
                try self.socket.sendFile(fd: descriptor, offset: index, count: endIndex - index)
            }
        )
        return result
    }

    final override func close0(error: Error, mode: CloseMode, promise: EventLoopPromise<Void>?) {
        do {
            switch mode {
            case .output:
                if self.outputShutdown {
                    promise?.fail(ChannelError._outputClosed)
                    return
                }
                if self.inputShutdown {
                    // Escalate to full closure
                    self.close0(error: error, mode: .all, promise: promise)
                    return
                }

                let result = self.pendingWrites.closeOutbound(promise)
                switch result {
                case .pending:
                    ()  // promise is stored in `pendingWrites` state for completing later

                case .readyForClose(let closePromise):
                    // Shutdown the socket only when the pending writes are dealt with
                    do {
                        try self.shutdownSocket(mode: mode)
                        closePromise?.succeed(())
                    } catch let err {
                        closePromise?.fail(err)
                    }
                    self.unregisterForWritable()
                    self.pipeline.fireUserInboundEventTriggered(ChannelEvent.outputClosed)

                case .closed(let closePromise):
                    closePromise?.succeed(())

                case .errored(let err, let closePromise):
                    assertionFailure("Close errored: \(err)")
                    closePromise?.fail(err)

                    // Escalate to full closure
                    // promise is nil here because we have used the supplied promise to convey failure of the half-close
                    self.close0(error: err, mode: .all, promise: nil)
                }