Skip to content

Commit 9e98c48

Browse files
committed
Fix retain cycle between BuiltInBuildSystemAdapter and BuiltInBuildSystemAdapterDelegate
This was causing memory leaks.
1 parent 9473aeb commit 9e98c48

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Sources/BuildSystemIntegration/BuiltInBuildSystemAdapter.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import struct TSCBasic.RelativePath
2222

2323
// FIXME: (BSP Migration) This should be a MessageHandler once we have migrated all build system queries to BSP and can use
2424
// LocalConnection for the communication.
25-
protocol BuiltInBuildSystemAdapterDelegate: Sendable {
25+
protocol BuiltInBuildSystemAdapterDelegate: Sendable, AnyObject {
2626
func handle(_ notification: some NotificationType) async
2727
func handle<R: RequestType>(_ request: R) async throws -> R.Response
2828
}
@@ -74,7 +74,7 @@ package actor BuiltInBuildSystemAdapter: BuiltInBuildSystemMessageHandler {
7474
// FIXME: (BSP Migration) This should be private, all messages should go through BSP. Only accessible from the outside for transition
7575
// purposes.
7676
private(set) package var underlyingBuildSystem: BuiltInBuildSystem!
77-
private let messageHandler: any BuiltInBuildSystemAdapterDelegate
77+
private weak var messageHandler: (any BuiltInBuildSystemAdapterDelegate)?
7878

7979
init?(
8080
buildSystemKind: (WorkspaceType, projectRoot: AbsolutePath)?,
@@ -163,6 +163,10 @@ package actor BuiltInBuildSystemAdapter: BuiltInBuildSystemMessageHandler {
163163
\(notification.forLogging)
164164
"""
165165
)
166+
guard let messageHandler else {
167+
logger.error("Ignoring notificaiton \(notification.forLogging) because message handler has been deallocated")
168+
return
169+
}
166170
await messageHandler.handle(notification)
167171
}
168172

@@ -173,6 +177,9 @@ package actor BuiltInBuildSystemAdapter: BuiltInBuildSystemMessageHandler {
173177
\(request.forLogging)
174178
"""
175179
)
180+
guard let messageHandler else {
181+
throw ResponseError.unknown("Connection to SourceKit-LSP closed")
182+
}
176183
return try await messageHandler.handle(request)
177184
}
178185

0 commit comments

Comments
 (0)