Skip to content

Commit ebd94eb

Browse files
committed
Support terminating suggestion services on normal exits
1 parent 68627c1 commit ebd94eb

File tree

9 files changed

+53
-5
lines changed

9 files changed

+53
-5
lines changed

Core/Sources/CodeiumService/CodeiumLanguageServer.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Preferences
77

88
protocol CodeiumLSP {
99
func sendRequest<E: CodeiumRequestType>(_ endpoint: E) async throws -> E.Response
10+
func terminate()
1011
}
1112

1213
final class CodeiumLanguageServer {
@@ -119,6 +120,14 @@ final class CodeiumLanguageServer {
119120
self.port = port
120121
launchHandler?()
121122
}
123+
124+
func terminate() {
125+
process.terminationHandler = nil
126+
if process.isRunning {
127+
process.terminate()
128+
}
129+
transport.close()
130+
}
122131
}
123132

124133
extension CodeiumLanguageServer: CodeiumLSP {

Core/Sources/CodeiumService/CodeiumService.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public protocol CodeiumSuggestionServiceType {
1919
func notifyChangeTextDocument(fileURL: URL, content: String) async throws
2020
func notifyCloseTextDocument(fileURL: URL) async throws
2121
func cancelRequest() async
22+
func terminate()
2223
}
2324

2425
enum CodeiumError: Error, LocalizedError {
@@ -325,6 +326,11 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
325326
public func notifyCloseTextDocument(fileURL: URL) async throws {
326327
await openedDocumentPool.closeDocument(url: fileURL)
327328
}
329+
330+
public func terminate() {
331+
server?.terminate()
332+
server = nil
333+
}
328334
}
329335

330336
func getXcodeVersion() async throws -> String {

Core/Sources/GitHubCopilotService/GitHubCopilotService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public protocol GitHubCopilotSuggestionServiceType {
3232
func notifyCloseTextDocument(fileURL: URL) async throws
3333
func notifySaveTextDocument(fileURL: URL) async throws
3434
func cancelRequest() async
35+
func terminate() async
3536
}
3637

3738
protocol GitHubCopilotLSP {
@@ -379,6 +380,10 @@ public final class GitHubCopilotSuggestionService: GitHubCopilotBaseService,
379380
// Logger.service.debug("Close \(uri)")
380381
try await server.sendNotification(.didCloseTextDocument(.init(uri: uri)))
381382
}
383+
384+
public func terminate() async {
385+
// automatically handled
386+
}
382387
}
383388

384389
extension InitializingServer: GitHubCopilotLSP {

Core/Sources/Service/ScheduledCleaner.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,12 @@ public final class ScheduledCleaner {
7070
}
7171
}
7272
}
73+
74+
@ServiceActor
75+
public func closeAllChildProcesses() async {
76+
for (_, workspace) in workspaces {
77+
await workspace.terminateSuggestionService()
78+
}
79+
}
7380
}
7481

Core/Sources/Service/Workspace.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,8 @@ extension Workspace {
428428
guard let suggestionService else { return }
429429
await suggestionService.cancelRequest()
430430
}
431+
432+
func terminateSuggestionService() async {
433+
await _suggestionService?.terminate()
434+
}
431435
}

Core/Sources/SuggestionService/CodeiumSuggestionProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,9 @@ extension CodeiumSuggestionProvider {
7575
await (try? createCodeiumServiceIfNeeded())?
7676
.cancelRequest()
7777
}
78+
79+
func terminate() async {
80+
(try? createCodeiumServiceIfNeeded())?.terminate()
81+
}
7882
}
7983

Core/Sources/SuggestionService/GitHubCopilotSuggestionProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,9 @@ extension GitHubCopilotSuggestionProvider {
7878
await (try? createGitHubCopilotServiceIfNeeded())?
7979
.cancelRequest()
8080
}
81+
82+
func terminate() async {
83+
await (try? createGitHubCopilotServiceIfNeeded())?.terminate()
84+
}
8185
}
8286

Core/Sources/SuggestionService/SuggestionService.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import AppKit
12
import Foundation
23
import Preferences
34
import SuggestionModel
@@ -21,6 +22,7 @@ public protocol SuggestionServiceType {
2122
func notifyCloseTextDocument(fileURL: URL) async throws
2223
func notifySaveTextDocument(fileURL: URL) async throws
2324
func cancelRequest() async
25+
func terminate() async
2426
}
2527

2628
protocol SuggestionServiceProvider: SuggestionServiceType {}
@@ -117,9 +119,13 @@ public extension SuggestionService {
117119
func notifySaveTextDocument(fileURL: URL) async throws {
118120
try await suggestionProvider.notifySaveTextDocument(fileURL: fileURL)
119121
}
120-
122+
121123
func cancelRequest() async {
122124
await suggestionProvider.cancelRequest()
123125
}
126+
127+
func terminate() async {
128+
await suggestionProvider.terminate()
129+
}
124130
}
125131

ExtensionService/AppDelegate.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
3434
_ = RealtimeSuggestionController.shared
3535
_ = XcodeInspector.shared
3636
AXIsProcessTrustedWithOptions([
37-
kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true
37+
kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true,
3838
] as CFDictionary)
3939
setupQuitOnUpdate()
4040
setupQuitOnUserTerminated()
@@ -105,7 +105,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
105105
}
106106

107107
@objc func quit() {
108-
exit(0)
108+
Task { @MainActor in
109+
await scheduledCleaner.closeAllChildProcesses()
110+
exit(0)
111+
}
109112
}
110113

111114
@objc func openCopilotForXcode() {
@@ -148,7 +151,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
148151
Logger.service.info("Extension Service will quit.")
149152
#if DEBUG
150153
#else
151-
exit(0)
154+
quit()
152155
#endif
153156
}
154157
}
@@ -172,7 +175,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
172175
if NSWorkspace.shared.runningApplications.contains(where: \.isUserOfService) {
173176
continue
174177
}
175-
exit(0)
178+
quit()
176179
}
177180
}
178181
}

0 commit comments

Comments
 (0)