fix(queue): lease messages with FOR UPDATE SKIP LOCKED - #240
Merged
Merged
Conversation
… never share one Queue.read() selected the visible candidates and then bumped their vt in a second statement. On libSQL the single multiplexed connection and the in-process write lock serialised readers, so that was safe; on the Postgres pool (#239) two concurrent readers see the same candidates and both lease them. The integration test 'never hands the same message to two readers' failed against a real Postgres (36 leases of 10 messages). The select now locks the rows it is about to lease and skips rows another reader holds. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
ThreatCrush Security Scan11 finding(s) HIGH/CRITICAL: 5 | MEDIUM: 3 | LOW: 3
Snippets are redacted; ThreatCrush never prints matched credential material. |
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.
What changed
Queue.read()inpackages/shared/lib/queue.jsselected the visible candidates and then bumped theirvtin a second statement inside the transaction. On libSQL the single multiplexed connection plus the in-process write lock serialised readers, so that was safe. On the Postgres pool (#239) two concurrent readers of one queue see the same candidates and both lease them.Running the DB-bound suites against a real Postgres for the first time (the meshhook.com cutover, 2026-09-25) failed exactly one test:
Queue system integration > concurrent consumers > never hands the same message to two readers(36 leases of 10 messages). The select now ends withfor update skip locked, so a reader locks the rows it is about to lease and skips rows another reader holds.Production today runs one orchestrator with one Worker per queue, so the race did not bite there; this closes it before a second consumer ever exists.
Test
TEST_DATABASE_URL=postgres://... pnpm exec vitest run src/queue packages/shared/lib/auth.test.js: 110 passed, 0 failed (was 109 / 1).🤖 Generated with Claude Code