Skip to content

Commit

Permalink
fix: stop db handler before client (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored Feb 2, 2024
1 parent ac02bde commit 7cbaa2a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.31
1.1.32
12 changes: 10 additions & 2 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 8 additions & 11 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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, <<?X, 4::32>>)
: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
Expand Down

0 comments on commit 7cbaa2a

Please sign in to comment.