From e4cdce545fa95507d423f55a745e0efc61fb2560 Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Mon, 25 Aug 2025 13:00:21 -0500 Subject: [PATCH] Correctly Handle Language Server Timeout When Quitting --- CodeEdit/AppDelegate.swift | 4 ++-- .../Features/LSP/LanguageServer/LanguageServer.swift | 4 +--- CodeEdit/Utils/withTimeout.swift | 12 ++++++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CodeEdit/AppDelegate.swift b/CodeEdit/AppDelegate.swift index 5581ef7fc2..124e7fb4b5 100644 --- a/CodeEdit/AppDelegate.swift +++ b/CodeEdit/AppDelegate.swift @@ -270,8 +270,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { TaskNotificationHandler.postTask(action: .create, model: task) } - try await withTimeout( - duration: .seconds(5.0), + try? await withTimeout( + duration: .seconds(2.0), onTimeout: { // Stop-gap measure to ensure we don't hang on CMD-Q await self.lspService.killAllServers() diff --git a/CodeEdit/Features/LSP/LanguageServer/LanguageServer.swift b/CodeEdit/Features/LSP/LanguageServer/LanguageServer.swift index d1c87f0eeb..7f855bbf01 100644 --- a/CodeEdit/Features/LSP/LanguageServer/LanguageServer.swift +++ b/CodeEdit/Features/LSP/LanguageServer/LanguageServer.swift @@ -258,9 +258,7 @@ class LanguageServer { /// Shuts down the language server and exits it. public func shutdown() async throws { self.logger.info("Shutting down language server") - try await withTimeout(duration: .seconds(1.0)) { - try await self.lspInstance.shutdownAndExit() - } + try await self.lspInstance.shutdownAndExit() } } diff --git a/CodeEdit/Utils/withTimeout.swift b/CodeEdit/Utils/withTimeout.swift index 2ebd9b406e..9db61b69b4 100644 --- a/CodeEdit/Utils/withTimeout.swift +++ b/CodeEdit/Utils/withTimeout.swift @@ -30,7 +30,7 @@ public func withTimeout( } // Start timeout child task. group.addTask { - if .now > deadline { + if .now < deadline { try await Task.sleep(until: deadline) // sleep until the deadline } try Task.checkCancellation() @@ -39,8 +39,12 @@ public func withTimeout( throw TimedOutError() } // First finished child task wins, cancel the other task. - let result = try await group.next()! - group.cancelAll() - return result + defer { group.cancelAll() } + do { + let result = try await group.next()! + return result + } catch { + throw error + } } }