Skip to content

Commit 0f55306

Browse files
authored
Merge pull request #1421 from ahoppen/track-recently-finished-requests
Keep track of recently finished requests
2 parents e73768b + ece8634 commit 0f55306

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
@@ -177,10 +177,22 @@ public actor SourceKitLSPServer {
177177
/// Used to cancel the tasks if the client requests cancellation.
178178
private var inProgressRequests: [RequestID: Task<(), Never>] = [:]
179179

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

186198
var onExit: () -> Void
@@ -1194,16 +1206,15 @@ extension SourceKitLSPServer {
11941206
// Since the request is very cheap to execute and stops other requests
11951207
// from performing more work, we execute it with a high priority.
11961208
cancellationMessageHandlingQueue.async(priority: .high) {
1197-
guard let task = await self.inProgressRequests[notification.id] else {
1209+
if let task = await self.inProgressRequests[notification.id] {
1210+
task.cancel()
1211+
return
1212+
}
1213+
if await !self.recentlyFinishedRequests.contains(notification.id) {
11981214
logger.error(
1199-
"""
1200-
Cannot cancel request \(notification.id, privacy: .public) because it hasn't been scheduled for execution \
1201-
yet or because the request already returned a response
1202-
"""
1215+
"Cannot cancel request \(notification.id, privacy: .public) because it hasn't been scheduled for execution yet"
12031216
)
1204-
return
12051217
}
1206-
task.cancel()
12071218
}
12081219
}
12091220

0 commit comments

Comments
 (0)