Repository navigation
Conversation
When a producer fetch holds a job's row lock while moving it to running, a concurrent JobDelete waits on that lock, skips the delete once the fetch commits, then reads the row back through its UNION fallback. That fallback is a plain read under the statement's snapshot, so it returns the pre-fetch available row, and the caller is told the job was deleted while it is still there and running. It should get ErrJobRunning. The test holds the fetch's lock until pg_stat_activity shows the delete waiting on it, then commits, so it fails deterministically on master. It is skipped on SQLite, whose driver deletes in two steps inside a transaction.
JobDelete's UNION fallback read the job with a plain SELECT, which sees the statement's starting snapshot. If a producer fetch moved the job to running while the delete waited on its row lock, the delete skipped the row but the fallback still returned the old available row, so the caller was told the job was deleted while it kept running. Make the fallback a locking read (FOR UPDATE inside a subquery, since Postgres doesn't allow it directly with UNION), the same shape JobCancel and JobRetry already use. It now waits past the fetch's commit, sees the job as running, and the driver returns ErrJobRunning. As a side effect, the loser of two concurrent deletes now gets ErrNotFound instead of the pre-delete row.
Poudel0
force-pushed
the
fix-jobdelete-stale-row
branch
from
November 28, 2026 06:17
4868216 to
9c6e5b7
Compare
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 bug
If a worker picks up a job at the same moment
JobDeleteruns,JobDeletecan say it deleted the job when it didn't. The job stays in the table and runs.How it happens:
running.JobDeletewaits on that lock.runningand skips the row, which is right.SELECTthen reads the row from the snapshot taken when the statement started, so it still seesavailable.ErrJobRunningfor arunningrow, so the caller gets the old row back and no error.This is the same stale fallback read that #1409 fixed for
JobCanceland #1410 fixed forJobRetry.The fix
Make the fallback read lock the row (
FOR UPDATE), the same wayJobCancelandJobRetrydo. A locking read waits for the other commit and then reads the newest row, so it seesrunningand the caller getsErrJobRunning.Postgres doesn't allow
FOR UPDATEnext toUNION, so the lock goes inside a subquery. The statement already locks this row at the top (job_to_delete), so this adds no new lock.Side effects
ErrNotFound, the same as deleting a job that's already gone. I checked this with the raw SQL. There's no test for it in this PR, but I can add one if you'd like.JobCancelandJobRetryrace tests do.JobDeleteManyhas no fallback read, so it doesn't have this bug.Test
The first commit adds a test that fails on master. It holds the fetch's row lock until
pg_stat_activityshows the delete waiting on it, then commits, so it doesn't depend on timing. The second commit is the fix.The test passes on all four Postgres drivers, including 3 runs with
-race, andmake testpasses locally.