@@ -177,26 +177,39 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
177177 indexTaskScheduler: TaskScheduler < AnyIndexTaskDescription >
178178 ) async {
179179 struct ConnectionToClient : BuildSystemManagerConnectionToClient {
180- let sourceKitLSPServer : SourceKitLSPServer
180+ weak var sourceKitLSPServer : SourceKitLSPServer ?
181181 func send( _ notification: some NotificationType ) async {
182+ guard let sourceKitLSPServer else {
183+ // `SourceKitLSPServer` has been destructed. We are tearing down the
184+ // language server. Nothing left to do.
185+ logger. error (
186+ " Ignoring notificaiton \( type ( of: notification) . method) because connection to editor has been closed "
187+ )
188+ return
189+ }
182190 await sourceKitLSPServer. waitUntilInitialized ( )
183191 sourceKitLSPServer. sendNotificationToClient ( notification)
184192 }
185193
186194 func send< R: RequestType > ( _ request: R ) async throws -> R . Response {
195+ guard let sourceKitLSPServer else {
196+ // `SourceKitLSPServer` has been destructed. We are tearing down the
197+ // language server. Nothing left to do.
198+ throw ResponseError . unknown ( " Connection to the editor closed " )
199+ }
187200 await sourceKitLSPServer. waitUntilInitialized ( )
188201 return try await sourceKitLSPServer. sendRequestToClient ( request)
189202 }
190203
191204 /// Whether the client can handle `WorkDoneProgress` requests.
192205 var clientSupportsWorkDoneProgress : Bool {
193206 get async {
194- await sourceKitLSPServer. capabilityRegistry? . clientCapabilities. window? . workDoneProgress ?? false
207+ await sourceKitLSPServer? . capabilityRegistry? . clientCapabilities. window? . workDoneProgress ?? false
195208 }
196209 }
197210
198211 func watchFiles( _ fileWatchers: [ FileSystemWatcher ] ) async {
199- await sourceKitLSPServer. watchFiles ( fileWatchers)
212+ await sourceKitLSPServer? . watchFiles ( fileWatchers)
200213 }
201214 }
202215
0 commit comments