From fa35c603a0e279636ccffaf5c59968d4daec8366 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Fri, 15 Dec 2017 16:58:12 +0000 Subject: [PATCH] on Darwin, sendfile might write fully and return EWOULDBLOCK --- Sources/NIO/Channel.swift | 8 +++++++- Sources/NIO/FileRegion.swift | 6 ++++++ Tests/NIOTests/ChannelTests+XCTest.swift | 1 + Tests/NIOTests/ChannelTests.swift | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index 715736e5b..d779f64f7 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -272,7 +272,13 @@ private struct PendingWritesState { case .byteBuffer(let buffer): fatalError("write with .wouldBlock(\(n)) but a ByteBuffer (\(buffer)) was written") case .fileRegion(let file): - assert(file.readableBytes > 0, "write with .wouldBlock(\(n)) but sendfile wrote fully on \(file)") + if file.readableBytes == 0 { + /* this is odd, sendfile wrote everything but notionally failed with EWOULDBLOCK. This happens on Darwin. */ + if let promise = self.fullyWrittenFirst() { + promises.append(promise) + } + return (fulfillPromises, .writtenCompletely) + } } } /* we don't update here as we get non-zero only if sendfile was used and we don't track bytes to be sent as `FileRegion` */ diff --git a/Sources/NIO/FileRegion.swift b/Sources/NIO/FileRegion.swift index b03a8de89..953e76444 100644 --- a/Sources/NIO/FileRegion.swift +++ b/Sources/NIO/FileRegion.swift @@ -93,6 +93,12 @@ public final class FileRegion { } } +extension FileRegion: CustomStringConvertible { + public var description: String { + return "FileRegion(descriptor: \(self.descriptor), readerIndex: \(self.readerIndex), endIndex: \(self.endIndex), open: \(self.open))" + } +} + extension FileRegion { /// Create a new `FileRegion`. /// diff --git a/Tests/NIOTests/ChannelTests+XCTest.swift b/Tests/NIOTests/ChannelTests+XCTest.swift index 92b2a054a..a9686a53e 100644 --- a/Tests/NIOTests/ChannelTests+XCTest.swift +++ b/Tests/NIOTests/ChannelTests+XCTest.swift @@ -45,6 +45,7 @@ extension ChannelTests { ("testPendingWritesWorksWithManyEmptyWrites", testPendingWritesWorksWithManyEmptyWrites), ("testPendingWritesCloseDuringVectorWrite", testPendingWritesCloseDuringVectorWrite), ("testPendingWritesMoreThanWritevIOVectorLimit", testPendingWritesMoreThanWritevIOVectorLimit), + ("testPendingWritesIsHappyWhenSendfileReturnsWouldBlockButWroteFully", testPendingWritesIsHappyWhenSendfileReturnsWouldBlockButWroteFully), ] } } diff --git a/Tests/NIOTests/ChannelTests.swift b/Tests/NIOTests/ChannelTests.swift index cf613f9bc..25bcf16a8 100644 --- a/Tests/NIOTests/ChannelTests.swift +++ b/Tests/NIOTests/ChannelTests.swift @@ -990,6 +990,24 @@ public class ChannelTests: XCTestCase { XCTAssertEqual(WriteResult.writtenCompletely, result) XCTAssertTrue(flushPromise.futureResult.fulfilled) } + } + func testPendingWritesIsHappyWhenSendfileReturnsWouldBlockButWroteFully() { + let el = EmbeddedEventLoop() + withPendingWritesManager { pwm in + let ps: [EventLoopPromise<()>] = (0..<1).map { _ in el.newPromise() } + + _ = pwm.add(data: .fileRegion(FileRegion(descriptor: -1, readerIndex: 0, endIndex: 8192)), promise: ps[0]) + pwm.markFlushCheckpoint(promise: nil) + + let result = assertExpectedWritability(pendingWritesManager: pwm, + promises: ps, + expectedSingleWritabilities: nil, + expectedVectorWritabilities: nil, + expectedFileWritabilities: [(0, 8192)], + returns: [.wouldBlock(8192)], + promiseStates: [[true]]) + XCTAssertEqual(.writtenCompletely, result) + } } }