Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/event_store/advisory_locks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,19 @@ defmodule EventStore.AdvisoryLocks do

notify_lost_locks(locks, reason)

{:noreply, %State{state | connection: nil, locks: %{}, released: [], verify_timer: nil}}
state = cancel_verify_retry(state)

{:noreply, %State{state | connection: nil, locks: %{}, released: []}}
end

# A session that could not be verified never reported a disconnect, so the
# pending unlocks are reconciled again once it can answer.
def handle_info(:verify_sessions, %State{} = state),
def handle_info({:timeout, ref, :verify_sessions}, %State{verify_timer: ref} = state),
do: {:noreply, verify_sessions(%State{state | verify_timer: nil})}

# A retry already cancelled or replaced would otherwise poll alongside the pending one.
def handle_info({:timeout, _ref, :verify_sessions}, %State{} = state), do: {:noreply, state}

# The connection pool outlives its connections, so a reconnect replaces the
# session that held the locks without the pool going down.
def handle_info({:connected, conn_pid}, %State{} = state) when is_pid(conn_pid),
Expand Down Expand Up @@ -511,7 +516,15 @@ defmodule EventStore.AdvisoryLocks do
defp schedule_verify_retry(%State{verify_timer: ref} = state) when is_reference(ref), do: state

defp schedule_verify_retry(%State{} = state),
do: %State{state | verify_timer: Process.send_after(self(), :verify_sessions, @verify_retry)}
do: %State{state | verify_timer: :erlang.start_timer(@verify_retry, self(), :verify_sessions)}

defp cancel_verify_retry(%State{verify_timer: nil} = state), do: state

defp cancel_verify_retry(%State{verify_timer: ref} = state) do
:erlang.cancel_timer(ref)

%State{state | verify_timer: nil}
end

defp notify_lost_locks(locks, _reason) when map_size(locks) == 0, do: :ok

Expand Down
32 changes: 32 additions & 0 deletions test/advisory_locks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,38 @@ defmodule EventStore.AdvisoryLocksTest do
Postgrex.query!(@conn, "ROLLBACK", [])
end

test "should ignore a verification retry that is no longer pending" do
{:ok, _lock} = AdvisoryLocks.try_advisory_lock(@locks, 1)

lock_connection = lock_connection_pid()
locks = Process.whereis(@locks)

Postgrex.query!(@conn, "BEGIN", [])
assert {:error, _error} = Postgrex.query(@conn, "SELECT 1 / 0", [])

send(@locks, {:connected, lock_connection})

assert %{verify_timer: timer} = :sys.get_state(@locks)
assert is_reference(timer)

# Nothing else talks to the lock server while the retry is pending, so the
# first message it receives is the retry firing.
:erlang.trace(locks, true, [:receive])
assert_receive {:trace, ^locks, :receive, retry}, 3_000
:erlang.trace(locks, false, [:receive])

assert %{verify_timer: pending} = :sys.get_state(@locks)
assert is_reference(pending) and pending != timer

# A retry that outlived its timer, such as one armed before the lock pool went
# down, would otherwise start a second polling loop next to the pending one.
send(@locks, retry)

assert %{verify_timer: ^pending} = :sys.get_state(@locks)

Postgrex.query!(@conn, "ROLLBACK", [])
end

test "should keep retrying an unlock that fails on a session that answers",
%{conn: conn, schema: schema} do
{:ok, lock} = AdvisoryLocks.try_advisory_lock(@locks, 1)
Expand Down
Loading