From cc7031ef5d533dcef2ca0de0b1f3f396fa09f1b6 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sun, 25 Oct 2026 13:59:19 -0400 Subject: [PATCH 1/2] test(subscriptions): show a long table lock on subscriptions drops every lock on the node Signed-off-by: Yordis Prieto --- test/advisory_locks_test.exs | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/advisory_locks_test.exs b/test/advisory_locks_test.exs index a68707df..b1975c8a 100644 --- a/test/advisory_locks_test.exs +++ b/test/advisory_locks_test.exs @@ -143,6 +143,31 @@ defmodule EventStore.AdvisoryLocksTest do AdvisoryLocks.try_subscription_lock(@locks, subscription_id) end + test "should keep every lock on the node while a table lock outlasts a claim", context do + %{conn: conn, schema: schema} = context + %{subscription_id: held_id} = create_subscription(context) + + {:ok, %AdvisoryLocks.Lease{ref: ref}} = AdvisoryLocks.try_subscription_lock(@locks, held_id) + + other = create_subscription(context, "other_subscription") + + table_lock = lock_subscriptions_table(context) + + try do + assert {:error, %Postgrex.Error{postgres: %{code: :lock_not_available}}} = + AdvisoryLocks.try_subscription_lock(@locks, other.subscription_id, 1_000) + after + release_table_lock(table_lock) + end + + _state = :sys.get_state(@locks) + + refute_received {AdvisoryLocks, :lock_released, ^ref, _reason} + + assert {:error, :lock_already_taken} = + Storage.Lock.try_acquire_exclusive_lock(conn, held_id, schema: schema) + end + test "should fail to acquire the lock of a subscription that does not exist" do assert {:error, :subscription_not_found} = AdvisoryLocks.try_subscription_lock(@locks, 999_999) @@ -941,6 +966,33 @@ defmodule EventStore.AdvisoryLocksTest do holder end + defp lock_subscriptions_table(%{conn: conn, schema: schema}) do + reply_to = self() + + table_lock = + Task.async(fn -> + Postgrex.transaction(conn, fn holder_conn -> + Postgrex.query!(holder_conn, "LOCK TABLE \"#{schema}\".subscriptions", []) + + send(reply_to, :table_locked) + + receive do + :release -> :ok + end + end) + end) + + assert_receive :table_locked + + table_lock + end + + defp release_table_lock(%Task{pid: pid} = table_lock) do + send(pid, :release) + + {:ok, :ok} = Task.await(table_lock) + end + defp wait_until_waiting_for_connection do locks = Process.whereis(@locks) From c114db12d23773705117247e6b538822ab94be3d Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sun, 25 Oct 2026 14:28:10 -0400 Subject: [PATCH 2/2] fix(subscriptions): keep a long table lock on subscriptions from dropping every lock on the node Signed-off-by: Yordis Prieto --- lib/event_store/sql/statements.ex | 1 + .../sql/statements/bound_lock_wait.sql.eex | 5 +++ lib/event_store/storage/lock.ex | 37 ++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/event_store/sql/statements/bound_lock_wait.sql.eex diff --git a/lib/event_store/sql/statements.ex b/lib/event_store/sql/statements.ex index ce2a1fdc..71457dcc 100644 --- a/lib/event_store/sql/statements.ex +++ b/lib/event_store/sql/statements.ex @@ -21,6 +21,7 @@ defmodule EventStore.Sql.Statements do {:try_advisory_lock, [:schema]}, {:try_advisory_session_lock, [:schema]}, {:advisory_unlock, [:schema]}, + {:bound_lock_wait, []}, {:claim_subscription_lock, [:schema]}, {:subscription_ack, [:schema]}, {:insert_snapshot, [:schema]}, diff --git a/lib/event_store/sql/statements/bound_lock_wait.sql.eex b/lib/event_store/sql/statements/bound_lock_wait.sql.eex new file mode 100644 index 00000000..42e15556 --- /dev/null +++ b/lib/event_store/sql/statements/bound_lock_wait.sql.eex @@ -0,0 +1,5 @@ +SELECT set_config('lock_timeout', $1::bigint::text, true) +FROM pg_settings +WHERE name = 'lock_timeout' + AND $1::bigint > 0 + AND (setting::bigint = 0 OR setting::bigint > $1::bigint); diff --git a/lib/event_store/storage/lock.ex b/lib/event_store/storage/lock.ex index 0c4301a3..2e8ec06a 100644 --- a/lib/event_store/storage/lock.ex +++ b/lib/event_store/storage/lock.ex @@ -6,6 +6,8 @@ defmodule EventStore.Storage.Lock do alias EventStore.Storage.Session alias EventStore.Subscriptions.FencingToken + @default_timeout 15_000 + @doc """ Use two 32-bit key values for advisory locks where the first key acts as the namespace. @@ -61,7 +63,12 @@ defmodule EventStore.Storage.Lock do query = Statements.claim_subscription_lock(schema) - case Postgrex.query(conn, query, [subscription_id], opts) do + reply = + with_bounded_lock_wait(conn, opts, fn conn -> + Postgrex.query(conn, query, [subscription_id], opts) + end) + + case reply do {:ok, %Postgrex.Result{rows: [[false, _backend_pid, _session_id, _generation, _last_seen]]}} -> {:error, :lock_already_taken} @@ -96,4 +103,32 @@ defmodule EventStore.Storage.Lock do {:error, _error} = reply -> reply end end + + # A query that outlives its timeout drops the connection along with every lock + # the session holds, so waiting on a lock held on the subscriptions table gives + # up well before that. + defp with_bounded_lock_wait(conn, opts, fun) do + Postgrex.transaction( + conn, + fn conn -> + with {:ok, _result} <- + Postgrex.query(conn, Statements.bound_lock_wait(), [lock_wait(opts)], opts), + {:ok, result} <- fun.(conn) do + result + else + {:error, error} -> Postgrex.rollback(conn, error) + end + end, + opts + ) + rescue + error in [DBConnection.ConnectionError, Postgrex.Error] -> {:error, error} + end + + defp lock_wait(opts) do + case Keyword.get(opts, :timeout, @default_timeout) do + :infinity -> 0 + timeout -> div(timeout, 2) + end + end end