Skip to content

Commit d15a7a0

Browse files
authored
Run teardown sequence when the custom closure throws (#292)
1 parent 9ba4e36 commit d15a7a0

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Sources/Subprocess/Configuration.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ public struct Configuration: Sendable {
139139
taskFinishFlag.addOne()
140140
result = .success(bodyResult)
141141
} catch {
142+
let execution = Execution<Input, Output, Error>(
143+
processIdentifier: processIdentifier,
144+
inputWriter: nil,
145+
outputStream: nil,
146+
errorStream: nil
147+
)
148+
// Attempt to terminate the child process when the body throws
149+
await execution.teardown(using: self.platformOptions.teardownSequence)
142150
result = .failure(error)
143151
}
144152

Tests/SubprocessTests/IntegrationTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,32 @@ extension SubprocessIntegrationTests {
34373437
#expect(output == ["100°C", "32—x", "€5"])
34383438
}
34393439
}
3440+
3441+
@Test func testSubprocessTearsdownChildProcessWhenBodyThrows() async throws {
3442+
struct MyError: Swift.Error {}
3443+
3444+
#if os(Windows)
3445+
let setup = TestSetup(
3446+
executable: .name("powershell.exe"),
3447+
arguments: ["-Command", "Start-Sleep -Seconds 9999"]
3448+
)
3449+
#else
3450+
let setup = TestSetup(
3451+
executable: .name("tail"),
3452+
arguments: ["-f", "/dev/null"]
3453+
)
3454+
#endif
3455+
await #expect(throws: MyError.self) {
3456+
_ = try await _run(
3457+
setup,
3458+
input: .none,
3459+
output: .discarded,
3460+
error: .discarded
3461+
) { execution in
3462+
throw MyError()
3463+
}
3464+
}
3465+
}
34403466
}
34413467

34423468
// MARK: - Utilities

0 commit comments

Comments
 (0)