From 95ed6aa2a4bd3d765dde19498037a21e71dbc426 Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Tue, 6 Oct 2026 10:21:52 +0200 Subject: [PATCH] Address review comments --- Sources/System/IORing/IORequest.swift | 4 ++++ Sources/System/IORing/IORing.swift | 8 ++++++++ Tests/SystemTests/IORingTests.swift | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index cc4a7f0..544c1ef 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -274,6 +274,10 @@ extension IORing.Request { /// continue /// } /// armed = completion.flags.contains(.moreCompletions) + /// if let error = completion.error { + /// // The poll ended with an error, such as `.canceled`. + /// throw error + /// } /// // Handle incoming connection /// } /// ``` diff --git a/Sources/System/IORing/IORing.swift b/Sources/System/IORing/IORing.swift index 38cef62..473fe43 100644 --- a/Sources/System/IORing/IORing.swift +++ b/Sources/System/IORing/IORing.swift @@ -818,6 +818,14 @@ public struct IORing: ~Copyable { } /// Sends all prepared requests to the kernel for processing, and then dequeues at least `minimumCount` completions, waiting up to `timeout` for them to become available. `consumer` is called to process each completed IO operation as it becomes available. + /// + /// `consumer` receives a completion, an error, and whether consuming is done: + /// - For each completed operation, it receives the completion. If the + /// operation failed, it receives the operation's error alongside the + /// completion. + /// - If submitting the requests or waiting for completions fails, such as + /// with ``Errno/timeout``, it receives the error without a completion. + /// - Once consuming is done, it is called a final time with `done` set to `true`. @inlinable public func submitPreparedRequestsAndConsumeCompletions( minimumCount: UInt32 = 1, diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index 24fba4f..dffe76b 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -331,6 +331,26 @@ final class IORingTests: XCTestCase { XCTAssertEqual(completion.result, -Errno.badFileDescriptor.rawValue) } + func testSubmitAndConsumeCompletionsKeepsContextOfFailedCompletion() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(ring.prepare(request: .close(FileDescriptor(rawValue: -1), context: 44))) + + var received: [(context: UInt64?, error: Errno?, done: Bool)] = [] + ring.submitPreparedRequestsAndConsumeCompletions { + (completion: consuming IORing.Completion?, error, done) in + received.append((completion?.context, error, done)) + } + + XCTAssertEqual(received.count, 2) + XCTAssertEqual(received.first?.context, 44) + XCTAssertEqual(received.first?.error, .badFileDescriptor) + XCTAssertEqual(received.first?.done, false) + XCTAssertEqual(received.last?.context, nil) + XCTAssertEqual(received.last?.error, nil) + XCTAssertEqual(received.last?.done, true) + } + func testCompletionOfSuccessfulOperationHasNoError() throws { try XCTSkipIf(!uringEnabled, failureMessage) var ring = try IORing(queueDepth: 4)