Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@ struct SwiftSyntaxDevUtils: ParsableCommand {
VerifySourceCode.self,
]
)

public static func main() {
SigIntListener.registerSigIntSubprocessTerminationHandler()
do {
var command = try parseAsRoot(nil)
try command.run()
} catch {
if !SigIntListener.hasReceivedSigInt {
// No point printing an error message if the user requested the termination
// of the script.
exit(withError: error)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@

import Foundation

/// Keeps track of subprocesses spawned by this script and forwards SIGINT
/// signals to them.
class SigIntListener {
/// The subprocesses spawned by this script that are currently running.
static var runningSubprocesses: Set<Process> = []

/// Whether a `SIGINT` signal has been received by this script.
static var hasReceivedSigInt: Bool = false

/// Registers a `SIGINT` signal handler that forwards `SIGINT` to all
/// subprocesses that are registered in `runningSubprocesses`
static func registerSigIntSubprocessTerminationHandler() {
#if canImport(Darwin) || canImport(Glibc)
signal(SIGINT) { _ in
SigIntListener.hasReceivedSigInt = true
for process in SigIntListener.runningSubprocesses {
process.interrupt()
}
}
#endif
}
}

/// Provides convenience APIs for launching and gathering output from a subprocess
public class ProcessRunner {
private static let serialQueue = DispatchQueue(label: "\(ProcessRunner.self)")
Expand Down Expand Up @@ -56,7 +79,9 @@ public class ProcessRunner {
}

try process.run()
SigIntListener.runningSubprocesses.insert(process)
process.waitUntilExit()
SigIntListener.runningSubprocesses.remove(process)
if captureStdout || captureStderr {
// Make sure we've received all stdout/stderr
group.wait()
Expand Down