Skip to content

Commit

Permalink
Merge pull request #110 from Carthage/nsconcretetask-setstartsnewproc…
Browse files Browse the repository at this point in the history
…essgroup

Use the `NSConcreteTask.setStartsNewProcessGroup` private API to terminate subprocesses when the parent process exits
  • Loading branch information
ikesyo authored Sep 27, 2018
2 parents 8420220 + 9143189 commit e243312
Showing 1 changed file with 15 additions and 3 deletions.
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

0 comments on commit e243312

Please sign in to comment.