Skip to content

[go] Fix JobDelete returning a stale row when the job starts running - #1607

Open
Poudel0 wants to merge 3 commits into
riverqueue:masterfrom
Poudel0:fix-jobdelete-stale-row
Open

Poudel0 wants to merge 3 commits into
riverqueue:masterfrom
Poudel0:fix-jobdelete-stale-row

Conversation

@Poudel0

@Poudel0 Poudel0 commented Nov 28, 2026 •

Copy link
Copy Markdown

The bug

If a worker picks up a job at the same moment JobDelete runs, JobDelete can say it deleted the job when it didn't. The job stays in the table and runs.

How it happens:

  1. The worker's fetch locks the job row and sets it to running.
  2. JobDelete waits on that lock.
  3. The fetch commits. The delete now sees running and skips the row, which is right.
  4. The fallback SELECT then reads the row from the snapshot taken when the statement started, so it still sees available.
  5. The driver only returns ErrJobRunning for a running row, so the caller gets the old row back and no error.

This is the same stale fallback read that #1409 fixed for JobCancel and #1410 fixed for JobRetry.

The fix

Make the fallback read lock the row (FOR UPDATE), the same way JobCancel and JobRetry do. A locking read waits for the other commit and then reads the newest row, so it sees running and the caller gets ErrJobRunning.

Postgres doesn't allow FOR UPDATE next to UNION, 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

  • Two deletes racing on the same job: the loser used to get the old row back with no error, as if it had deleted the job too. Now it gets 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.
  • SQLite is unchanged. Its driver deletes in two steps inside a transaction, and the new test skips it, like the JobCancel and JobRetry race tests do.
  • JobDeleteMany has 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_activity shows 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, and make test passes locally.

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
Poudel0 force-pushed the fix-jobdelete-stale-row branch from 4868216 to 9c6e5b7 Compare November 28, 2026 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant