Skip to content

Commit

Permalink
fix: cleanly shutdown DbHandler on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
hauleth committed Nov 28, 2024
1 parent 77b248d commit c076e31
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,20 @@ defmodule Supavisor.ClientHandler do
}

{:ok, db_pid} = DbHandler.start_link(args)
db_sock = :gen_statem.call(db_pid, {:checkout, data.sock, self()})

db_sock =
try do
DbHandler.checkout(db_pid, data.sock)
rescue
err ->
Logger.error(
"ClientHandler: Error while connecting to the DB - #{Exception.message(err)}"
)

_ = DbHandler.stop(db_pid)
reraise err, __STACKTRACE__
end

{:keep_state, %{data | db_pid: {nil, db_pid, db_sock}, mode: :proxy}}
end

Expand Down Expand Up @@ -852,7 +865,7 @@ defmodule Supavisor.ClientHandler do
start = System.monotonic_time(:microsecond)
db_pid = :poolboy.checkout(data.pool, true, data.timeout)
Process.link(db_pid)
db_sock = DbHandler.checkout(db_pid, data.sock, self())
db_sock = DbHandler.checkout(db_pid, data.sock)
same_box = if node(db_pid) == node(), do: :local, else: :remote
Telem.pool_checkout_time(System.monotonic_time(:microsecond) - start, data.id, same_box)
{data.pool, db_pid, db_sock}
Expand Down
4 changes: 2 additions & 2 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ defmodule Supavisor.DbHandler do
def start_link(config),
do: :gen_statem.start_link(__MODULE__, config, hibernate_after: 5_000)

def checkout(pid, sock, caller, timeout \\ 15_000),
do: :gen_statem.call(pid, {:checkout, sock, caller}, timeout)
def checkout(pid, sock, timeout \\ 15_000),
do: :gen_statem.call(pid, {:checkout, sock, self()}, timeout)

@spec checkin(pid()) :: :ok
def checkin(pid), do: :gen_statem.cast(pid, :checkin)
Expand Down

0 comments on commit c076e31

Please sign in to comment.