Skip to content

Commit c75a309

Browse files
authored
Merge pull request #1135 from ahoppen/ahoppen/fix-leaks
Fix memory leaks
2 parents 1fe7978 + 86e1878 commit c75a309

File tree

8 files changed

+17
-23
lines changed

8 files changed

+17
-23
lines changed

Sources/SKCore/BuildSystem.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public protocol BuildSystem: AnyObject {
5252
/// Path remappings for remapping index data for local use.
5353
var indexPrefixMappings: [PathPrefixMapping] { get async }
5454

55-
/// Delegate to handle any build system events such as file build settings
56-
/// initial reports as well as changes.
55+
/// Delegate to handle any build system events such as file build settings initial reports as well as changes.
56+
///
57+
/// The build system must not retain the delegate because the delegate can be the `BuildSystemManager`, which could
58+
/// result in a retain cycle `BuildSystemManager` -> `BuildSystem` -> `BuildSystemManager`.
5759
var delegate: BuildSystemDelegate? { get async }
5860

5961
/// Set the build system's delegate.

Sources/SKCore/BuildSystemDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import BuildServerProtocol
1313
import LanguageServerProtocol
1414

15-
/// Handles build system events, such as file build settings changes.
15+
/// Handles build system events, such as file build settings changes.
1616
public protocol BuildSystemDelegate: AnyObject {
1717
/// Notify the delegate that the build targets have changed.
1818
///

Sources/SKCore/BuildSystemManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ extension BuildSystemManager: BuildSystemDelegate {
238238
}
239239
}
240240

241-
extension BuildSystemManager: MainFilesDelegate {
241+
extension BuildSystemManager {
242242
// FIXME: Consider debouncing/limiting this, seems to trigger often during a build.
243243
/// Checks if there are any files in `mainFileAssociations` where the main file
244244
/// that we have stored has changed.

Sources/SKCore/MainFilesProvider.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,3 @@ public protocol MainFilesProvider: AnyObject {
2525
/// ```
2626
func mainFilesContainingFile(_: DocumentURI) async -> Set<DocumentURI>
2727
}
28-
29-
/// Delegate that responds to possible main file changes.
30-
public protocol MainFilesDelegate: AnyObject {
31-
32-
/// The mapping from files to main files (may have) changed.
33-
func mainFilesChanged() async
34-
}

Sources/SourceKitLSP/SourceKitIndexDelegate.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public actor SourceKitIndexDelegate: IndexDelegate {
2323
let queue = AsyncQueue<Serial>()
2424

2525
/// Registered `MainFilesDelegate`s to notify when main files change.
26-
var mainFilesDelegates: [MainFilesDelegate] = []
26+
var mainFilesChangedCallbacks: [() async -> Void] = []
2727

2828
/// The count of pending unit events. Whenever this transitions to 0, it represents a time where
2929
/// the index finished processing known events. Of course, that may have already changed by the
@@ -62,20 +62,16 @@ public actor SourceKitIndexDelegate: IndexDelegate {
6262
}
6363

6464
func _indexChanged() {
65-
for delegate in mainFilesDelegates {
65+
for callback in mainFilesChangedCallbacks {
6666
queue.async {
67-
await delegate.mainFilesChanged()
67+
await callback()
6868
}
6969
}
7070
}
7171

7272
/// Register a delegate to receive notifications when main files change.
73-
public func registerMainFileChanged(_ delegate: MainFilesDelegate) {
74-
mainFilesDelegates.append(delegate)
73+
public func addMainFileChangedCallback(_ callback: @escaping () async -> Void) {
74+
mainFilesChangedCallbacks.append(callback)
7575
}
7676

77-
/// Un-register a delegate to receive notifications when main files change.
78-
public func unregisterMainFileChanged(_ delegate: MainFilesDelegate) {
79-
mainFilesDelegates.removeAll(where: { $0 === delegate })
80-
}
8177
}

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,8 @@ extension SourceKitServer {
11291129
buildSetup: self.options.buildSetup.merging(workspaceBuildSetup),
11301130
compilationDatabaseSearchPaths: self.options.compilationDatabaseSearchPaths,
11311131
indexOptions: self.options.indexOptions,
1132-
reloadPackageStatusCallback: { status in
1132+
reloadPackageStatusCallback: { [weak self] status in
1133+
guard let self else { return }
11331134
guard capabilityRegistry.clientCapabilities.window?.workDoneProgress ?? false else {
11341135
// Client doesn’t support work done progress
11351136
return

Sources/SourceKitLSP/Workspace.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public final class Workspace {
8888
fallbackBuildSystem: FallbackBuildSystem(buildSetup: buildSetup),
8989
mainFilesProvider: index
9090
)
91-
await indexDelegate?.registerMainFileChanged(buildSystemManager)
91+
await indexDelegate?.addMainFileChangedCallback { [weak self] in
92+
await self?.buildSystemManager.mainFilesChanged()
93+
}
9294
}
9395

9496
/// Creates a workspace for a given root `URL`, inferring the `ExternalWorkspace` if possible.

Tests/SKCoreTests/BuildSystemManagerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class ManualBuildSystem: BuildSystem {
429429

430430
var map: [DocumentURI: FileBuildSettings] = [:]
431431

432-
var delegate: BuildSystemDelegate? = nil
432+
weak var delegate: BuildSystemDelegate? = nil
433433

434434
func setDelegate(_ delegate: SKCore.BuildSystemDelegate?) async {
435435
self.delegate = delegate

0 commit comments

Comments
 (0)