Skip to content

Commit 407d1c2

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 de4aa4d commit 407d1c2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Sources/BuildSystemIntegration/BuildSystemManager.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,23 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
399399
}
400400
}
401401

402+
/// Explicitly shut down the build server.
403+
///
404+
/// The build server is automatically shut down using a background task when `BuildSystemManager` is deallocated.
405+
/// This, however, leads to possible race conditions where the shutdown task might not finish before the test is done,
406+
/// which could result in the connection being reported as a leak. To avoid this problem, we want to explicitly shut
407+
/// down the build server when the `SourceKitLSPServer` gets shut down.
408+
package func shutdown() async {
409+
guard let connectionToBuildSystem = await connectionToBuildSystem else {
410+
return
411+
}
412+
await orLog("Sending shutdown request to build server") {
413+
_ = try await connectionToBuildSystem.send(BuildShutdownRequest())
414+
connectionToBuildSystem.send(OnBuildExitNotification())
415+
}
416+
_connectionToBuildSystem = nil
417+
}
418+
402419
deinit {
403420
// Shut down the build server before closing the connection to it
404421
Task { [connectionToBuildSystem = _connectionToBuildSystem, initializeResult] in

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,11 @@ extension SourceKitLSPServer {
12101210
await service.shutdown()
12111211
}
12121212
}
1213+
for workspace in workspaces {
1214+
taskGroup.addTask {
1215+
await workspace.buildSystemManager.shutdown()
1216+
}
1217+
}
12131218
}
12141219

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

0 commit comments

Comments
 (0)