Skip to content

fix(storage): concurrent duplicate reservations can both succeed #281

Closed

Description

@LouisHaftmann

Storage.createUpload() checks for an existing upload with a SELECT and then INSERTs, with no transaction and no unique constraint. The only index on uploads is the non-unique idx_uploads_key_version on (key, version).

Two CreateCacheEntry calls for the same key, version, scope and repo that arrive at the same moment can both pass the check, so both get 200 and a signed upload URL. #277 returns 409 already_exists for a duplicate, but only when the requests don't overlap. Parallel BuildKit jobs writing the same cache key (#276) are the clients most likely to overlap.

Proposed fix

  • Migration: add a unique index on uploads (key, version, scope, repoId). Deduplicate existing rows first, keeping the newest upload per tuple, or the migration fails on databases that already have duplicates.
  • createUpload(): insert directly and map the unique-violation error from SQLite, Postgres and MySQL to AlreadyExistsError. The pre-check SELECT then becomes unnecessary.
  • Test: fire several CreateCacheEntry calls for the same key concurrently and assert exactly one 200, with every other call getting 409. Run it across the test:matrix DB drivers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions