Skip to content

SQS: a wildcard consistency seed can be double-consumed, because the lock is striped by queue name #582

Closed

Description

@scttfrdmn

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.Mutex

taken 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 -race and 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.md under ### Fixed.

References

Activity

  1. added
    type: bug
    on Aug 6, 2026
  2. added this to the v0.94.0 milestone on Aug 6, 2026
  3. scttfrdmn commented on Aug 6, 2026

    @scttfrdmn
    OwnerAuthor

    Confirmed, and the suggested shape is slightly more than is needed.

    queueMu.lock has exactly one call site — inside consumeQueueMiss itself (emulator/sqs_control.go:159). This issue proposes adding a seedMu alongside 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 single seedMu sync.Mutex on SQSPlugin, 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 sqsQueueMutex doc 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 — that StateManager has 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: sqstype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions