mirror of
https://github.com/apple/swift-system
synced 2026-11-24 04:38:14 +00:00
Merge pull request #401 from apple/fb-iouring-completion-error
[IO_URING] Keep the completion of failed IORing operations
This commit is contained in:
4 files changed
+114
-24
No files matched your search
@@ -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)
|
||||
|
||||
@@ -274,13 +274,17 @@ 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
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// 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.
|
||||
|
||||
@@ -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<Err: Error>(
|
||||
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? {
|
||||
@@ -810,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<Err: Error>(
|
||||
minimumCount: UInt32 = 1,
|
||||
|
||||
@@ -297,6 +297,70 @@ 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 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)
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user