Skip to content

fix: Deluge lookups and the shared-torrent guard ignore the letter case of a hash - #461

Merged
kikootwo merged 2 commits into
mainfrom
fix/460-deluge-uppercase-hash
Oct 27, 2026
Merged

kikootwo merged 2 commits into
mainfrom
fix/460-deluge-uppercase-hash

Conversation

@kikootwo

@kikootwo kikootwo commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

Closes #460

What changed

  • normalizeTorrentId() (src/lib/integrations/torrent-ids.ts) lower-cases a valid 40-hex ID as well as converting base32. Every client lookup that goes through it (Deluge status, probe, pause, resume, delete and set-label; Transmission torrent-get; qBittorrent's lookups) now sends lower-case hex.
  • Shared-torrent guard: torrentHashForms() now lists the stored hash, then hex and base32, each in lower and upper case. Its only caller is otherActiveRequestsUsingTorrent() (src/lib/utils/shared-torrent.ts), the check the seeding cleanup and request delete run before removing a torrent, so a lower-case row now sees another active request's upper-case row for the same torrent.
  • Docs: documentation/phase3/download-clients.md (Torrent IDs: lookups, plus two Fixed Deluge lookups miss a torrent when its stored hash is upper case #460 lines).

Why

When a download row has no downloadClientId, the seeding cleanup and request delete fall back to torrentHash, which holds the indexer's infohash and is often upper case. Deluge's core.get_torrent_status looks torrents up by exact lower-case key, so the lookup would find nothing: the cleanup would mark the row removed and never clean the torrent, and request delete would leave it in Deluge. qBittorrent and Transmission already matched ignoring case.

No write path, today or since Deluge support was added, stores a Deluge row without its client ID, and that ID is always saved in lower case. So no installs are known to be affected; this part is hardening for the fallback path.

The guard had a case gap of its own, on every client. It matched rows with a case-sensitive Prisma in, and the list only held the stored form, lower-case hex and lower-case base32. torrentHash keeps the indexer's infohash in whatever case it came in, so two requests for the same torrent can store it in different cases: one grabbed from an indexer that sent no infohash (lower-case v1), the other from one that sent it in upper case. Cleaning or deleting the first request then missed the second, and could remove the torrent and its files while the second request still used it.

Evidence

  • New tests/utils/shared-torrent.test.ts runs the real guard with a findMany that applies its where to fixture rows using a case-sensitive in, as PostgreSQL does. A request whose row is lower-case hex sees an active request whose row is the same hash in upper case, and the same for upper- and lower-case base32. Deleted requests, unselected rows, other torrents and the request itself are still left out.
  • New Deluge test in tests/integrations/torrent-client-ids.test.ts runs the real DelugeService against the exact-key fake: an upper-case stored hash (the fallback a row with no client ID uses) is found by probeDownload and getDownload and removed by deleteDownload, with lower case sent on every call.
  • tests/integrations/qbittorrent-ids.test.ts: upper-case hex comes back lower case from normalizeTorrentId, and torrentHashForms() lists both cases. The guard's filter assertion in tests/processors/qbittorrent-hybrid-ownership.test.ts lists the new forms.
  • With main's torrent-ids.ts and shared-torrent.ts (0fd98ef) and this branch's four test files: 7 failed, 68 passed, all on an assertion (3 guard cases return no other request; the probe answers { found: false }; the hash forms lack the upper-case entries). On this branch: 75/75 pass.

Checks

  • Unit tests for the changed code (vitest related on torrent-ids.ts and shared-torrent.ts, plus the new test file): PASS, 107 files, 1691/1691
  • Type check and lint: PASS (tsc --noEmit clean; ESLint reports no errors or warnings in the 6 touched files)
  • No image build or E2E run: the change is in src/lib only
  • Full unit suite: PR Tests (CI)

Risk and rollback

  • Newly reachable: Deluge rows with no stored client ID whose torrentHash is upper-case hex and that aren't already marked removed. The seeding cleanup and request delete can now find those torrents (non-hybrid). No write path is known to create such rows, so this should reach few or none.
  • Finding a torrent doesn't make it removable. Ownership is decided as before: a row marked removed or recorded as taken over gets no client call; every other row goes through resolveOwnership() before any remove. Rows without a client ID predate ownership tracking, so the legacy rule applies: the torrent counts as RMAB's only if the client added it between 15 minutes before and 5 minutes after RMAB's grab. Added 24 hours or more before means not RMAB's (recorded, kept); anything else is kept and checked again.
  • Shared-torrent guard: the list of forms only grows, so the guard can only find more requests sharing a torrent. The effect is that more torrents are kept; none that was kept before is removed now.
  • Rows the old code already marked removed stay removed; they are not cleaned.
  • qBittorrent and Transmission: no change in what their lookups match. Both already compared hashes ignoring case, and qBittorrent's ID cache already lower-cased its keys.
  • Rollback: revert this PR. Rows aren't rewritten, so nothing needs undoing.

Release note

🤖 Generated with Claude Code

kikootwo and others added 2 commits October 27, 2026 00:34
normalizeTorrentId() now lower-cases a valid 40-hex ID as well as
converting base32. A row with no client ID falls back to the indexer's
infohash, which can be upper case; Deluge looks torrents up by exact
lower-case key, so the seeding cleanup marked such a row removed and
request delete left the torrent. torrentHashForms() keeps the stored form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
torrentHashForms() lists the stored hash as hex and base32 in both lower and
upper case, so the guard sees another active request's row for the same
torrent whatever case either row stores. It only adds matches, so cleanup and
request delete only keep more torrents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo marked this pull request as ready for review October 27, 2026 04:57
@kikootwo kikootwo changed the title fix: Deluge lookups ignore the letter case of the stored hash fix: Deluge lookups and the shared-torrent guard ignore the letter case of a hash Oct 27, 2026
@kikootwo
kikootwo merged commit b0dda96 into main Oct 27, 2026
1 check passed
@kikootwo
kikootwo deleted the fix/460-deluge-uppercase-hash branch October 27, 2026 05:01
@kikootwo kikootwo mentioned this pull request Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deluge lookups miss a torrent when its stored hash is upper case

1 participant