Skip to content

Commit 48851a4

Browse files
committed
Explicitly shut down the build server when SourceKit-LSP is shut down
The build server is automatically shut down using a background task when `BuildSystemManager` is deallocated. This, however, leads to possible race conditions where the shutdown task might not finish before the test is done, which could result in the connection being reported as a leak. To avoid this problem, we want to explicitly shut, down the build server when the `SourceKitLSPServer` gets shut down.
1 parent c96fff1 commit 48851a4

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Sources/BuildSystemIntegration/BuildSystemManager.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,23 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
410410
}
411411
}
412412

413+
/// Explicitly shut down the build server.
414+
///
415+
/// The build server is automatically shut down using a background task when `BuildSystemManager` is deallocated.
416+
/// This, however, leads to possible race conditions where the shutdown task might not finish before the test is done,
417+
/// which could result in the connection being reported as a leak. To avoid this problem, we want to explicitly shut
418+
/// down the build server when the `SourceKitLSPServer` gets shut down.
419+
package func shutdown() async {
420+
guard let buildSystemAdapter = await self.buildSystemAdapterAfterInitialized else {
421+
return
422+
}
423+
await orLog("Sending shutdown request to build server") {
424+
_ = try await buildSystemAdapter.send(BuildShutdownRequest())
425+
await buildSystemAdapter.send(OnBuildExitNotification())
426+
}
427+
self.buildSystemAdapter = nil
428+
}
429+
413430
deinit {
414431
// Shut down the build server before closing the connection to it
415432
Task { [buildSystemAdapter, initializeResult] in

Sources/BuildSystemIntegration/ExternalBuildSystemAdapter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ actor ExternalBuildSystemAdapter {
118118
}
119119

120120
/// Send a notification to the build server.
121-
func send(_ notification: some NotificationType) async {
121+
func send(_ notification: some NotificationType) {
122122
guard let connectionToBuildServer else {
123123
logger.error("Dropping notification because BSP server has crashed: \(notification.forLogging)")
124124
return

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,11 @@ extension SourceKitLSPServer {
12161216
await service.shutdown()
12171217
}
12181218
}
1219+
for workspace in workspaces {
1220+
taskGroup.addTask {
1221+
await workspace.buildSystemManager.shutdown()
1222+
}
1223+
}
12191224
}
12201225

12211226
// We have a semantic guarantee that no request or notification should be

0 commit comments

Comments
 (0)