Skip to content

fix(torrent): resume into the chosen folder, find magnet metadata faster and stop dialing ourselves - #538

Merged
linroid merged 2 commits into
mainfrom
fix/torrent-resume-and-peer-discovery
Oct 13, 2026
Merged

linroid merged 2 commits into
mainfrom
fix/torrent-resume-and-peer-discovery

Conversation

@linroid

@linroid linroid commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

Found while watching a desktop session's log: a torrent that failed forever after a quick quit, a "Download all files now" that paused, and magnet lookups that barely tried their peers.

What changes for users

  • No more "Ownership journal belongs to another task". If the app quit within seconds of a torrent starting (before its first progress save), the next launch started it fresh under a new folder name (Sintel (3) beside Sintel (2)), and the torrent's ownership journal, bound to the first name, failed every retry. It now resumes into the folder it chose. Tasks already stuck this way stay stuck; remove and add them again.
  • "Download all files now" (and the per-magnet "Add all") in the add sheet, pressed while a magnet is still looking for its file list, now downloads every file once the list arrives. It used to add the task waiting for a file choice, so it showed as paused.
  • Magnets resolve faster in big swarms. A lookup asks 16 peers at once instead of 4. Before, a swarm listing 1,400 peers had about 48 asked per 2-minute round and never resolved; after, it resolved in 72 s.
  • Ketch stops dialing itself. Trackers and the DHT kept handing back our own announcements, so lookups dialed this machine's public address on its current and earlier listen ports (about 1,300 attempts in one log); for a small swarm, nearly every "peer" was us.
  • No peer is dialed twice because a tracker listed it as an IPv4-mapped IPv6 address (::ffff:a.b.c.d).

How

  • library:core: DownloadCoordinator.resume treats a self-managed task with an output path and no saved segments as resumable (an empty segment list), instead of falling back to a fresh start, which picks a new name (n).
  • app:shared: IntakeEntry.allFiles, set by both buttons; requestsFor sets awaitFileSelection only for a plain Add anyway.
  • library:torrent:
    • METADATA_PARALLEL_PEERS 4 → 16; dials keep the standard connect deadline, so a peer only reachable over uTP still gets TCP's 10 s and then uTP. Only one exchange at a time holds the metadata buffer, so memory stays bounded. fetchFromMetadataPeers reports peers asked (onAsked) beside peers found, and skips peers a skip predicate rules out when their turn comes, retries included.
    • KotlinTorrentEngine.isOwnAddress drops peers at our public address (any port) from tracker answers (announceToTracker wraps every TrackerTiers), DHT lookups and metadata lookups. The address comes from TorrentPortMapper (new onExternalAddress: NAT-PMP's reply, asked again on renewal, or UPnP GetExternalIPAddress), else from the DHT's own vote (DhtNode.externalHost).
    • numericHost writes ::ffff:a.b.c.d as a.b.c.d. The DHT already refused mapped node addresses, so only peers change.
  • AGENTS.md updated.

Tests

  • :library:core:jvmTest and :library:torrent:jvmTest pass in full; the new resume_withoutSavedSegments_keepsTheChosenOutputPath fails without the fix.
  • :app:shared:jvmTest --tests '*IntakeStateTest*' passes (66), including the new downloadAllNow_magnetOnSupportingDevice_downloadsEveryFile.
  • New or updated: TorrentMetadataPeersTest (16 at once, asked counts, a peer ruled out after it failed), PortMappingTest (the external address from NAT-PMP and UPnP), TorrentTrackerTest (a mapped peers6 entry).
  • Ran on the desktop app for about two hours of repeated downloads of three public torrents, including a restart mid-download.
  • Not run: iOS and Android compiles (CI covers them).

Known limits

  • A router behind further NATs reports its own outer address, which is still private, so that address never matches a peer; the DHT's vote then supplies the real public one.
  • Another device behind the same public address is only found through local discovery (LSD), not through trackers or the DHT.

…ter and stop dialing ourselves

- A torrent whose process exited before its first save restarted fresh under a new
  `name (n)` beside the folder its ownership journal is bound to, failing every retry with
  "Ownership journal belongs to another task". A source that writes its own files now resumes
  into the output path its task chose, even without saved segments.
- "Download all files now" (and "Add all") on a magnet still looking for its file list added it
  waiting for a file choice; it now downloads every file once the list arrives.
- Magnet lookups ask 16 peers at once, each allowed 8 s to connect, instead of 4 with up to
  20 s, which left most of a large swarm unasked; the log reports peers found and asked.
- Peers at our own public address (the router's while it maps our port, else the DHT's vote)
  are left out of tracker and DHT answers and of lookups, which dialed our current and earlier
  listen ports; IPv4-mapped IPv6 peers are read as IPv4, so none is dialed twice.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T14:14:28.626591Z 0909047 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 09090470bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 871 files  ± 0   1 898 suites  ±0   22m 16s ⏱️ -36s
12 705 tests +12  12 460 ✅ +12  245 💤 ±0  0 ❌ ±0 
15 194 runs  +16  14 949 ✅ +16  245 💤 ±0  0 ❌ ±0 

Results for commit c786b92. ± Comparison against base commit 8fa48e3.

@linroid
linroid merged commit 876bf95 into main Oct 13, 2026
14 checks passed
@linroid
linroid deleted the fix/torrent-resume-and-peer-discovery branch October 13, 2026 14:38
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.

1 participant