diff --git a/Sources/Subprocess/Configuration.swift b/Sources/Subprocess/Configuration.swift index d67eede..a5350f6 100644 --- a/Sources/Subprocess/Configuration.swift +++ b/Sources/Subprocess/Configuration.swift @@ -74,23 +74,17 @@ public struct Configuration: Sendable { let pid = spawnResults.execution.processIdentifier var spawnResultBox: SpawnResult?? = consume spawnResults + var _spawnResult = spawnResultBox!.take()! - return try await withAsyncTaskCleanupHandler { - var _spawnResult = spawnResultBox!.take()! + let processIdentifier = _spawnResult.execution.processIdentifier + + let result = try await withAsyncTaskCleanupHandler { let inputIO = _spawnResult.inputWriteEnd() let outputIO = _spawnResult.outputReadEnd() let errorIO = _spawnResult.errorReadEnd() - let processIdentifier = _spawnResult.execution.processIdentifier - async let terminationStatus = try monitorProcessTermination( - forProcessWithIdentifier: processIdentifier - ) // Body runs in the same isolation - let result = try await body(_spawnResult.execution, inputIO, outputIO, errorIO) - return ExecutionResult( - terminationStatus: try await terminationStatus, - value: result - ) + return try await body(_spawnResult.execution, inputIO, outputIO, errorIO) } onCleanup: { // Attempt to terminate the child process await Execution.runTeardownSequence( @@ -98,6 +92,11 @@ public struct Configuration: Sendable { on: pid ) } + + return ExecutionResult( + terminationStatus: try await monitorProcessTermination(forProcessWithIdentifier: processIdentifier), + value: result + ) } }