Skip to content

Preserve request order in UDP scrape responses (BEP 15) #2314

Closed

Description

@josecelano

Summary

The UDP tracker can return scrape statistics at the wrong position. BEP 15 scrape responses are positional: the response has no info_hash field, so the nth 12-byte statistics block must describe the nth requested info hash. The tracker builds that positional list by iterating a HashMap, so the order is arbitrary and varies between otherwise identical requests. A client following BEP 15 attributes torrent B's statistics to torrent A (and vice versa) whenever the order differs from the request.

A second symptom from the same cause: duplicated info hashes in a request are collapsed, so a 3-hash request [A, A, B] returns only 2 entries, which also breaks positional decoding.

Reproduction

Reproduced on unmodified develop (60a4a160) with the default development configuration. Torrent A was seeded with 1 seeder; torrent B with 2 seeders and 1 leecher. The same UDP scrape request was repeated ten times per order using tracker_client udp scrape:

Request order Correct responses Swapped responses
[A, B] 7 / 10 3 / 10
[B, A] 3 / 10 7 / 10

Example swapped response for request [A, B] (expected [1 seeder, 2 seeders]):

{"Scrape":{"transaction_id":-888840697,"torrent_stats":[{"seeders":2,"completed":0,"leechers":1},{"seeders":1,"completed":0,"leechers":0}]}}

Duplicate request [A, A, B] returned 2 entries instead of 3.

Full commands, output, and environment are recorded in the issue-local manual-verification-evidence.md.

Cause

  1. tracker-core::ScrapeHandler::handle_scrape iterates the requested info hashes in order but stores each result in ScrapeData.files, a HashMap<InfoHash, SwarmMetadata> (packages/primitives/src/scrape.rs).
  2. udp-server::handlers::scrape::build_response iterates scrape_data.files to build ScrapeResponse.torrent_stats (packages/udp-server/src/handlers/scrape.rs).

HashMap iteration order is not insertion order, and the default hasher is seeded per map instance.

The HTTP tracker is not affected: its scrape response is a bencoded dictionary keyed by info hash.

The released main branch (v3.0.0) contains the same loop in src/servers/udp/handlers.rs. No backport is planned; the fix ships with the next major release.

Decided fix

Option 1 – the UDP response builder iterates the request. build_response iterates request.info_hashes and looks up each hash in scrape_data.files, producing exactly one entry per requested hash (duplicates included). ScrapeData stays keyed and unordered (it correctly identifies torrents by info hash; position is a UDP wire-format concern). An alternative (an insertion-ordered ScrapeData) was analysed and rejected: it touches five crates and does not fix the duplicate collapse on its own. Full analysis in the spec.

Regression tests

Two package-level collaboration tests in udp-server through the production handle_scrape:

  • Entry-count test: request [A, A], assert two entries. Deterministically red against the current code.
  • Order test: request ≥ 8 distinct hashes in a non-sorted known order with distinct statistics, assert the complete torrent_stats vector in request order. Chance pass against the current code is 1/N!.

Performance

The fix adds N keyed lookups per request (N ≤ ~74), estimated at about 2 µs worst case. Scrape throughput is measured before and after on the same machine with a new Criterion scrape_once microbenchmark and a scrape-heavy aquatic_udp_load_test run; results are recorded in the issue-local scrape-benchmark-evidence.md. Acceptance requires the end-to-end median to stay within 5% of the baseline.

Acceptance criteria

  • AC1: Local UDP tracker artifact demonstrates the pre-fix mismatch (done, recorded).
  • AC2: Both regression tests fail before the fix.
  • AC3: torrent_stats[i] corresponds to info_hashes[i] for every i, duplicates included, without sorting.
  • AC4: Both regression tests pass after the fix.
  • AC5: The original scenario rerun after the fix returns request order in all 20 responses and 3 entries for [A, A, B].
  • AC6: HTTP scrape behaviour unchanged.
  • AC7: Scrape performance measured before/after on the same machine; end-to-end median within 5% of baseline.
  • linter all passes; relevant tests pass; manual verification recorded.

Specification

Folder-style spec with reproduction evidence, bug-fix plan, and benchmark evidence skeleton: docs/issues/open/<number>-preserve-udp-scrape-response-order/ (spec-only PR to follow).

Discovered while reviewing handlers/scrape.rs tests under #2283 (EPIC #1347); this is a product correctness bug, not coverage work, so it is filed independently.

Reference: BEP 15 – UDP Tracker Protocol.

Activity

  1. added
    Bug
    High Priority
    on Sep 23, 2026
  2. self-assigned this
    on Sep 23, 2026
  3. added 7 commits that reference this issue on Sep 23, 2026
  4. added 5 commits that reference this issue on Sep 24, 2026
  5. added 4 commits that reference this issue on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugHigh Priority

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions