Skip to content

Concurrent identical whitelist add or remove fails although the operation is idempotent #3052

Done

Description

@josecelano-bot

Problem

DatabaseWhitelist::add and remove (packages/tracker-core/src/whitelist/repository/persisted.rs) are written to be idempotent, but they read is_info_hash_whitelisted and then write, without a transaction. Under concurrency the check-then-act races:

  • Two concurrent add calls of the same info-hash both read "not whitelisted", both insert, and the loser fails with UNIQUE constraint failed: whitelist.info_hash.
  • Two concurrent remove calls both read "whitelisted"; the loser deletes zero rows, which the store reports as DeleteFailed.

WhitelistManager::add_torrent_to_whitelist (the REST whitelist endpoint) calls add, so one of two simultaneous identical requests gets an error although the hash is whitelisted. Stored data and the in-memory whitelist stay correct; the harm is a false error.

Reproduction

SQLite, temporary database, #[tokio::test(flavor = "multi_thread", worker_threads = 4)], 200 rounds of two concurrent identical add then two concurrent identical remove: both fail in 200 of 200 rounds (first error InvalidQuery { Database(SqliteError { code: 2067, "UNIQUE constraint failed: whitelist.info_hash" }) }). MySQL and PostgreSQL use the same code shape; to be reproduced.

Chosen fix

Make the store writes idempotent (ON CONFLICT DO NOTHING / ON DUPLICATE KEY UPDATE info_hash = info_hash; a delete that accepts zero rows), drop the read-before-write, and replace the three driver tests that document the failures with no-op tests.

Found as finding F4 of #2902 (EPIC #1347). Spec: docs/issues/open/<n>-whitelist-add-remove-race/ISSUE.md, in the spec PR.

Activity

  1. added 13 commits that reference this issue on Nov 25, 2026
  2. added 2 commits that reference this issue on Nov 25, 2026
  3. 6 remaining items

  4. added 9 commits that reference this issue on Nov 25, 2026
  5. added a commit that references this issue on Nov 26, 2026
  6. added 2 commits that reference this issue on Nov 26, 2026
  7. added 2 commits that reference this issue on Nov 26, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions