Skip to content
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

Use the NSConcreteTask.setStartsNewProcessGroup private API to terminate subprocesses when the parent process exits #110

Merged
merged 2 commits into from
Sep 27, 2018
Merged
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
18 changes: 15 additions & 3 deletions Sources/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,13 @@ extension Task {
/// - Parameters:
/// - standardInput: Data to stream to standard input of the launched process. If nil, stdin will
/// be inherited from the parent process.
/// - shouldBeTerminatedOnParentExit: A flag to control whether the launched child process should be terminated
/// when the parent process exits. The default value is `false`.
///
/// - Returns: A producer that will launch the receiver when started, then send
/// `TaskEvent`s as execution proceeds.
public func launch(standardInput: SignalProducer<Data, NoError>? = nil) -> SignalProducer<TaskEvent<Data>, TaskError> {
public func launch(standardInput: SignalProducer<Data, NoError>? = nil,
shouldBeTerminatedOnParentExit: Bool = false) -> SignalProducer<TaskEvent<Data>, TaskError> {
return SignalProducer { observer, lifetime in
let queue = DispatchQueue(label: self.description, attributes: [])
let group = Task.group
Expand All @@ -402,6 +405,15 @@ extension Task {
process.launchPath = self.launchPath
process.arguments = self.arguments

if shouldBeTerminatedOnParentExit {
// This is for terminating subprocesses when the parent process exits.
// See https://github.com/Carthage/ReactiveTask/issues/3 for the details.
let selector = Selector(("setStartsNewProcessGroup:"))
if process.responds(to: selector) {
process.perform(selector, with: false as NSNumber)
}
}

if let cwd = self.workingDirectoryPath {
process.currentDirectoryPath = cwd
}
Expand Down Expand Up @@ -484,8 +496,8 @@ extension Task {
process.standardError = stderrPipe.writeHandle

group.enter()
process.terminationHandler = { nstask in
let terminationStatus = nstask.terminationStatus
process.terminationHandler = { process in
let terminationStatus = process.terminationStatus
if terminationStatus == EXIT_SUCCESS {
// Wait for stderr to finish, then pass
// through stdout.
Expand Down