fix(drizzle): prevent duplicate latest=true version rows under concurrent/same-second writes#17413
Open
gonzoblasco wants to merge 2 commits into
Open
fix(drizzle): prevent duplicate latest=true version rows under concurrent/same-second writes#17413gonzoblasco wants to merge 2 commits into
gonzoblasco wants to merge 2 commits into
Conversation
…rent/same-second writes The createVersion function clears prior latest flags with a separate UPDATE guarded by updatedAt < comparison. This guard is too strict: two autosave writes within the same second produce identical updatedAt values, so the < operator skips the earlier row and both keep latest=true. Replace the updatedAt < guard with id < result.id. Version IDs are auto-incremented in SQLite/D1, so any prior version always has a smaller id. This is deterministic and not subject to timestamp collisions. Also adds an assertion to the existing parallel-writes test and a new same-second-writes test that reproduces the exact failure mode from payloadcms#17216. Fixes payloadcms#17216
sviat-barbutsa
left a comment
There was a problem hiding this comment.
Hi, I left an inline comment, please, check it beause it might be useful. Thank you!
…p race The cleanup query used id != result.id, which is vulnerable to an interleaving race: two concurrent writes can insert rows A and B, then B's cleanup runs first (keeps B), then A's cleanup runs second and with != it also clears B — leaving no version with latest=true. Changing to id < result.id ensures each cleanup only touches rows older than its own insert, so a newer concurrent insert is preserved. Also adds a regression test that simulates the exact interleaving pattern described in sviat-barbutsa's review. Refs payloadcms#17413
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.
The
createVersionfunction in the drizzle adapter clears priorlatestflags with a separate UPDATE guarded byupdatedAt <. This guard is too strict: two autosave writes within the same second produce identicalupdatedAtvalues, so the<operator skips the earlier row and both keeplatest = true. On D1/SQLite this routinely produces duplicate rows under normal autosave editing.The fix: replace
updatedAt <withid < result.id. Version IDs are auto-incremented in SQLite/D1, so any prior version always has a smaller id. This is deterministic and not subject to timestamp collisions.Testing:
expect(docs).toHaveLength(1)to the existing parallel-writes test (was only checkingdocs[0]is defined)updatedAttimestamp and asserts only onelatest = truerowFixes #17216