Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(transfers): isolate concurrent transfer match in a savepoint (#2769)

pro395829c0a369d08f
diff --git a/app/models/family/auto_transfer_matchable.rb b/app/models/family/auto_transfer_matchable.rb
index 9ff6395fdc..e75155dbf2 100644
--- a/app/models/family/auto_transfer_matchable.rb
+++ b/app/models/family/auto_transfer_matchable.rb
@@ -43,14 +43,10 @@ module Family::AutoTransferMatchable
next if used_transaction_ids.include?(match.inflow_transaction_id) ||
used_transaction_ids.include?(match.outflow_transaction_id)
- begin
- Transfer.find_or_create_by!(
- inflow_transaction_id: match.inflow_transaction_id,
- outflow_transaction_id: match.outflow_transaction_id,
- )
- rescue ActiveRecord::RecordNotUnique
- # Another concurrent job created the transfer; safe to ignore
- end
+ # Skip this candidate when the transfer for this exact pair was not created
+ # (a concurrent sync claimed one of the transactions for a different pairing);
+ # marking it matched here would leave a transaction matched with no Transfer.
+ next unless find_or_create_transfer!(match)
inflow_transaction = transactions_by_id.fetch(match.inflow_transaction_id)
outflow_transaction = transactions_by_id.fetch(match.outflow_transaction_id)
@@ -80,6 +76,45 @@ module Family::AutoTransferMatchable
end
private
+ # Create the transfer for a matched candidate, tolerating a concurrent sync
+ # that already inserted the same pair.
+ #
+ # The insert runs in its own savepoint (requires_new: true). On PostgreSQL a
+ # failed statement aborts the entire surrounding transaction, so rescuing a
+ # RecordNotUnique raised by find_or_create_by! is not enough on its own: the
+ # next write would fail with PG::InFailedSqlTransaction and the remaining
+ # candidates would be silently dropped. Isolating the insert in a savepoint
+ # rolls back only the failed statement, leaving the outer transaction healthy.
+ def find_or_create_transfer!(match)
+ Transfer.transaction(requires_new: true) do
+ Transfer.find_or_create_by!(
+ inflow_transaction_id: match.inflow_transaction_id,
+ outflow_transaction_id: match.outflow_transaction_id,
+ )
+ end
+ rescue ActiveRecord::RecordNotUnique
+ # The composite unique index rejected the insert because this exact
+ # (inflow, outflow) pair was committed concurrently between our find and our
+ # insert. Return that committed row; if it is somehow absent, return nil so the
+ # caller skips rather than marking a transaction with no Transfer behind it.
+ existing_transfer(match)
+ rescue ActiveRecord::RecordInvalid => e
+ # The same race surfaces through the per-column uniqueness validation. Re-raise
+ # anything that is not a :taken on the transfer's transaction ids...
+ raise unless %i[inflow_transaction_id outflow_transaction_id].any? { |attr| e.record.errors.of_kind?(attr, :taken) }
+ # ...and even for :taken, only accept it once the exact (inflow, outflow) row is
+ # confirmed present; otherwise the :taken came from a different pairing.
+ existing_transfer(match)
+ end
+
+ # The committed transfer for this exact candidate pair, or nil if none exists.
+ def existing_transfer(match)
+ Transfer.find_by(
+ inflow_transaction_id: match.inflow_transaction_id,
+ outflow_transaction_id: match.outflow_transaction_id,
+ )
+ end
+
def coerce_transfer_match_date_window!(value)
Integer(value)
rescue ArgumentError, TypeError
diff --git a/test/models/family/auto_transfer_matchable_test.rb b/test/models/family/auto_transfer_matchable_test.rb
index 5951d94876..ab07b16164 100644
--- a/test/models/family/auto_transfer_matchable_test.rb
+++ b/test/models/family/auto_transfer_matchable_test.rb
@@ -19,6 +19,91 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCase
end
end
+ test "concurrent unique-index race does not abort the surrounding transaction" do
+ outflow_entry = create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 500)
+ inflow_entry = create_transaction(date: Date.current, account: @credit_card, amount: -500)
+ inflow_id = inflow_entry.entryable_id
+
+ # A separate matched pair whose transfer already exists, standing in for the
+ # row a concurrent sync committed first. Colliding with its unique index gives
+ # us a genuine failing INSERT (not a synthetic raise) -- only that aborts the
+ # PostgreSQL transaction, so only that reproduces the bug.
+ rival_out = create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 250)
+ rival_in = create_transaction(date: Date.current, account: @credit_card, amount: -250)
+ rival = Transfer.create!(inflow_transaction_id: rival_in.entryable_id, outflow_transaction_id: rival_out.entryable_id)
+
+ # A second, non-conflicting candidate: matching must CONTINUE past the skipped
+ # collision and still create this transfer.
+ good_out = create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 700)
+ good_in = create_transaction(date: Date.current, account: @credit_card, amount: -700)
+
+ original = Transfer.method(:find_or_create_by!)
+ Transfer.singleton_class.send(:define_method, :find_or_create_by!) do |attributes|
+ if attributes[:inflow_transaction_id] == inflow_id
+ insert!({ inflow_transaction_id: rival.inflow_transaction_id, outflow_transaction_id: rival.outflow_transaction_id })
+ else
+ original.call(attributes)
+ end
+ end
+
+ begin
+ assert_nothing_raised { @family.auto_match_transfers! }
+ ensure
+ Transfer.singleton_class.send(:remove_method, :find_or_create_by!)
+ end
+
+ inflow_entry.reload
+ outflow_entry.reload
+
+ # The collision was with a DIFFERENT pair (the rival), so no Transfer exists for
+ # THIS pair: the match is skipped, not marked. The savepoint kept the surrounding
+ # transaction healthy (no abort asserted above).
+ assert_nil Transfer.find_by(inflow_transaction_id: inflow_id, outflow_transaction_id: outflow_entry.entryable_id)
+ refute_equal "funds_movement", inflow_entry.entryable.kind
+
+ # ...and matching did not stop at the skip: the non-conflicting candidate was
+ # still created and both its entries marked.
+ good_in.reload
+ good_out.reload
+ assert Transfer.exists?(inflow_transaction_id: good_in.entryable_id, outflow_transaction_id: good_out.entryable_id)
+ assert_equal "funds_movement", good_in.entryable.kind
+ assert_equal "cc_payment", good_out.entryable.kind
+ end
+
+ test "a :taken on one column from a different pairing is skipped, not marked" do
+ outflow_entry = create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 500)
+ inflow_entry = create_transaction(date: Date.current, account: @credit_card, amount: -500)
+
+ # A concurrent sync matched this inflow to a DIFFERENT outflow, so the per-column
+ # uniqueness validation raises :taken on inflow_transaction_id -- but no Transfer
+ # exists for THIS (inflow, outflow) pair.
+ invalid = Transfer.new(inflow_transaction_id: inflow_entry.entryable_id, outflow_transaction_id: outflow_entry.entryable_id)
+ invalid.errors.add(:inflow_transaction_id, :taken)
+ Transfer.stubs(:find_or_create_by!).raises(ActiveRecord::RecordInvalid.new(invalid))
+
+ assert_nothing_raised { @family.auto_match_transfers! }
+
+ inflow_entry.reload
+ outflow_entry.reload
+
+ # No Transfer was created for this pair, so neither transaction may be marked.
+ assert_nil Transfer.find_by(inflow_transaction_id: inflow_entry.entryable_id, outflow_transaction_id: outflow_entry.entryable_id)
+ refute_equal "funds_movement", inflow_entry.entryable.kind
+ refute_equal "cc_payment", outflow_entry.entryable.kind
+ end
+
+ test "non-uniqueness validation failure during matching is not swallowed" do
+ create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 500)
+ create_transaction(date: Date.current, account: @credit_card, amount: -500)
+
+ # A genuine (non-race) validation failure must surface, not be skipped silently.
+ invalid = Transfer.new
+ invalid.errors.add(:base, :different_accounts)
+ Transfer.stubs(:find_or_create_by!).raises(ActiveRecord::RecordInvalid.new(invalid))
+
+ assert_raises(ActiveRecord::RecordInvalid) { @family.auto_match_transfers! }
+ end
+
test "auto-matches multi-currency transfers" do
load_exchange_prices
create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 500)