From aab1cb4fb8eab80694e4bbd2f8c6601602dbc74e Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sun, 25 Oct 2026 13:54:27 -0400 Subject: [PATCH 1/2] test(subscriptions): show a checkpoint racing a delete mistaking the gone row for a fenced one Signed-off-by: Yordis Prieto --- .../storage/subscription_persistence_test.exs | 47 +++++++++++++++++++ test/subscriptions/subscription_fsm_test.exs | 11 +++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/test/storage/subscription_persistence_test.exs b/test/storage/subscription_persistence_test.exs index 55774913..d7b6a0fa 100644 --- a/test/storage/subscription_persistence_test.exs +++ b/test/storage/subscription_persistence_test.exs @@ -4,6 +4,7 @@ defmodule EventStore.Storage.SubscriptionPersistenceTest do alias EventStore.Storage alias EventStore.Storage.Lock alias EventStore.Subscriptions.FencingToken + alias EventStore.Wait @all_stream "$all" @subscription_name "test_subscription" @@ -210,6 +211,52 @@ defmodule EventStore.Storage.SubscriptionPersistenceTest do assert {:ok, %Storage.Subscription{last_seen: 10}} = read_subscription(context) end + test "ack last seen event racing a delete reports the subscription as gone", context do + %{conn: conn, schema: schema, postgrex_config: postgrex_config} = context + + {:ok, %Storage.Subscription{subscription_id: subscription_id}} = subscribe_to_stream(context) + + token = claim_token(context, subscription_id) + + deleter = start_supervised!({Postgrex, postgrex_config}, id: :deleter) + observer = start_supervised!({Postgrex, postgrex_config}, id: :observer) + + test = self() + + delete = + Task.async(fn -> + Postgrex.transaction(deleter, fn deleter -> + :ok = + Storage.delete_subscription(deleter, @all_stream, @subscription_name, schema: schema) + + send(test, :deleted) + + receive do + :commit -> :ok + end + end) + end) + + assert_receive :deleted + + ack = Task.async(fn -> Storage.ack_last_seen_event(conn, token, 1, schema: schema) end) + + Wait.until(5_000, fn -> + assert %Postgrex.Result{rows: [[1]]} = + Postgrex.query!( + observer, + "SELECT count(*)::int FROM pg_stat_activity WHERE datname = current_database() AND wait_event_type = 'Lock'", + [] + ) + end) + + send(delete.pid, :commit) + + assert {:ok, :ok} = Task.await(delete) + assert {:error, :subscription_not_found} = Task.await(ack) + assert {:error, :subscription_not_found} = read_subscription(context) + end + defp read_subscription(context) do %{conn: conn, schema: schema} = context diff --git a/test/subscriptions/subscription_fsm_test.exs b/test/subscriptions/subscription_fsm_test.exs index 93066c43..153d875e 100644 --- a/test/subscriptions/subscription_fsm_test.exs +++ b/test/subscriptions/subscription_fsm_test.exs @@ -161,8 +161,7 @@ defmodule EventStore.Subscriptions.SubscriptionFsmTest do assert_received {:checkpoint_failed, :subscription_not_found} end - test "tells the subscription it is fenced when the probe that classifies the rejection fails", - context do + test "does not take a rejection it could not classify for a fence", context do %{conn: conn, schema: schema, subscription_id: subscription_id} = context probe_conn = start_supervised!({Postgrex, context.postgrex_config}, id: :probe_conn) @@ -180,10 +179,12 @@ defmodule EventStore.Subscriptions.SubscriptionFsmTest do drop_privileges_after_update(context) - assert %SubscriptionFsm{data: %SubscriptionState{checkpoints_pending: 0}} = - SubscriptionFsm.checkpoint(fsm) + assert %SubscriptionFsm{} = SubscriptionFsm.checkpoint(fsm) - assert_received {:fenced, ^lock_ref} + # Claiming the lock again would recreate a row that a delete removed, so only a probe that + # found the row may send the subscription to do that. + refute_received {:fenced, ^lock_ref} + refute_received {:checkpoint_failed, _reason} end test "does not write the position onto a subscription that reused the name", context do From 0715b615b9469e2dffc07393d3452570494d0630 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sun, 25 Oct 2026 14:10:20 -0400 Subject: [PATCH 2/2] fix(subscriptions): stop a checkpoint racing a delete from bringing the deleted subscription back Signed-off-by: Yordis Prieto --- lib/event_store/sql/statements.ex | 3 +- .../query_subscription_exists.sql.eex | 1 + .../sql/statements/subscription_ack.sql.eex | 11 ++----- lib/event_store/storage/subscription.ex | 31 +++++++++++++++---- 4 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 lib/event_store/sql/statements/query_subscription_exists.sql.eex diff --git a/lib/event_store/sql/statements.ex b/lib/event_store/sql/statements.ex index b165d7fa..c370e603 100644 --- a/lib/event_store/sql/statements.ex +++ b/lib/event_store/sql/statements.ex @@ -33,7 +33,8 @@ defmodule EventStore.Sql.Statements do {:query_stream_events_backward, [:schema]}, {:query_stream_events_forward, [:schema]}, {:query_streams, [:schema, :sort_by, :sort_dir]}, - {:query_subscription, [:schema]} + {:query_subscription, [:schema]}, + {:query_subscription_exists, [:schema]} ] do file = Path.expand("statements/#{fun}.sql.eex", __DIR__) diff --git a/lib/event_store/sql/statements/query_subscription_exists.sql.eex b/lib/event_store/sql/statements/query_subscription_exists.sql.eex new file mode 100644 index 00000000..1340fdeb --- /dev/null +++ b/lib/event_store/sql/statements/query_subscription_exists.sql.eex @@ -0,0 +1 @@ +SELECT EXISTS (SELECT 1 FROM "<%= schema %>".subscriptions WHERE subscription_id = $1); diff --git a/lib/event_store/sql/statements/subscription_ack.sql.eex b/lib/event_store/sql/statements/subscription_ack.sql.eex index ee3f9517..6dd8bdd9 100644 --- a/lib/event_store/sql/statements/subscription_ack.sql.eex +++ b/lib/event_store/sql/statements/subscription_ack.sql.eex @@ -1,8 +1,3 @@ -WITH progress AS ( - UPDATE "<%= schema %>".subscriptions - SET last_seen = GREATEST(last_seen, $2) - WHERE subscription_id = $1 AND lock_generation = $3 - RETURNING subscription_id -) -SELECT EXISTS (SELECT 1 FROM progress) AS acknowledged, - EXISTS (SELECT 1 FROM "<%= schema %>".subscriptions WHERE subscription_id = $1) AS present; +UPDATE "<%= schema %>".subscriptions +SET last_seen = GREATEST(last_seen, $2) +WHERE subscription_id = $1 AND lock_generation = $3; diff --git a/lib/event_store/storage/subscription.ex b/lib/event_store/storage/subscription.ex index 2d41f1b6..812a1f4b 100644 --- a/lib/event_store/storage/subscription.ex +++ b/lib/event_store/storage/subscription.ex @@ -148,26 +148,45 @@ defmodule EventStore.Storage.Subscription do # Naming the subscription by its identifier, rather than by the stream and name a later # subscription can reuse, is what keeps this from writing a checkpoint onto whichever row # holds that name now. - {:ok, %Postgrex.Result{rows: [[true, _present]]}} -> + {:ok, %Postgrex.Result{num_rows: 1}} -> :ok - {:ok, %Postgrex.Result{rows: [[false, true]]}} -> + {:ok, %Postgrex.Result{num_rows: 0}} -> + rejected(conn, subscription_id, schema, opts) + + {:error, error} = reply -> Logger.warning( - "Failed to ack last seen event for subscription #{subscription_id} because another lock owner has claimed it" + "Failed to ack last seen event for subscription #{subscription_id} due to: " <> + inspect(error) ) - {:error, :fenced} + reply + end + end - {:ok, %Postgrex.Result{rows: [[false, false]]}} -> + # A delete that commits while the update waits on its row is only visible to a statement that + # starts after it, so asking within the update would still find the deleted row. + defp rejected(conn, subscription_id, schema, opts) do + query = Statements.query_subscription_exists(schema) + + case Postgrex.query(conn, query, [subscription_id], opts) do + {:ok, %Postgrex.Result{rows: [[false]]}} -> Logger.warning( "Failed to ack last seen event for subscription #{subscription_id} because it no longer exists" ) {:error, :subscription_not_found} + {:ok, %Postgrex.Result{rows: [[true]]}} -> + Logger.warning( + "Failed to ack last seen event for subscription #{subscription_id} because another lock owner has claimed it" + ) + + {:error, :fenced} + {:error, error} = reply -> Logger.warning( - "Failed to ack last seen event for subscription #{subscription_id} due to: " <> + "Failed to ack last seen event for subscription #{subscription_id}, which refused the write, due to: " <> inspect(error) )