Repository navigation
Description
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
tracker-core::ScrapeHandler::handle_scrapeiterates the requested info hashes in order but stores each result inScrapeData.files, aHashMap<InfoHash, SwarmMetadata>(packages/primitives/src/scrape.rs).udp-server::handlers::scrape::build_responseiteratesscrape_data.filesto buildScrapeResponse.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_statsvector 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 toinfo_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 allpasses; 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
docs(issues): [torrust#2314] link spec PR
docs(issues): [torrust#2314] quote evidence timestamps
docs(issues): [torrust#2314] add spec for preserving UDP scrape respo…
docs(issues): [torrust#2314] link spec PR
docs(issues): [torrust#2314] quote evidence timestamps
Merge #2315: docs(issues): [#2314] add spec for preserving UDP scrape…
docs(skills): make the branch-id pre-push check clear its reference case
Merge torrust#2320: fix(udp-server): [torrust#2314] preserve scrape r…
chore(issues): archive closed issue torrust#2314 spec to docs/issues/…
chore(issues): archive closed issue torrust#2314 spec to docs/issues/…
chore(issues): archive closed issue torrust#2314 spec to docs/issues/…
Merge #2329: chore(issues): archive closed issue #2314 spec to docs/i…
docs(issues): [torrust#2458] plan performance measurement and the ben…
docs(issues): [torrust#2458] plan performance measurement and the ben…
docs(issues): [torrust#2458] plan performance measurement and the ben…
docs(issues): [torrust#2458] plan performance measurement and the ben…