Repository navigation
Conversation
When a key has no free slot and already has max_blocked jobs waiting, a new job on it is discarded. Until now it took the semaphore's row lock first, twice, and held it while counting the blocked jobs, deleting the job and committing. Many enqueues on one busy key then waited in line for that lock only to be discarded. dispatch now checks for that case first, in one unlocked statement that reads the semaphore and the blocked jobs together. The job is discarded in exactly the cases it was before. Skipping the lock is safe because a blocked job seen by the check hasn't started, so it runs after this enqueue. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF
The check put the blocked jobs' subquery, with the key as a literal, into a SQL string that was then scanned for ? placeholders, so a concurrency key with a ? in it failed to enqueue. It now uses an EXISTS for the max_blocked-th blocked job, built with Arel, which keeps the key a bound value. With max_blocked: 0, only the slot is checked. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF
…commits A job discarded for being over max_blocked counts on the blocked job to run in its place, after the transaction enqueuing it commits. The semaphore's lock used to guarantee that, since it was held until then. The check before locking didn't: the blocked job could be released and run while that transaction was still open, and miss its changes. The check now takes a shared lock on the last blocked job. Discarded jobs hold it together, so they still don't wait for each other, and a blocked job is skipped for release while it's locked. Since a job that finishes meanwhile can't release it, it's released when the transaction ends, if a slot is free by then. This needs PostgreSQL and Active Record 7.2. Elsewhere jobs over max_blocked are discarded under the semaphore's lock, as before. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF
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.
A job on a key with no free slot and
max_blockedjobs already waiting is discarded. Until now, it took the semaphore's row lock to find that out, and held it until the transaction enqueuing it committed. When many enqueues arrive for one busy key, they wait in line for that lock, one transaction at a time, only to be discarded. Inpg_stat_activitythis shows as many sessions waiting onLock:tuplefor the semaphore lookup.What the lock was for
A discarded job counts on the blocked job to run in its place, after the transaction enqueuing it commits. The semaphore's lock guaranteed that: while it was held, the running job couldn't give its slot back and the blocked job couldn't be released. It also made discarded enqueues wait for each other, which nothing needs.
What changes
dispatchfirst callsblocked_queue_full?. It's one statement that checks that the key's semaphore has no free slot and takes a shared lock (FOR SHARE SKIP LOCKED) on the last of the key'smax_blockedblocked jobs. If it gets the lock, the job is discarded without touching the semaphore.FOR UPDATE SKIP LOCKED), so it stays blocked until those transactions end.block's locked check still keeps the cap exact while the key fills up.This applies on PostgreSQL with Active Record 7.2 or later. On MySQL, SQLite and Active Record 7.1, jobs over
max_blockedare discarded under the semaphore's lock, as before. Keys withoutmax_blocked, and keys withon_conflict: :discard, don't change.What to know
duration.Tests
In
job_test.rb:Semaphore.lock;max_blockedworks with a key containing?and:name.Full suites, 0 failures: Rails 8.1 on PostgreSQL, SQLite and MySQL (433 runs each); Rails 7.2 on PostgreSQL (the job tests); Rails 7.1 on PostgreSQL (431 runs).
Measured
PostgreSQL 17 on a laptop. 16 processes each run transactions that write a row, enqueue a job on one
max_blocked: 1key, and stay open for a while. Two workers run the jobs, 20 ms each.Lock:tupleLock:tupleIn every run, each job saw every row committed before it started or a later job did, at most one job ran at a time, and nothing was left blocked.
An earlier version of this PR checked without any lock. It was faster (659–722 transactions per second at 5 ms), but a blocked job could run before a discarding transaction committed. The random-sequence comparison against main in the earlier description was run on that version and hasn't been repeated.
🤖 Generated with Claude Code
https://claude.ai/code/session_01JfqjnQwPt8FZaFywjjXfPF