Repository navigation
Description
Summary
consumeQueueMiss (emulator/sqs_control.go:158) serializes seed consumption with
p.queueMu.lock(queueName) — a stripe chosen by hashing the queue name. It then reads
and decrements up to two state keys under that lock: consistency:<name>, and
consistency:* if the name-scoped seed is absent or exhausted.
The wildcard key is shared by every queue, but the lock is not. Two concurrent lookups on
different queue names take different stripes, so both can read the same
consistency:* seed with getUrlMisses: 1, both decrement to 0, and both Put. One
seeded miss is consumed twice — or, symmetrically, a seed with getUrlMisses: 2 can be
spent by three concurrent calls.
This is exactly the class of hazard s3KeyMutex and sqsQueueMutex exist to prevent, as
their own doc comments say: StateManager offers no compare-and-swap, and
MemoryStateManager is last-write-wins, so a read-modify-write must be serialized against
every other writer of the same key. The name-scoped path is airtight; the wildcard path
falls through the striping.
Found while implementing the S3 conditional-conflict seed for #540, which follows this
same pattern and therefore had to decide how to lock. That plugin takes a single dedicated
mutex for seed consumption rather than relying on the per-key stripe, which closes the
window; this issue is the corresponding fix on the SQS side.
Reproducing
Seed {"getUrlMisses": 1} with no queueName (the wildcard), create N queues, and call
GetQueueUrl on all of them concurrently. The property the seed promises is "exactly one
of these calls reports QueueDoesNotExist". Under -race with enough goroutines, two do.
The window is narrow — a Get, an unmarshal, a decrement, a marshal and a Put — and a test
asserting the count will flake rather than fail reliably, which is what makes this worth
fixing rather than documenting.
What a fix looks like
Consume under a single mutex rather than a per-queue stripe. Seed consumption is a
harness-frequency operation, not a hot path, so there is nothing to gain from striping it:
// on SQSPlugin, alongside queueMu
seedMu sync.Mutextaken for the whole read→decrement→Put in consumeQueueMiss. The per-queue stripe is
still the right tool for the operations it was introduced for; it is only seed consumption
that touches a key shared across queues.
If both locks are held, the order must be stripe → seed-mutex everywhere, and the comment
should say so, since the reverse order would deadlock.
Acceptance criteria
- Concurrent consumption of a wildcard seed from different queue names consumes each
seeded miss exactly once. - A test drives N concurrent lookups against a wildcard seed of M misses and asserts
exactly M report the seeded deviation — the property that flakes today. It must run
under-raceand must fail before the fix. - The name-scoped path keeps its current behaviour, including the fall-through to a
funded wildcard when a name-scoped seed is exhausted. -
sqsQueueMutex's doc comment is updated: it currently describes the guarantee as
airtight for single-process topology, which is true of the name-scoped path and was
not true of the wildcard one. -
CHANGELOG.mdunder### Fixed.
References
emulator/sqs_control.go—consumeQueueMiss,sqsQueueMutex,sqsCtrlKeyemulator/s3_conditional.go—s3KeyMutex, the type this one mirrors, and the
reasoning aboutStateManagerhaving no compare-and-swap- S3: conditional writes never return 409 Conflict, so the retry path a caller must take is untestable #540 — where this was found; its seed takes a single mutex for the same reason
Activity
scttfrdmn commentedon Aug 6, 2026
Confirmed, and the suggested shape is slightly more than is needed.
queueMu.lockhas exactly one call site — insideconsumeQueueMissitself (emulator/sqs_control.go:159). This issue proposes adding aseedMualongside the existing stripe with a documented lock-order rule, but since nothing else uses the stripe, the striped type can be replaced rather than supplemented: a singleseedMu sync.MutexonSQSPlugin, held across the whole read → decrement →Put. That removes the lock-order rule along with the hazard, which is a smaller surface than keeping both locks and a rule about them. Seed consumption is a harness-frequency operation, so there is nothing to gain from striping it.That also makes the shape identical to
S3Plugin.seedMu, added in v0.93.0 for #540's conditional-write seed for exactly this reason, and the two comments will cross-reference each other.Acceptance criterion 4 still applies: the
sqsQueueMutexdoc comment currently calls the guarantee "airtight for substrate's single-process topology", which was true of the name-scoped path and not the wildcard one. The replacement keeps the two paragraphs that are still true — thatStateManagerhas no compare-and-swap, and that the guarantee is process-local — and corrects that claim.Already in milestone v0.94.0; shipping alongside #560 and #581.
fix(sqs): the wildcard consistency seed cannot be double-consumed (#582…
feat(ec2): a snapshot's progression is seedable, so a poll loop has s…
refactor: one shared seeded-progression helper, with EC2 snapshots an…