Fix crash and lost-update window when sync deletion candidates change concurrently - #256
Merged
Conversation
The deletion queryset is evaluated before any locks are taken, so a concurrent sync could change or remove an object before the loop reaches it. Now the object is re-fetched inside its lock and only deleted if it is still active and still absent from the remote set.
… as ignored The re-check under the lock now uses a single exists() query instead of re-fetching the instance with its related objects, and candidates skipped because they were concurrently deactivated or hard-deleted are counted as ignored so outcome counts still sum to the number of objects considered. Also documents the isolation assumptions the re-check relies on.
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.
In
sync_local_to_set, the set of local objects to deactivate is computed before any per-object locks are taken, so a concurrent sync can change an object before the deletion loop reaches it. This caused two problems:delete_local'ssave(update_fields=...)affected 0 rows and raised aDatabaseError, crashing the syncThe deletion loop now re-checks each candidate under its lock with a cheap
exists()probe: candidates that no longer exist or are already inactive are skipped and counted asignored, so outcome counts still sum to the number of objects considered.Known limitation: an object re-affirmed by a concurrent sync after the deletion set is computed can still be deactivated, since the remote identity set is frozen before the loop. Closing that fully would require tracking a last-confirmed timestamp on synced objects. The re-check also assumes it sees committed state (READ COMMITTED/autocommit), so callers must not wrap syncs in an outer transaction that outlives the lock.