Skip to content

Commit 9abd4dd

Browse files
committed
Improve error handling
1 parent 48b97f2 commit 9abd4dd

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

Sources/Subprocess/AsyncBufferSequence.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,21 @@ public struct AsyncBufferSequence: AsyncSequence, Sendable {
7373
diskIO.stream(upToLength: readBufferSize, continuation: continuation)
7474
}
7575

76-
if let buffer = try await streamIterator.next() {
76+
let buffer: Buffer?
77+
78+
do {
79+
buffer = try await streamIterator.next()
80+
} catch {
81+
#if os(Windows)
82+
try self.diskIO.close()
83+
#else
84+
self.diskIO.close()
85+
#endif
86+
87+
throw error
88+
}
89+
90+
if let buffer {
7791
bytesRemaining -= buffer.count
7892
return buffer
7993
} else {
@@ -82,6 +96,7 @@ public struct AsyncBufferSequence: AsyncSequence, Sendable {
8296
#else
8397
self.diskIO.close()
8498
#endif
99+
85100
return nil
86101
}
87102
}

Sources/Subprocess/Platforms/Subprocess+Unix.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,26 @@ extension DispatchIO {
421421
length: maxLength,
422422
queue: .global()
423423
) { done, data, error in
424-
if error != 0 {
424+
guard error == 0 else {
425425
continuation.finish(throwing: SubprocessError(
426426
code: .init(.failedToReadFromSubprocess),
427427
underlyingError: .init(rawValue: error)
428428
))
429429
return
430430
}
431431

432-
// Treat empty data and nil as the same
433-
if let data = data.map({ $0.isEmpty ? nil : $0 }) ?? nil {
432+
guard let data else {
433+
fatalError("Unexpectedly received nil data from DispatchIO with error == 0.")
434+
}
435+
436+
if !data.isEmpty {
437+
// We have non-empty data. Yield it to the continuation
434438
continuation.yield(AsyncBufferSequence.Buffer(data: data))
435439
} else if done {
440+
// Receiving an empty data and done == true means we've reached the end of the file.
436441
continuation.finish()
442+
} else {
443+
fatalError("Unexpectedly received no data from DispatchIO with it indicating it is not done.")
437444
}
438445
}
439446
}

Sources/Subprocess/Platforms/Subprocess+Windows.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ public struct PlatformOptions: Sendable {
417417
/// process in case the parent task is cancelled before
418418
/// the child proces terminates.
419419
/// Always ends in forcefully terminate at the end.
420-
internal var streamOptions: StreamOptions = .init()
421420
public var teardownSequence: [TeardownStep] = []
422421
/// A closure to configure platform-specific
423422
/// spawning constructs. This closure enables direct
@@ -442,12 +441,6 @@ public struct PlatformOptions: Sendable {
442441
public init() {}
443442
}
444443

445-
extension PlatformOptions {
446-
internal struct StreamOptions: Sendable {
447-
internal init() {}
448-
}
449-
}
450-
451444
extension PlatformOptions: CustomStringConvertible, CustomDebugStringConvertible {
452445
internal func description(withIndent indent: Int) -> String {
453446
let indent = String(repeating: " ", count: indent * 4)

0 commit comments

Comments
 (0)