Skip to content

Commit 7789953

Browse files
committed
Improve error handling with the swift functions
1 parent d292465 commit 7789953

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

Sources/Subprocess/PipeConfiguration.swift

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,20 @@ extension PipeConfiguration {
551551
}
552552

553553
group.addTask {
554-
let retVal = try await function(inSequence, outWriter, errWriter)
555-
try await outWriter.finish()
556-
try await errWriter.finish()
557-
558-
return retVal
554+
do {
555+
let retVal = try await function(inSequence, outWriter, errWriter)
556+
557+
// Close outputs in case the function did not
558+
try await outWriter.finish()
559+
try await errWriter.finish()
560+
561+
return retVal
562+
} catch {
563+
// Close outputs in case the function did not
564+
try await outWriter.finish()
565+
try await errWriter.finish()
566+
throw error
567+
}
559568
}
560569

561570
for try await t in group {
@@ -564,10 +573,6 @@ extension PipeConfiguration {
564573
}
565574
}
566575

567-
// Close outputs in case the function did not
568-
try await outWriter.finish()
569-
try await errWriter.finish()
570-
571576
return 0
572577
}
573578

@@ -672,7 +677,20 @@ extension PipeConfiguration {
672677
let errWriter = StandardInputWriter(diskIO: errorWriteEnd.take()!)
673678

674679
group.addTask {
675-
return try await function(inSequence, outWriter, errWriter)
680+
do {
681+
let result = try await function(inSequence, outWriter, errWriter)
682+
683+
// Close outputs in case the function did not
684+
try await outWriter.finish()
685+
try await errWriter.finish()
686+
687+
return result
688+
} catch {
689+
// Close outputs in case the function did not
690+
try await outWriter.finish()
691+
try await errWriter.finish()
692+
throw error
693+
}
676694
}
677695

678696
for try await t in group {
@@ -681,10 +699,6 @@ extension PipeConfiguration {
681699
}
682700
}
683701

684-
// Close outputs in case the function did not
685-
try await outWriter.finish()
686-
try await errWriter.finish()
687-
688702
return 0
689703
}
690704

@@ -845,17 +859,18 @@ extension PipeConfiguration {
845859
}
846860

847861
group.addTask {
848-
let retVal = try await function(inSequence, outWriter, errWriter)
849-
try await outWriter.finish()
850-
try await errWriter.finish()
851-
return (retVal, .none)
862+
do {
863+
let retVal = try await function(inSequence, outWriter, errWriter)
864+
try await outWriter.finish()
865+
try await errWriter.finish()
866+
return (retVal, .none)
867+
} catch {
868+
try await outWriter.finish()
869+
try await errWriter.finish()
870+
throw error
871+
}
852872
}
853873

854-
// FIXME: determine how best to handle these writers so that the function doesn't finish them, and it doesn't cause deadlock
855-
// Close outputs in case the function did not
856-
//try await outWriter.finish()
857-
//try await errWriter.finish()
858-
859874
var exitCode: UInt32 = 0
860875
var output: Output.OutputType? = nil
861876
for try await r in group {

0 commit comments

Comments
 (0)