Skip to content

Respond to LSP Shutdown command to prevent VS Code deadlocks #100

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 21, 2022
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
3 changes: 2 additions & 1 deletion lib/syntax_tree/language_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def run
write(id: id, result: { capabilities: capabilities })
in method: "initialized"
# ignored
in method: "shutdown"
in method: "shutdown" # tolerate missing ID to be a good citizen
store.clear
write(id: request[:id], result: {})
return
in {
method: "textDocument/didChange",
Expand Down
36 changes: 26 additions & 10 deletions test/language_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def to_hash
end
end

class Shutdown
class Shutdown < Struct.new(:id)
def to_hash
{ method: "shutdown" }
{ method: "shutdown", id: id }
end
end

Expand Down Expand Up @@ -107,13 +107,14 @@ def test_formatting
TextDocumentDidChange.new("file:///path/to/file.rb", "class Bar; end"),
TextDocumentFormatting.new(2, "file:///path/to/file.rb"),
TextDocumentDidClose.new("file:///path/to/file.rb"),
Shutdown.new
Shutdown.new(3)
]

case run_server(messages)
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] }
{ id: 2, result: [{ newText: new_text }] },
{ id: 3, result: {} }
]
assert_equal("class Bar\nend\n", new_text)
end
Expand All @@ -129,13 +130,14 @@ def test_inlay_hints
end
RUBY
TextDocumentInlayHints.new(2, "file:///path/to/file.rb"),
Shutdown.new
Shutdown.new(3)
]

case run_server(messages)
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: { before:, after: } }
{ id: 2, result: { before:, after: } },
{ id: 3, result: {} }
]
assert_equal(1, before.length)
assert_equal(2, after.length)
Expand All @@ -147,11 +149,15 @@ def test_visualizing
Initialize.new(1),
TextDocumentDidOpen.new("file:///path/to/file.rb", "1 + 2"),
SyntaxTreeVisualizing.new(2, "file:///path/to/file.rb"),
Shutdown.new
Shutdown.new(3)
]

case run_server(messages)
in [{ id: 1, result: { capabilities: Hash } }, { id: 2, result: }]
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: },
{ id: 3, result: {} }
]
assert_equal(
"(program (statements ((binary (int \"1\") + (int \"2\")))))\n",
result
Expand All @@ -167,13 +173,14 @@ def test_reading_file
messages = [
Initialize.new(1),
TextDocumentFormatting.new(2, "file://#{file.path}"),
Shutdown.new
Shutdown.new(3)
]

case run_server(messages)
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] }
{ id: 2, result: [{ newText: new_text }] },
{ id: 3, result: {} }
]
assert_equal("class Foo\nend\n", new_text)
end
Expand All @@ -186,6 +193,15 @@ def test_bogus_request
end
end

def test_clean_shutdown
messages = [Initialize.new(1), Shutdown.new(2)]

case run_server(messages)
in [{ id: 1, result: { capabilities: Hash } }, { id: 2, result: {} }]
assert_equal(true, true)
end
end

private

def write(content)
Expand Down