From a16bcb4a4d09ba78c20985ded0fa9998d3f5d459 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sun, 25 Oct 2026 14:37:09 -0400 Subject: [PATCH] test(subscriptions): keep the zombie lock test from terminating a session that holds some other advisory lock Signed-off-by: Yordis Prieto --- .../subscription_zombie_lock_test.exs | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/test/subscriptions/subscription_zombie_lock_test.exs b/test/subscriptions/subscription_zombie_lock_test.exs index 3d5f13c1..b38743a9 100644 --- a/test/subscriptions/subscription_zombie_lock_test.exs +++ b/test/subscriptions/subscription_zombie_lock_test.exs @@ -28,7 +28,13 @@ defmodule EventStore.Subscriptions.SubscriptionZombieLockTest do end test "a node that dies dirty cannot move the progress of the owner that replaced it", - %{conn: conn, schema: schema, proxy: proxy, subscription_name: subscription_name} do + %{ + conn: conn, + postgrex_config: postgrex_config, + schema: schema, + proxy: proxy, + subscription_name: subscription_name + } do {:ok, subscription_a} = TestEventStore.subscribe_to_all_streams(subscription_name, self(), name: @store_a, @@ -49,6 +55,8 @@ defmodule EventStore.Subscriptions.SubscriptionZombieLockTest do Wait.until(fn -> assert 3 == last_seen(conn, schema, subscription_name) end) assert 1 == lock_generation(conn, schema, subscription_name) + hold_unrelated_advisory_locks(conn, postgrex_config, schema, subscription_name) + :ok = TcpProxy.freeze(proxy) test_pid = self() @@ -75,7 +83,7 @@ defmodule EventStore.Subscriptions.SubscriptionZombieLockTest do refute_receive {:b, :subscribed, _subscription}, 2_500 assert 1 == lock_generation(conn, schema, subscription_name) - terminate_lock_holder(conn) + terminate_lock_holder(conn, schema, subscription_name) assert_receive {:b, :subscribed, ^subscription_b}, 5_000 assert_receive {:b, :events, [%{event_number: 4} | _]}, 5_000 @@ -106,18 +114,53 @@ defmodule EventStore.Subscriptions.SubscriptionZombieLockTest do assert 2 == lock_generation(conn, schema, subscription_name) end - defp terminate_lock_holder(conn) do + defp terminate_lock_holder(conn, schema, subscription_name) do %Postgrex.Result{rows: [[backend_pid]]} = Postgrex.query!( conn, - "SELECT pid FROM pg_locks WHERE locktype = 'advisory' AND granted", - [] + """ + SELECT l.pid + FROM pg_locks l + JOIN pg_database d ON d.oid = l.database + JOIN "#{schema}".subscriptions s ON s.subscription_id = l.objid::bigint + WHERE l.locktype = 'advisory' + AND l.granted + AND d.datname = current_database() + AND l.classid = '"#{schema}".subscriptions'::regclass::oid + AND s.subscription_name = $1 + """, + [subscription_name] ) %Postgrex.Result{rows: [[true]]} = Postgrex.query!(conn, "SELECT pg_terminate_backend($1)", [backend_pid]) end + # Advisory locks are server-wide, so sessions of other databases and other + # lock users of this one must not be mistaken for the subscription's owner. + defp hold_unrelated_advisory_locks(conn, postgrex_config, schema, subscription_name) do + %Postgrex.Result{rows: [[classid, subscription_id]]} = + Postgrex.query!( + conn, + """ + SELECT '"#{schema}".subscriptions'::regclass::oid::int, subscription_id::int + FROM "#{schema}".subscriptions + WHERE subscription_name = $1 + """, + [subscription_name] + ) + + other_database = + start_supervised!({Postgrex, Keyword.put(postgrex_config, :database, "postgres")}, + id: :other_database + ) + + same_database = start_supervised!({Postgrex, postgrex_config}, id: :same_database) + + Postgrex.query!(other_database, "SELECT pg_advisory_lock($1, $2)", [classid, subscription_id]) + Postgrex.query!(same_database, "SELECT pg_advisory_lock($1)", [subscription_id]) + end + defp assert_receive_through(last_event_number) do assert_receive {:events, events}