Skip to content

Avoid an unintended FOR UPDATE query execution inside lock_candidates#720

Merged
rosa merged 1 commit intorails:mainfrom
genya0407:feature/avoid-unintended-locking-query
Mar 2, 2026
Merged

Avoid an unintended FOR UPDATE query execution inside lock_candidates#720
rosa merged 1 commit intorails:mainfrom
genya0407:feature/avoid-unintended-locking-query

Conversation

@genya0407
Copy link
Contributor

With 408b84c, select_candidates began returning an ActiveRecord::Relation instead of an Array.

Consequently, calling executions.none? inside lock_candidates now triggers an SELECT 1 ... FOR UPDATE SKIP LOCKED query:

SELECT 1 AS one FROM `solid_queue_ready_executions` LIMIT 1 FOR UPDATE SKIP LOCKED;

While we are still investigating the exact database mechanics, this additional locking query has become a performance bottleneck in our environment. (We are currently conducting performance tests as part of migrating from another queue backend to Solid Queue).

Since this behavior change appears to be an unintended side effect, this PR adds .to_a to ensure the relation is loaded as an array, avoiding the redundant query and improving performance.

Without `#to_a`, `select_candidates` returns an `ActiveRecord::Relation`.
Consequently, `executeions.none?` inside the `lock_candidates` method triggers an unintended query:

```
SELECT 1 AS one FROM `solid_queue_ready_executions` LIMIT 1 FOR UPDATE SKIP LOCKED;
```
@genya0407
Copy link
Contributor Author

The SQLite test failed. I’ll look into it and try to fix it.
https://github.com/rails/solid_queue/actions/runs/22561584550/job/65349215875?pr=720

@genya0407
Copy link
Contributor Author

@rosa
Copy link
Member

rosa commented Mar 2, 2026

Yes, it's a flaky test, I need to fix it.

Thanks for this, good catch!

@rosa rosa merged commit 50defe2 into rails:main Mar 2, 2026
63 of 64 checks passed
@genya0407 genya0407 deleted the feature/avoid-unintended-locking-query branch March 2, 2026 12:14
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.

2 participants