Skip to content

Ensure monitorProcessTermination is ALWAYS called after body runs #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions Sources/Subprocess/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,34 @@ public struct Configuration: Sendable {

let processIdentifier = _spawnResult.execution.processIdentifier

let result = try await withAsyncTaskCleanupHandler {
let inputIO = _spawnResult.inputWriteEnd()
let outputIO = _spawnResult.outputReadEnd()
let errorIO = _spawnResult.errorReadEnd()

// Body runs in the same isolation
return try await body(_spawnResult.execution, inputIO, outputIO, errorIO)
} onCleanup: {
// Attempt to terminate the child process
await Execution.runTeardownSequence(
self.platformOptions.teardownSequence,
on: pid
)
let result: Swift.Result<Result, Error>
do {
result = try await .success(withAsyncTaskCleanupHandler {
let inputIO = _spawnResult.inputWriteEnd()
let outputIO = _spawnResult.outputReadEnd()
let errorIO = _spawnResult.errorReadEnd()

// Body runs in the same isolation
return try await body(_spawnResult.execution, inputIO, outputIO, errorIO)
} onCleanup: {
// Attempt to terminate the child process
await Execution.runTeardownSequence(
self.platformOptions.teardownSequence,
on: pid
)
})
} catch {
result = .failure(error)
}

return ExecutionResult(
terminationStatus: try await monitorProcessTermination(forProcessWithIdentifier: processIdentifier),
value: result
)
// Ensure that we begin monitoring process termination after `body` runs
// and regardless of whether `body` throws, so that the pid gets reaped
// even if `body` throws, and we are not leaving zombie processes in the
// process table which will cause the process termination monitoring thread
// to effectively hang due to the pid never being awaited
let terminationStatus = try await Subprocess.monitorProcessTermination(forProcessWithIdentifier: processIdentifier)

return try ExecutionResult(terminationStatus: terminationStatus, value: result.get())
}
}

Expand Down Expand Up @@ -779,15 +788,15 @@ internal func withAsyncTaskCleanupHandler<Result>(
// so _immediately_ if the failure scenario is due to parent task
// cancellation. We do so in a detached Task to prevent cancellation
// of the parent task from interrupting enumeration of the stream itself.
await Task.detached {
await withUncancelledTask {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already had an abstraction for this

do {
var iterator = runCancellationHandlerStream.makeAsyncIterator()
while let _ = try await iterator.next() {
}
} catch {
await handler()
}
}.value
}
}

defer {
Expand Down
Loading