Skip to content

Keep track of recently finished requests #1421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,22 @@ public actor SourceKitLSPServer {
/// Used to cancel the tasks if the client requests cancellation.
private var inProgressRequests: [RequestID: Task<(), Never>] = [:]

/// Up to 10 request IDs that have recently finished.
///
/// This is only used so we don't log an error when receiving a `CancelRequestNotification` for a request that has
/// just returned a response.
private var recentlyFinishedRequests: [RequestID] = []

/// - Note: Needed so we can set an in-progress request from a different
/// isolation context.
private func setInProgressRequest(for id: RequestID, task: Task<(), Never>?) {
self.inProgressRequests[id] = task
if task == nil {
recentlyFinishedRequests.append(id)
while recentlyFinishedRequests.count > 10 {
recentlyFinishedRequests.removeFirst()
}
}
}

var onExit: () -> Void
Expand Down Expand Up @@ -1191,16 +1203,15 @@ extension SourceKitLSPServer {
// Since the request is very cheap to execute and stops other requests
// from performing more work, we execute it with a high priority.
cancellationMessageHandlingQueue.async(priority: .high) {
guard let task = await self.inProgressRequests[notification.id] else {
if let task = await self.inProgressRequests[notification.id] {
task.cancel()
return
}
if await !self.recentlyFinishedRequests.contains(notification.id) {
logger.error(
"""
Cannot cancel request \(notification.id, privacy: .public) because it hasn't been scheduled for execution \
yet or because the request already returned a response
"""
"Cannot cancel request \(notification.id, privacy: .public) because it hasn't been scheduled for execution yet"
)
return
}
task.cancel()
}
}

Expand Down