From 2fd52ccbbf4b9116965bfe1f88ce74fc765c03ec Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Sun, 24 Mar 2024 13:26:51 +0100 Subject: [PATCH] fix parser crash when file is closed and debounce timeout has already put a message to process queue --- .../lib/language_server/parser.ex | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/apps/language_server/lib/language_server/parser.ex b/apps/language_server/lib/language_server/parser.ex index 657d2c571..1ba935b34 100644 --- a/apps/language_server/lib/language_server/parser.ex +++ b/apps/language_server/lib/language_server/parser.ex @@ -212,23 +212,29 @@ defmodule ElixirLS.LanguageServer.Parser do {:parse_file, uri}, %{files: files, debounce_refs: debounce_refs} = state ) do - file = Map.fetch!(files, uri) - version = file.source_file.version + state = + case files[uri] do + nil -> + # file got closed, no need to do anything + state + file -> + version = file.source_file.version - parent = self() + parent = self() - {pid, ref} = - spawn_monitor(fn -> - updated_file = do_parse(file) - send(parent, {:parse_file_done, uri, updated_file, nil}) - end) + {pid, ref} = + spawn_monitor(fn -> + updated_file = do_parse(file) + send(parent, {:parse_file_done, uri, updated_file, nil}) + end) - state = %{ - state - | debounce_refs: Map.delete(debounce_refs, uri), - parse_pids: put_nested(state.parse_pids, {uri, version}, ref, {pid, nil}), - parse_uris: Map.put(state.parse_uris, ref, {uri, version}) - } + %{ + state + | debounce_refs: Map.delete(debounce_refs, uri), + parse_pids: put_nested(state.parse_pids, {uri, version}, ref, {pid, nil}), + parse_uris: Map.put(state.parse_uris, ref, {uri, version}) + } + end {:noreply, state} end