Skip to content

Commit 765a049

Browse files
committed
Add method on Process that waits until exit and sends a SIGINT to the process if the Task is cancelled
1 parent ed6d15c commit 765a049

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Sources/SKSupport/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_library(SKSupport STATIC
1111
DocumentURI+CustomLogStringConvertible.swift
1212
FileSystem.swift
1313
LineTable.swift
14+
Process+WaitUntilExitWithCancellation.swift
1415
Random.swift
1516
Result.swift
1617
ThreadSafeBox.swift
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
15+
import class TSCBasic.Process
16+
import struct TSCBasic.ProcessResult
17+
18+
public extension Process {
19+
/// Wait for the process to exit. If the task gets cancelled, during this time, send a `SIGINT` to the process.
20+
func waitUntilExitSendingSigIntOnTaskCancellation() async throws -> ProcessResult {
21+
return try await withTaskCancellationHandler {
22+
try await waitUntilExit()
23+
} onCancel: {
24+
signal(SIGINT)
25+
}
26+
}
27+
}

Sources/SourceKitLSP/Swift/DocumentFormatting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ extension SwiftLanguageService {
154154
writeStream.send(snapshot.text)
155155
try writeStream.close()
156156

157-
let result = try await process.waitUntilExit()
157+
let result = try await process.waitUntilExitSendingSigIntOnTaskCancellation()
158158
guard result.exitStatus == .terminated(code: 0) else {
159159
let swiftFormatErrorMessage: String
160160
switch result.stderrOutput {

0 commit comments

Comments
 (0)