Skip to content

Commit 328a02a

Browse files
committed
Fix a race condition that caused IndexProgressManager to create two WorkDoneProgress
1 parent 814f3c5 commit 328a02a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Sources/SourceKitLSP/IndexProgressManager.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ import SemanticIndex
1818
/// Listens for index status updates from `SemanticIndexManagers`. From that information, it manages a
1919
/// `WorkDoneProgress` that communicates the index progress to the editor.
2020
actor IndexProgressManager {
21+
/// A queue on which `indexTaskWasQueued` and `indexStatusDidChange` are handled.
22+
///
23+
/// This allows the two functions two be `nonisolated` (and eg. the caller of `indexStatusDidChange` doesn't have to
24+
/// wait for the work done progress to be updated) while still guaranteeing that there is only one
25+
/// `indexStatusDidChangeImpl` running at a time, preventing race conditions that would cause two
26+
/// `WorkDoneProgressManager`s to be created.
27+
private let queue = AsyncQueue<Serial>()
28+
2129
/// The `SourceKitLSPServer` for which this manages the index progress. It gathers all `SemanticIndexManagers` from
2230
/// the workspaces in the `SourceKitLSPServer`.
2331
private weak var sourceKitLSPServer: SourceKitLSPServer?
@@ -42,7 +50,7 @@ actor IndexProgressManager {
4250

4351
/// Called when a new file is scheduled to be indexed. Increments the target index count, eg. the 3 in `1/3`.
4452
nonisolated func indexTasksWereScheduled(count: Int) {
45-
Task {
53+
queue.async {
4654
await self.indexTasksWereScheduledImpl(count: count)
4755
}
4856
}
@@ -54,7 +62,7 @@ actor IndexProgressManager {
5462

5563
/// Called when a `SemanticIndexManager` finishes indexing a file. Adjusts the done index count, eg. the 1 in `1/3`.
5664
nonisolated func indexStatusDidChange() {
57-
Task {
65+
queue.async {
5866
await self.indexStatusDidChangeImpl()
5967
}
6068
}

Tests/SourceKitLSPTests/WorkspaceTestDiscoveryTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,6 @@ final class WorkspaceTestDiscoveryTests: XCTestCase {
902902
)
903903

904904
let testsAfterEdit = try await project.testClient.send(WorkspaceTestsRequest())
905-
print(testsAfterEdit)
906905
// We know from the semantic index that NotQuiteTest does not inherit from XCTestCase, so we should not include it.
907906
// We don't have any semantic knowledge about `OtherNotQuiteTest`, so we are conservative and should include it.
908907
XCTAssertFalse(testsAfterEdit.contains { $0.label == "NotQuiteTest" })

0 commit comments

Comments
 (0)