From 8d21f388366ce5568b9ef7c5a03260ba0393e1d2 Mon Sep 17 00:00:00 2001 From: Ismael Abreu Date: Fri, 29 Sep 2023 18:40:13 +0100 Subject: [PATCH] Kill pending compilation tasks --- lib/next_ls/runtime.ex | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/next_ls/runtime.ex b/lib/next_ls/runtime.ex index b9b481f3..edbeaabb 100644 --- a/lib/next_ls/runtime.ex +++ b/lib/next_ls/runtime.ex @@ -198,6 +198,8 @@ defmodule NextLS.Runtime do end def handle_call({:compile, opts}, from, %{node: node} = state) do + stop_pending_compilations(state) + task = Task.Supervisor.async_nolink(state.task_supervisor, fn -> if opts[:force] do @@ -224,14 +226,15 @@ defmodule NextLS.Runtime do end end) - {:noreply, %{state | compiler_refs: Map.put(state.compiler_refs, task.ref, from)}} + {:noreply, %{state | compiler_refs: Map.put(state.compiler_refs, task.ref, {task.pid, from})}} end @impl GenServer def handle_info({ref, errors}, %{compiler_refs: compiler_refs} = state) when is_map_key(compiler_refs, ref) do Process.demonitor(ref, [:flush]) - GenServer.reply(compiler_refs[ref], errors) + orig = elem(compiler_refs[ref], 1) + GenServer.reply(orig, errors) {:noreply, %{state | compiler_refs: Map.delete(compiler_refs, ref)}} end @@ -274,6 +277,12 @@ defmodule NextLS.Runtime do {:noreply, state} end + defp stop_pending_compilations(%{compiler_refs: %{}}), do: :ok + defp stop_pending_compilations(%{compiler_refs: refs}) do + for {_ref, {task_pid, _from}} <- refs, do: Process.exit(task_pid, :kill) + :ok + end + defp connect(_node, _port, 0) do false end