Skip to content

Commit ece8634

Browse files
committed
Keep track of recently finished requests
This is only used so we don't log an error when receiving a `CancelRequestNotification` for a request that has just returned a response.
1 parent e4d8331 commit ece8634

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,22 @@ public actor SourceKitLSPServer {
176176
/// Used to cancel the tasks if the client requests cancellation.
177177
private var inProgressRequests: [RequestID: Task<(), Never>] = [:]
178178

179+
/// Up to 10 request IDs that have recently finished.
180+
///
181+
/// This is only used so we don't log an error when receiving a `CancelRequestNotification` for a request that has
182+
/// just returned a response.
183+
private var recentlyFinishedRequests: [RequestID] = []
184+
179185
/// - Note: Needed so we can set an in-progress request from a different
180186
/// isolation context.
181187
private func setInProgressRequest(for id: RequestID, task: Task<(), Never>?) {
182188
self.inProgressRequests[id] = task
189+
if task == nil {
190+
recentlyFinishedRequests.append(id)
191+
while recentlyFinishedRequests.count > 10 {
192+
recentlyFinishedRequests.removeFirst()
193+
}
194+
}
183195
}
184196

185197
var onExit: () -> Void
@@ -1191,16 +1203,15 @@ extension SourceKitLSPServer {
11911203
// Since the request is very cheap to execute and stops other requests
11921204
// from performing more work, we execute it with a high priority.
11931205
cancellationMessageHandlingQueue.async(priority: .high) {
1194-
guard let task = await self.inProgressRequests[notification.id] else {
1206+
if let task = await self.inProgressRequests[notification.id] {
1207+
task.cancel()
1208+
return
1209+
}
1210+
if await !self.recentlyFinishedRequests.contains(notification.id) {
11951211
logger.error(
1196-
"""
1197-
Cannot cancel request \(notification.id, privacy: .public) because it hasn't been scheduled for execution \
1198-
yet or because the request already returned a response
1199-
"""
1212+
"Cannot cancel request \(notification.id, privacy: .public) because it hasn't been scheduled for execution yet"
12001213
)
1201-
return
12021214
}
1203-
task.cancel()
12041215
}
12051216
}
12061217

0 commit comments

Comments
 (0)