diff --git a/Sources/NIOSSL/NIOSSLHandler.swift b/Sources/NIOSSL/NIOSSLHandler.swift index 464db957..ba8acb7d 100644 --- a/Sources/NIOSSL/NIOSSLHandler.swift +++ b/Sources/NIOSSL/NIOSSLHandler.swift @@ -698,9 +698,12 @@ extension NIOSSLHandler { // Here we guard against the possibility that any of these writes are larger than CInt.max. // This is very unusual but it can happen. To work around it, we just pretend that there were // multiple writes. + // + // During the short writes we set the promise to `nil` to make sure they only arrive at the end. + // Note that we make sure that there's always a single write, at the end, that holds the promise. let maxWriteSize = Int(CInt.max) - while let slice = data.readSlice(length: maxWriteSize) { - bufferedWrites.append((data: slice, promise: promise)) + while data.readableBytes > maxWriteSize, let slice = data.readSlice(length: maxWriteSize) { + bufferedWrites.append((data: slice, promise: nil)) } assert(data.readableBytes <= maxWriteSize) diff --git a/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift b/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift index 34e4727e..8a0d12ec 100644 --- a/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift +++ b/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift @@ -2405,8 +2405,7 @@ class NIOSSLIntegrationTest: XCTestCase { // for an existing bug. // We only run this test on 64-bit systems where we can safely allocate enough memory. try XCTSkipIf(MemoryLayout.size <= 4) - let targetSize = (1 << 31) + 1 - + let targetSize = Int(CInt.max) + 1 let write = ByteBuffer(repeating: 0, count: targetSize) let b2b = BackToBackEmbeddedChannel() @@ -2434,7 +2433,11 @@ class NIOSSLIntegrationTest: XCTestCase { ) XCTAssertNoThrow(try b2b.connectInMemory()) - b2b.client.writeAndFlush(write, promise: nil) + var completed = false + let promise = b2b.loop.makePromise(of: Void.self) + promise.futureResult.whenComplete { _ in completed = true } + + b2b.client.writeAndFlush(write, promise: promise) try b2b.interactInMemory() var reads: [ByteBuffer] = [] @@ -2443,6 +2446,7 @@ class NIOSSLIntegrationTest: XCTestCase { } let totalReadBytes = reads.reduce(into: 0, { $0 += $1.readableBytes }) XCTAssertEqual(totalReadBytes, targetSize) + XCTAssertTrue(completed) b2b.client.close(promise: nil) try b2b.interactInMemory() diff --git a/Tests/NIOSSLTests/NIOSSLTestHelpers.swift b/Tests/NIOSSLTests/NIOSSLTestHelpers.swift index 9e7edffa..77acfdb9 100644 --- a/Tests/NIOSSLTests/NIOSSLTestHelpers.swift +++ b/Tests/NIOSSLTests/NIOSSLTestHelpers.swift @@ -669,7 +669,7 @@ func generateSelfSignedCert(keygenFunction: () -> UnsafeMutablePointer final class BackToBackEmbeddedChannel { private(set) var client: EmbeddedChannel private(set) var server: EmbeddedChannel - private var loop: EmbeddedEventLoop + private(set) var loop: EmbeddedEventLoop init() {