Skip to content

Commit

Permalink
Fix connection error handling (#2328)
Browse files Browse the repository at this point in the history
The "passthrough" trick didn't work as planned since the error handling
code has been moved around a bit after it got introduced.

To prevent a recursive call loop, we just need to replace the recursive
call with different function call.
  • Loading branch information
alco authored Feb 12, 2025
1 parent bc0f3eb commit 29d73f1
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/sync-service/lib/electric/connection/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ defmodule Electric.Connection.Manager do
end

defp handle_connection_error(
%DBConnection.ConnectionError{message: message, severity: :error},
%DBConnection.ConnectionError{message: message, severity: :error} = error,
%State{connection_opts: connection_opts, ipv6_enabled: true} = state,
mode
) do
Expand All @@ -565,12 +565,17 @@ defmodule Electric.Connection.Manager do
step = current_connection_step(state)
handle_continue(step, state)
else
# Pass through other errors to avoid infinite loop
handle_connection_error({:passthrough, message}, state, mode)
fail_on_error_or_reconnect(error, state, mode)
end
end

defp handle_connection_error(error, state, mode) do
fail_on_error_or_reconnect(error, state, mode)
end

# This separate function is needed for `handle_connection_error()` not to get stuck in a
# recursive function call loop.
defp fail_on_error_or_reconnect(error, state, mode) do
with false <- stop_if_fatal_error(error, state) do
state = schedule_reconnection_after_error(error, state, mode)
{:noreply, state}
Expand All @@ -588,9 +593,6 @@ defmodule Electric.Connection.Manager do

%Postgrex.Error{postgres: %{message: message} = pg_error} ->
message <> pg_error_extra_info(pg_error)

{:passthrough, message} ->
message
end

Logger.warning("Database connection in #{mode} mode failed: #{message}")
Expand Down

0 comments on commit 29d73f1

Please sign in to comment.