Repository navigation
fix(collections): write no synced items under a collection deleted meanwhile - #4066
Conversation
…anwhile A list sync's ReplaceCollectionItems locked the collection row with a statement that succeeds when no row matches. When it waited on a delete of the collection, or of its profile, it went on after the delete committed and inserted the whole list under the deleted ID. The items have no foreign key to the collection, so nothing removed them. The replace now locks the row with a read that answers ErrCollectionNotFound when the row is gone, also after waiting out the delete, and writes nothing. Closes Silo-Server#2844 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
🤖 Claude Code (claude-opus-5-5) Review of Defect confirmed on main.
What I checked
Validation: no passed case on the v1.0 board covers synced personal lists. Collections — Server (#193) S2 covers manual add, remove, reorder, and delete, which this PR doesn't change. User manual: unaffected. Other clients: N/A, as the PR says; no response shape changes. Fit 9/10: a narrow data-integrity fix at the store boundary that owns the lock, and it matches what the library-collection path already does. Readiness 9/10: the new test fails on main's locking and passes here, and CI is green. |
Problem
Closes #2844
Related issue: #2615
Validation tasks: none
Other clients: Apple N/A (the orphan rows never reach the API); Android N/A (same); Roku N/A (same)
A personal synced list (MDBList or TMDB) could leave item rows behind for a collection that no longer exists. If the collection, or the profile that owns it, was deleted while a sync of it was in flight, the sync's
ReplaceCollectionItemswaited for the delete and then inserted the whole list intouser_personal_collection_itemsunder the deleted collection's ID. Nothing removed those rows, and the delete reported success.lockCollectionRowrunsSELECT ... FOR UPDATEthroughExec, which succeeds when no row matches, anduser_personal_collection_itemshas no foreign key to the collection.This PR has no dependencies and can merge in any order. #2615, which the issue said to wait for, is merged.
Approach
lockExistingCollectionRowreads the locked row and answersuserstore.ErrCollectionNotFoundwhen there is none, also when the lock waited on a delete that then committed.ReplaceCollectionItemsuses it and writes nothing for a collection that is gone, aslockLibraryCollectionParentdoes for library collections.lockCollectionRow, so deleting a missing collection behaves as before.Validation
gofmt,go vet, and the changed-package lint are clean.go testpasses forinternal/userstore/pgstoreandinternal/usercollectionsagainst a migrated Postgres 18.6 test database, and the replace tests pass with-race.TestPostgresReplaceCollectionItemsWaitsOutADelete, added toscripts/ci/db-contracts.txt: a transaction deletes the collection the waydeleteCollectiondoes, a replace from another session waits on its lock (checked inpg_stat_activity), and the delete commits. WithlockCollectionRowput back, the replace returnsnil.make test-db-contracts.Evidence
No visible layout change: the orphan rows are invisible in the API. The evidence is the new DB test, on WSL2 (Ubuntu,
go1.26.4 linux/amd64) with Postgres 18.6,mainat0387303d9and this PR at04c66e51e. "Before" is the same test with the replace locking throughlockCollectionRow, as onmain.main's lock)nilErrCollectionNotFoundRisks
Checklist
AI Disclosure
🤖 Generated with Claude Code
Note
Fix
ReplaceCollectionItemsto skip writes when collection is deleted concurrentlylockExistingCollectionRow, which selects and locks the collection row for the current user. It mapspgx.ErrNoRowstouserstore.ErrCollectionNotFoundand wraps other query errors with the collection ID (collection_mutations.go).ReplaceCollectionItemsnow uses this helper, so a replace that waits on a deletion transaction returnsuserstore.ErrCollectionNotFoundbefore writing item rows. This matters because collection items have no foreign key to the collection, so rows could otherwise be orphaned under a deleted ID (collections.go).Macroscope summarized 04c66e5.