Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(sync): guard finalization against externally-staled syncs (#2680)

Guillem Arias Fausteab14944a9ff3
diff --git a/app/models/sync.rb b/app/models/sync.rb
index c78788bfdc..eb0cd4c845 100644
--- a/app/models/sync.rb
+++ b/app/models/sync.rb
@@ -125,7 +125,7 @@ class Sync < ApplicationRecord
unless syncable.present?
Rails.logger.warn("Sync #{id} - syncable #{syncable_type}##{syncable_id} no longer exists. Marking as failed.")
start! if may_start?
- fail!
+ fail! if may_fail?
update(error: "Syncable record was deleted")
return
end
@@ -134,7 +134,7 @@ class Sync < ApplicationRecord
if syncable.respond_to?(:scheduled_for_deletion?) && syncable.scheduled_for_deletion?
Rails.logger.warn("Sync #{id} - syncable #{syncable_type}##{syncable_id} is scheduled for deletion. Skipping sync.")
start! if may_start?
- fail!
+ fail! if may_fail?
update(error: "Syncable record is scheduled for deletion")
return
end
@@ -144,7 +144,11 @@ class Sync < ApplicationRecord
begin
syncable.perform_sync(self)
rescue => e
- fail!
+ # Re-check state under a row lock (with_lock reloads): the sync may
+ # have been terminalized externally (marked stale by SyncCleanerJob)
+ # while this job was still running. An unguarded fail! on the in-memory
+ # record would silently overwrite that terminal status.
+ with_lock { fail! if may_fail? }
update(error: e.message)
report_error(e)
ensure
@@ -169,8 +173,11 @@ class Sync < ApplicationRecord
end
end
- # If we make it here, the sync is finalized. Run post-sync, regardless of failure/success.
- perform_post_sync
+ # If we make it here, the sync is finalized. Run post-sync, regardless of failure/success —
+ # unless the sync was terminalized externally (marked stale by SyncCleanerJob while its job
+ # was still running). A stale sync's job has been written off: re-running transfer matching,
+ # rules, and broadcasts for it would apply side effects for work the system already abandoned.
+ perform_post_sync unless stale?
end
# If this sync has a parent, try to finalize it so the child status propagates up the chain.
diff --git a/test/models/sync_test.rb b/test/models/sync_test.rb
index 92dcba2893..379750f124 100644
--- a/test/models/sync_test.rb
+++ b/test/models/sync_test.rb
@@ -179,6 +179,39 @@ class SyncTest < ActiveSupport::TestCase
assert_equal "completed", account_sync.reload.status
end
+ test "sync staled mid-run does not run post-sync when its job finishes" do
+ syncable = accounts(:depository)
+ sync = Sync.create!(syncable: syncable)
+
+ # Simulate SyncCleanerJob marking the sync stale while the job is still running
+ syncable.expects(:perform_sync).with { |s| Sync.find(s.id).mark_stale!; true }
+
+ Account.any_instance.expects(:perform_post_sync).never
+ Account.any_instance.expects(:broadcast_sync_complete).never
+
+ sync.perform
+
+ assert_equal "stale", sync.reload.status
+ end
+
+ test "sync staled mid-run does not raise when its job fails" do
+ syncable = accounts(:depository)
+ sync = Sync.create!(syncable: syncable)
+
+ syncable.expects(:perform_sync).with { |s| Sync.find(s.id).mark_stale!; true }
+ .raises(StandardError.new("provider blew up"))
+
+ Account.any_instance.expects(:perform_post_sync).never
+ Account.any_instance.expects(:broadcast_sync_complete).never
+
+ assert_nothing_raised do
+ sync.perform
+ end
+
+ assert_equal "stale", sync.reload.status
+ assert_equal "provider blew up", sync.error
+ end
+
test "clean marks stale incomplete rows" do
stale_pending = Sync.create!(
syncable: accounts(:depository),