Skip to content

Commit 86a08eb

Browse files
Correctly Handle Language Server Timeout When Quitting (#2110)
### Description There has been work done to ensure CodeEdit cleans up language servers correctly on quit, this fixes a few final bugs with that system. - Changes the `withTimeout` call's `try` to `try?` in the `AppDelegate` so a thrown timeout error still allows execution to continue. - Removes an unnecessary `withTimeout` in the language server shutdown code. - Ensures `withTimeout` always cancels all child tasks on either an error or a return value. ### Related Issues ### Checklist - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots
1 parent eb0a363 commit 86a08eb

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

CodeEdit/AppDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
270270
TaskNotificationHandler.postTask(action: .create, model: task)
271271
}
272272

273-
try await withTimeout(
274-
duration: .seconds(5.0),
273+
try? await withTimeout(
274+
duration: .seconds(2.0),
275275
onTimeout: {
276276
// Stop-gap measure to ensure we don't hang on CMD-Q
277277
await self.lspService.killAllServers()

CodeEdit/Features/LSP/LanguageServer/LanguageServer.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,7 @@ class LanguageServer<DocumentType: LanguageServerDocument> {
258258
/// Shuts down the language server and exits it.
259259
public func shutdown() async throws {
260260
self.logger.info("Shutting down language server")
261-
try await withTimeout(duration: .seconds(1.0)) {
262-
try await self.lspInstance.shutdownAndExit()
263-
}
261+
try await self.lspInstance.shutdownAndExit()
264262
}
265263
}
266264

CodeEdit/Utils/withTimeout.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public func withTimeout<R>(
3030
}
3131
// Start timeout child task.
3232
group.addTask {
33-
if .now > deadline {
33+
if .now < deadline {
3434
try await Task.sleep(until: deadline) // sleep until the deadline
3535
}
3636
try Task.checkCancellation()
@@ -39,8 +39,12 @@ public func withTimeout<R>(
3939
throw TimedOutError()
4040
}
4141
// First finished child task wins, cancel the other task.
42-
let result = try await group.next()!
43-
group.cancelAll()
44-
return result
42+
defer { group.cancelAll() }
43+
do {
44+
let result = try await group.next()!
45+
return result
46+
} catch {
47+
throw error
48+
}
4549
}
4650
}

0 commit comments

Comments
 (0)