Skip to content

Escape LIKE metacharacters in queue prefixes - #811

Merged
rosa merged 4 commits into
rails:mainfrom
afurm:escape-queue-prefix-like-wildcards
Nov 3, 2026
Merged

rosa merged 4 commits into
rails:mainfrom
afurm:escape-queue-prefix-like-wildcards

Conversation

@afurm

@afurm afurm commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

What

QueueSelector turns a worker prefix such as mail_* into a SQL LIKE pattern. _ and % in that prefix were left unescaped, so LIKE 'mail_%' also matched mailbox or mail-ops. * is still the only wildcard.

When that false match was the only row, in_raw_order returned it immediately because queues.one? skipped the literal start_with? filter. The worker then claimed jobs from a queue it was not configured to run.

Why

_ and % are escaped (via sanitize_sql_like) before * is replaced with %, and the LIKE clause sets ESCAPE '\' so the escape works on SQLite as well as MySQL and PostgreSQL. Prefix matches always go through the literal filter, including when only one name comes back, so a false LIKE hit is not claimed.

A regression test covers a prefix that is the only LIKE hit but not a literal match, and a real mail_* / 100%_* queue that should still be claimed.

@afurm

afurm commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

The CI matrix on the current head was cancelled mid-run - the jobs show cancelled, not failed. The focused tests pass locally. Could a maintainer re-run the workflow when convenient? Happy to push a rebase if that helps.

afurm and others added 4 commits November 2, 2026 20:01
A worker prefix such as mail_* became LIKE 'mail_%', so _ and % matched
any character. When that false match was the only row, the selector
returned it without the literal prefix check and the worker claimed a
queue it was not configured to run.
ESCAPE '\' is a valid string on SQLite and PostgreSQL, but MySQL treats
the backslash as an escape and rejects the clause. MySQL needs the same
one-character escape written as '\\'.
A backslash needs doubling inside MySQL string literals but not in SQLite
or PostgreSQL, which is why the LIKE clause checked the adapter. Escaping
with ! works the same everywhere, so the check goes away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A worker listing queue_* and queue_b*, or queue_b* and queue_b1, polled
queue_b1 twice on every pass and counted its jobs twice. Prefix matches
were never removed from the remaining names, because `queues -= matches`
only reassigned the block's local variable, and a queue matched both by
name and by a prefix came back from both lookups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rosa
rosa force-pushed the escape-queue-prefix-like-wildcards branch from 101dbf7 to cf15cc6 Compare November 3, 2026 07:41
@rosa

rosa commented Nov 3, 2026

Copy link
Copy Markdown
Member

Thanks a lot, @afurm! I rebased this onto main, dropped the two empty commits to retrigger CI (main has the json fix now, so the Ruby 3.2 jobs should pass), and added two commits on top:

  • Escape the prefix with ! instead of a backslash, so the LIKE clause works the same on every database and doesn't need to check for MySQL.
  • Select each queue once when several entries in the list match it. While looking at in_raw_order, we noticed that queues -= matches in delete_in_order only reassigned a local variable, so with queue_* and queue_b*, or queue_b* and queue_b1, a worker polled queue_b1 twice. That was already there before your change, but it's right next to it.

@rosa
rosa merged commit ec42a7e into rails:main Nov 3, 2026
115 of 116 checks passed
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