From cbf57d457533d4f55b6ca6a7530643e2df9f4b0d Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Mon, 5 Oct 2026 20:58:32 +0200 Subject: [PATCH 1/2] [IO_URING] Keep the completion of failed IORing operations When an operation failed, the blocking consume methods dropped its completion and only reported the `Errno`. The caller could not tell which request failed, since the `context` was lost. This breaks any user that matches completions to requests. This PR makes `blockingConsumeCompletions` pass the completion of a failed operation together with its error. Errors of the ring itself, such as `.timeout`, are still passed without a completion. Additionally, `blockingConsumeCompletion(timeout:)` now returns the completion of a failed operation instead of throwing. It only throws errors of the ring itself. I also added `Completion.error`, which is `nil` on success and the decoded `Errno` otherwise to make it easy to get to the error of a completion. Fixes #364 Fixes #395 --- Sources/System/IORing/IOCompletion.swift | 12 +++++-- Sources/System/IORing/IORequest.swift | 6 ++-- Sources/System/IORing/IORing.swift | 44 ++++++++++++++---------- Tests/SystemTests/IORingTests.swift | 44 ++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 24 deletions(-) diff --git a/Sources/System/IORing/IOCompletion.swift b/Sources/System/IORing/IOCompletion.swift index 672606b..9c7d55d 100644 --- a/Sources/System/IORing/IOCompletion.swift +++ b/Sources/System/IORing/IOCompletion.swift @@ -60,15 +60,21 @@ public extension IORing.Completion { /// for a read or a write, an event mask for a poll, and so on. /// /// A negative value is an `errno` code multiplied by -1. Recover the error - /// by negating it again: `Errno(rawValue: -completion.result)`. Only - /// ``IORing/tryConsumeCompletion()`` returns completions with a negative - /// result; the blocking methods report the error as an ``Errno`` instead. + /// by negating it again: `Errno(rawValue: -completion.result)`, or use + /// ``error``. @inlinable var result: Int32 { get { rawValue.res } } + /// The error of the completed operation, or `nil` if the operation succeeded. + @inlinable var error: Errno? { + get { + rawValue.res < 0 ? Errno(rawValue: -rawValue.res) : nil + } + } + @inlinable var flags: IORing.Completion.Flags { get { Flags(rawValue: rawValue.flags & 0x0000FFFF) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index a5c4c40..cc4a7f0 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -278,9 +278,9 @@ extension IORing.Request { /// } /// ``` /// - /// If the poll ends with an error, such as ``Errno/canceled``, - /// ``IORing/blockingConsumeCompletion(timeout:)`` throws that error - /// instead of returning the completion. + /// If the poll ends with an error, such as ``Errno/canceled``, its last + /// completion carries that error in ``IORing/Completion/error`` and + /// doesn't contain `.moreCompletions`. /// /// - Parameters: /// - file: The file descriptor to monitor for I/O events. diff --git a/Sources/System/IORing/IORing.swift b/Sources/System/IORing/IORing.swift index c55f28a..38cef62 100644 --- a/Sources/System/IORing/IORing.swift +++ b/Sources/System/IORing/IORing.swift @@ -519,11 +519,10 @@ public struct IORing: ~Copyable { var count = 0 while let completion = _tryConsumeCompletion(ring: completionRing) { count += 1 - if completion.result < 0 { - try consumer(nil, Errno(rawValue: -completion.result), false) - } else { - try consumer(completion, nil, false) - } + // We still return the completion for a failed operation so that the + // caller can tell which request failed through its `context`. + let error = completion.error + try consumer(completion, error, false) if count == maximumCount { try consumer(nil, nil, true) return @@ -575,11 +574,8 @@ public struct IORing: ~Copyable { var count = 0 while let completion = _tryConsumeCompletion(ring: completionRing) { count += 1 - if completion.result < 0 { - try consumer(nil, Errno(rawValue: -completion.result), false) - } else { - try consumer(completion, nil, false) - } + let error = completion.error + try consumer(completion, error, false) if count == maximumCount { break } @@ -595,17 +591,23 @@ public struct IORing: ~Copyable { var result: Completion? = nil try _blockingConsumeCompletionGuts(minimumCount: 1, maximumCount: 1, extraArgs: extraArgs) { (completion: consuming Completion?, error, done) throws(Errno) in - if let error { - throw error - } if let completion { result = consume completion + } else if let error { + throw error } } return result.take()! } /// Synchronously waits for an operation to complete for up to `timeout` (or forever if not specified) + /// + /// The completion is returned even if its operation failed. Check + /// ``IORing/Completion/error`` to find out whether it did, and + /// ``IORing/Completion/context`` to find out which request it belongs to. + /// + /// - Throws: An error of the ring itself, such as ``Errno/timeout`` when + /// no operation completed within `timeout`. @inlinable public func blockingConsumeCompletion( timeout: Duration? = nil @@ -629,7 +631,17 @@ public struct IORing: ~Copyable { } } - /// Synchronously waits for `minimumCount` or more operations to complete for up to `timeout` (or forever if not specified). For each completed operation found, `consumer` is called to handle processing it + /// Synchronously waits for `minimumCount` or more operations to complete for up to `timeout` + /// (or forever if not specified). For each completed operation found, `consumer` is called to handle + /// processing it. + /// + /// `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 the ring itself 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 blockingConsumeCompletions( minimumCount: UInt32 = 1, @@ -658,10 +670,6 @@ public struct IORing: ~Copyable { } } - // public func peekNextCompletion() -> IOCompletion { - - // } - /// Takes a completed operation from the ring and returns it, if one is ready. Otherwise, returns nil. @inlinable public func tryConsumeCompletion() -> Completion? { diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index 799e591..24fba4f 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -297,6 +297,50 @@ final class IORingTests: XCTestCase { XCTAssertEqual(dones, [false, true]) } + func testBlockingConsumeCompletionsKeepsContextOfFailedCompletion() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(try ring.submit( + linkedRequests: .close(FileDescriptor(rawValue: -1), context: 42) + )) + + var received: [(context: UInt64?, error: Errno?, done: Bool)] = [] + ring.blockingConsumeCompletions { (completion: consuming IORing.Completion?, error, done) in + received.append((completion?.context, error, done)) + } + + XCTAssertEqual(received.count, 2) + XCTAssertEqual(received.first?.context, 42) + 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 testBlockingConsumeCompletionReturnsFailedCompletion() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(try ring.submit( + linkedRequests: .close(FileDescriptor(rawValue: -1), context: 43) + )) + + let completion = try ring.blockingConsumeCompletion() + XCTAssertEqual(completion.context, 43) + XCTAssertEqual(completion.error, .badFileDescriptor) + XCTAssertEqual(completion.result, -Errno.badFileDescriptor.rawValue) + } + + func testCompletionOfSuccessfulOperationHasNoError() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 4) + XCTAssertTrue(try ring.submit(linkedRequests: .nop())) + + let completion = try ring.blockingConsumeCompletion() + XCTAssertEqual(completion.result, 0) + XCTAssertNil(completion.error) + } + func testRegisterEventFDTwiceThrows() throws { try XCTSkipIf(!uringEnabled, failureMessage) var ring = try IORing(queueDepth: 4) From 95ed6aa2a4bd3d765dde19498037a21e71dbc426 Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Tue, 6 Oct 2026 10:21:52 +0200 Subject: [PATCH 2/2] 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)