10000 Early CancelRequest handling by castwide · Pull Request #914 · castwide/solargraph · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Early CancelRequest handling #914

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
Apr 28, 2025
Merged
Show file tree
Hide file tree
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
20 changes: 10 additions & 10 deletions lib/solargraph/language_server/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ class Host
attr_writer :client_capabilities

def initialize
@cancel_semaphore = Mutex.new
@buffer_semaphore = Mutex.new
@request_mutex = Mutex.new
@cancel = []
@buffer = String.new
@stopped = true
@next_request_id = 1
Expand Down Expand Up @@ -65,7 +63,7 @@ def options
# @param id [Integer]
# @return [void]
def cancel id
@cancel_semaphore.synchronize { @cancel.push id }
cancelled.push id
end

# True if the host received a request to cancel the method with the
Expand All @@ -74,17 +72,15 @@ def cancel id
# @param id [Integer]
# @return [Boolean]
def cancel? id
result = false
@cancel_semaphore.synchronize { result = @cancel.include? id }
result
cancelled.include? id
end

# Delete the specified ID from the list of cancelled IDs if it exists.
#
# @param id [Integer]
# @return [void]
def clear id
@cancel_semaphore.synchronize { @cancel.delete id }
cancelled.delete id
end

# Called by adapter, to handle the request
Expand All @@ -101,11 +97,11 @@ def process request
# @return [Solargraph::LanguageServer::Message::Base, nil] The message handler.
def receive request
if request['method']
logger.info "Server received #{request['method']}"
logger.info "Host received ##{request['id']} #{request['method']}"
logger.debug request
message = Message.select(request['method']).new(self, request)
begin
message.process
message.process unless cancel?(request['id'])
rescue StandardError => e
logger.warn "Error processing request: [#{e.class}] #{e.message}"
logger.warn e.backtrace.join("\n")
Expand Down Expand Up @@ -381,7 +377,6 @@ def send_request method, params, &block
envelope = "Content-Length: #{json.bytesize}\r\n\r\n#{json}"
queue envelope
@next_request_id += 1
logger.info "Server sent #{method}"
logger.debug params
end
end
Expand Down Expand Up @@ -693,6 +688,11 @@ def client_supports_progress?

private

# @return [Array<Integer>]
def cancelled
@cancelled ||= []
end

# @return [MessageWorker]
def message_worker
@message_worker ||= MessageWorker.new(self)
Expand Down
26 changes: 20 additions & 6 deletions lib/solargraph/language_server/host/message_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
module Solargraph
module LanguageServer
class Host
# A serial worker Thread to handle message.
#
# this make check pending message possible, and maybe cancelled to speedup process
# A serial worker Thread to handle incoming messages.
#
class MessageWorker
UPDATE_METHODS = ['textDocument/didOpen', 'textDocument/didChange', 'workspace/didChangeWatchedFiles'].freeze
UPDATE_METHODS = [
'textDocument/didOpen',
'textDocument/didChange',
'workspace/didChangeWatchedFiles'
].freeze

# @param host [Host]
def initialize(host)
Expand Down Expand Up @@ -64,12 +66,24 @@ def tick
private

def next_message
cancel_message || next_priority
end

def cancel_message
# Handle cancellations first
idx = messages.find_index { |msg| msg['method'] == '$/cancelRequest' }
return unless idx

msg = messages[idx]
messages.delete_at idx
msg
end

def next_priority
# Prioritize updates and version-dependent messages for performance
idx = messages.find_index do |msg|
UPDATE_METHODS.include?(msg['method']) || version_dependent?(msg)
end
# @todo We might want to clear duplicate instances of this message
# that occur before the next update
return messages.shift unless idx

msg = messages[idx]
Expand Down
29 changes: 18 additions & 11 deletions lib/solargraph/language_server/message/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,11 @@ def set_error code, message
# @return [void]
def send_response
return if id.nil?
if host.cancel?(id)
# https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest
# cancel should send response RequestCancelled
Solargraph::Logging.logger.info "Cancelled response to #{method}"
set_result nil
set_error ErrorCodes::REQUEST_CANCELLED, "cancelled by client"
else
Solargraph::Logging.logger.info "Sending response to #{method}"
end

accept_or_cancel
response = {
jsonrpc: "2.0",
id: id,
jsonrpc: '2.0',
id: id
}
response[:result] = result unless result.nil?
response[:error] = error unless error.nil?
Expand All @@ -83,6 +76,20 @@ def send_response
host.queue envelope
host.clear id
end

private

def accept_or_cancel
if host.cancel?(id)
# https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest
# cancel should send response RequestCancelled
Solargraph::Logging.logger.info "Cancelled response to ##{id} #{method}"
set_result nil
set_error ErrorCodes::REQUEST_CANCELLED, 'Cancelled by client'
else
Solargraph::Logging.logger.info "Sending response to ##{id} #{method}"
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ def process
col = params['position']['character']
begin
completion = host.completions_at(params['textDocument']['uri'], line, col)
if host.cancel?(id)
return set_result(empty_result) if host.cancel?(id)
end
items = []
last_context = nil
idx = -1
Expand Down
0