diff --git a/VERSION b/VERSION index c1847bf9..ba7b2f76 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.31 +1.1.32 diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index a61e04cf..c76957b7 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -548,10 +548,18 @@ defmodule Supavisor.ClientHandler do end def terminate(reason, _state, %{db_pid: {_, pid}}) do - resp = Db.client_termination(pid) + db_info = + case Db.get_state_and_mode(pid) do + {:ok, {state, mode} = resp} -> + if state == :busy || mode == :session, do: Db.stop(pid) + resp + + error -> + error + end Logger.warning( - "ClientHandler: socket closed with reason #{inspect(reason)}, DbHandler #{inspect(pid)} response #{inspect(resp)}" + "ClientHandler: socket closed with reason #{inspect(reason)}, DbHandler #{inspect({pid, db_info})}" ) :ok diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index bed32fea..b0072982 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -27,15 +27,18 @@ defmodule Supavisor.DbHandler do @spec call(pid(), pid(), binary()) :: :ok | {:error, any()} | {:buffering, non_neg_integer()} def call(pid, caller, msg), do: :gen_statem.call(pid, {:db_call, caller, msg}, 15_000) - @spec client_termination(pid()) :: {:ok, :ignore | :stopping} | {:error, any()} - def client_termination(pid) do + @spec get_state_and_mode(pid()) :: {:ok, {state, Supavisor.mode()}} | {:error, term()} + def get_state_and_mode(pid) do try do - {:ok, :gen_statem.call(pid, :client_termination, 5_000)} + {:ok, :gen_statem.call(pid, :get_state_and_mode, 5_000)} catch error, reason -> {:error, {error, reason}} end end + @spec stop(pid()) :: :ok + def stop(pid), do: :gen_statem.stop(pid, :client_termination, 5_000) + @impl true def init(args) do Process.flag(:trap_exit, true) @@ -407,14 +410,8 @@ defmodule Supavisor.DbHandler do end end - def handle_event({:call, from}, :client_termination, state, data) do - if state == :busy || data.mode == :session do - sock_send(data.sock, <>) - :gen_tcp.close(elem(data.sock, 1)) - {:stop_and_reply, {:client_termination, data.mode}, [{:reply, from, :stopping}]} - else - {:keep_state_and_data, {:reply, from, :ignore}} - end + def handle_event({:call, from}, :get_state_and_mode, state, data) do + {:keep_state_and_data, {:reply, from, {state, data.mode}}} end def handle_event(type, content, state, data) do