Skip to content

Commit 1205f31

Browse files
authored
Fix suggest contracts race (#544)
* use get_source_file * fix race condition in suggest specs Code lens request is handled asynchronously. If the file is closed and close notification handle_info executes before suggest_contracts handle_call the previous code added an entry to awaiting_contracts. That entry used URI of a closed file that would later lead to crash when handling analysis_ready Fixes elixir-lsp/vscode-elixir-ls#186
1 parent ec11cd0 commit 1205f31

File tree

1 file changed

+10
-3
lines changed
  • apps/language_server/lib/language_server

1 file changed

+10
-3
lines changed

apps/language_server/lib/language_server/server.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,15 @@ defmodule ElixirLS.LanguageServer.Server do
128128

129129
{:reply, Dialyzer.suggest_contracts([abs_path]), state}
130130

131-
_ ->
131+
%{source_files: %{^uri => _}} ->
132+
# file not saved or analysis not finished
132133
awaiting_contracts = reject_awaiting_contracts(state.awaiting_contracts, uri)
133134

134135
{:noreply, %{state | awaiting_contracts: [{from, uri} | awaiting_contracts]}}
136+
137+
_ ->
138+
# file not or no longer open
139+
{:reply, [], state}
135140
end
136141
end
137142

@@ -556,8 +561,10 @@ defmodule ElixirLS.LanguageServer.Server do
556561
end
557562

558563
defp handle_request(implementation_req(_id, uri, line, character), state) do
564+
source_file = get_source_file(state, uri)
565+
559566
fun = fn ->
560-
Implementation.implementation(uri, state.source_files[uri].text, line, character)
567+
Implementation.implementation(uri, source_file.text, line, character)
561568
end
562569

563570
{:async, fun, state}
@@ -969,7 +976,7 @@ defmodule ElixirLS.LanguageServer.Server do
969976
{dirty, not_dirty} =
970977
state.awaiting_contracts
971978
|> Enum.split_with(fn {_, uri} ->
972-
state.source_files[uri].dirty?
979+
Map.fetch!(state.source_files, uri).dirty?
973980
end)
974981

975982
contracts_by_file =

0 commit comments

Comments
 (0)