Skip to content

Commit

Permalink
fix: shutdown Postgrex connection on failure
Browse files Browse the repository at this point in the history
Handle errors during authentication query gracefully and shutdown
the client process to not leave the hanging connections.
  • Loading branch information
hauleth committed Nov 19, 2024
1 parent 3729f9f commit fe78f2e
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -957,22 +957,26 @@ defmodule Supavisor.ClientHandler do
ssl_opts: ssl_opts || []
)

# kill the postgrex connection if the current process exits unexpectedly
Process.link(conn)

Logger.debug(
"ClientHandler: Connected to db #{tenant.db_host} #{tenant.db_port} #{tenant.db_database} #{user.db_user}"
)
try do
Logger.debug(
"ClientHandler: Connected to db #{tenant.db_host} #{tenant.db_port} #{tenant.db_database} #{user.db_user}"
)

resp =
with {:ok, secret} <- Helpers.get_user_secret(conn, tenant.auth_query, db_user) do
t = if secret.digest == :md5, do: :auth_query_md5, else: :auth_query
{:ok, {t, fn -> Map.put(secret, :alias, user.db_user_alias) end}}
end
resp =
with {:ok, secret} <- Helpers.get_user_secret(conn, tenant.auth_query, db_user) do
t = if secret.digest == :md5, do: :auth_query_md5, else: :auth_query
{:ok, {t, fn -> Map.put(secret, :alias, user.db_user_alias) end}}
end

GenServer.stop(conn, :normal, 5_000)
Logger.info("ClientHandler: Get secrets finished")
resp
Logger.info("ClientHandler: Get secrets finished")
resp
rescue
exception ->
Logger.error("ClientHandler: Couldn't fetch user secrets from #{tenant.db_host}")
reraise exception, __STACKTRACE__
after
GenServer.stop(conn, :normal, 5_000)
end
end

@spec exchange_first(:password | :auth_query, fun(), binary(), binary(), binary()) ::
Expand Down

0 comments on commit fe78f2e

Please sign in to comment.