fix(sync): scope after_commit to prevent nil family error on destroy#1976
Draft
sentry[bot] wants to merge 1 commit into
Draft
fix(sync): scope after_commit to prevent nil family error on destroy#1976sentry[bot] wants to merge 1 commit into
sentry[bot] wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses a
NoMethodError: undefined method 'family' for niloccurring inSync#update_family_sync_timestamp.Problem:
When
InactiveFamilyCleanerJobdestroys aFamilyrecord, associatedSyncrecords are cascade-destroyed. Theafter_commit :update_family_sync_timestampcallback on theSyncmodel would then fire. Inside this callback, thefamilymethod attempts to accesssyncable.family. However, because thesyncable(the polymorphic association target) had already been destroyed in the same transaction,syncablewasnil, leading to theNoMethodError.Solution:
after_commitcallback: Theafter_commit :update_family_sync_timestampcallback inapp/models/sync.rb(line 22) was modified toafter_commit :update_family_sync_timestamp, on: [:create, :update]. This ensures the callback only runs when aSyncrecord is created or updated, and not when it is destroyed, as updatinglatest_sync_activity_atis not relevant during destruction.familymethod: Areturn nil unless syncableguard was added to the privatefamilymethod inSync(line 258). This provides a defense-in-depth mechanism, preventingNoMethodErrorifsyncableisnilfor any reason whenfamilyis called.update_family_sync_timestamp: Thereturn unless family.persisted?line inupdate_family_sync_timestamp(line 253) was updated toreturn unless family&.persisted?to safely handle cases where thefamilymethod might returnnildue to the new guard.Fixes SURE-APP-Y5