Repository navigation
Conversation
On Postgres, the dedup queue could run a completed job a second time. sendAndGetIDTx checks job_done and then inserts into jobs in two statements, and under READ COMMITTED each statement takes its own snapshot. When the worker's Delete of the same payload commits between them, the check sees no job_done row and the insert finds no live job (or waits for the delete and then proceeds), so the payload is queued and handled again. SQLite serialises writers, so it never hit this. This made TestJobQueue_DedupPreventsDuplicates/dedup/postgres flaky: the handler counter reaches 1 before the worker calls Delete, so the test's third Enqueue races with that Delete. Check job_done again after a successful insert and remove the new row when the payload completed in the meantime. The insert can only succeed once the live job's delete has committed, and that transaction also wrote job_done, so the second check sees it. Signed-off-by: Miroslav Bajtoš <oss@bajtos.net> Assisted-by: Claude:claude-opus-5-5
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This branch has not been deployed
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.
Written by Claude.
Fix a race in the dedup job queue that let Postgres run a completed job a second time. The race also made
TestJobQueue_DedupPreventsDuplicates/dedup/postgresflaky (for example this run).Root cause
sendAndGetIDTxchecksjob_done, then inserts intojobswithON CONFLICT DO NOTHING. These are two statements, and under Postgres READ COMMITTED each takes its own snapshot. The worker'sDeleteremoves the live job and writesjob_donein one transaction. When that transaction commits between the two statements, the check sees nojob_donerow and the insert finds no live job, so the payload is queued and handled again. If the insert starts while the delete is still open, it waits for the delete to commit and then inserts anyway.The test hits this window because the worker increments the handler counter before it calls
Delete.require.Eventually(processed == 1)returns while the delete is still pending, and the thirdEnqueueraces with it. SQLite serialises writers, so the SQLite variant never sees the interleaving.The
JSON serializer cannot be used with dedupe queuelog line is unrelated.TestMessageis a struct,encoding/jsonmarshals struct fields in a fixed order, and the SQLite variant uses the same serializer and passes.Fix
After a successful insert, check
job_doneagain in the same transaction and delete the new row if the payload completed in the meantime. The insert can only succeed once the live job's delete has committed, and that same commit wrotejob_done, so the second check sees it. The new row is still uncommitted, so no worker can have received it. The fix relies on READ COMMITTED: a caller ofSendTxrunning REPEATABLE READ or SERIALIZABLE keeps its earlier snapshot, and the code comment says so.TestQueue_DedupSkipsPayloadCompletedDuringSendreproduces the interleaving on Postgres. It holds a delete transaction open, startsSendfor the same payload, commits the delete, and asserts that nothing is left to receive.Testing
go test -count=20 ./lib/jobqueue/...passes locally. Docker was unavailable there, so the Postgres variants skipped and CI covers them.