Skip to content

fix(torrent): answer a new LAN peer's announce so it finds us at once - #630

Merged
linroid merged 2 commits into
mainfrom
fix/torrent-lsd-answer
Oct 23, 2026
Merged

linroid merged 2 commits into
mainfrom
fix/torrent-lsd-answer

Conversation

@linroid

@linroid linroid commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Found while testing Local Service Discovery (BEP 14) between two Ketch instances on one machine: a seeder (ketch server) and a downloader (ketch <magnet>) with no trackers, DHT or web seed to help.

Problem

The downloader found the seeder only when the seeder next announced the torrent. That's every 5 minutes, so two runs of 90 s and 180 s found nothing, and a third waited about 100 s. Sniffing the group showed both sides' announces going out on the LAN. The seeder heard the newcomer's announce, but a complete swarm never dials the peers it hears, so the newcomer only learns of the seeder from the seeder's own announce.

Picture a phone joining the Wi-Fi while the desktop seeds: it waits up to 5 minutes for a peer on the same LAN.

What changes

TorrentLocalDiscovery.accept: when an announce brings a peer we haven't heard from this minute, for a torrent we take part in, that torrent's next announce moves up to the first moment BEP 14 allows (max(now, last announce + 1 min)), and the announce loop is woken. Many newcomers within a minute still produce at most one announce per torrent, so the per-torrent limit holds and there's no storm.

Verification

  • Live, seeder and downloader on this branch: the downloader found the seeder (LAN peer 192.168.31.11:16999) and downloaded the 16 MB file in 4 s, with the seeder answering within the same second. Before: about 100 s, up to 5 minutes. SHA-256 matches.
  • New TorrentLocalDiscoveryTest cases: accept_newPeer_isAnsweredAsSoonAsAMinuteAllows (an immediate answer two minutes in; peers within the next minute get a single answer at the minute mark) and accept_peerOfAnotherTorrent_isNotAnswered.
  • ./gradlew :library:torrent:jvmTest passes.

A device that joined the LAN found a seeder next to it only at the
seeder's next BEP 14 round, up to five minutes later: a complete swarm
never dials the peers it hears, so the newcomer learns of it only from its
announce. A peer heard for one of our torrents for the first time in a
minute now brings that torrent's next announce forward to the first
moment BEP 14's one a minute allows. A magnet on the LAN found its seeder
in 4 s, where it waited 100 s before.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-23T17:05:53.487381Z 95995cd 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: 95995cdc5b

ℹ️ 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".

Answering every peer not heard in a minute let two instances echo each
other: each one's answer reached the other just as the minute ran out,
and was answered in turn, every minute. Only a peer not heard for a
torrent in two announce rounds (10 minutes) is answered now, so peers
that announce every round never answer each other, while a device that
joins the LAN still gets its answer at once.
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 2 200 files  + 1   2 228 suites  +1   56m 32s ⏱️ -12s
15 350 tests +16  15 082 ✅ +17  268 💤 ±0  0 ❌  - 1 
18 715 runs  +22  18 447 ✅ +23  268 💤 ±0  0 ❌  - 1 

Results for commit 92aa48b. ± Comparison against base commit edebbd5.

@linroid
linroid merged commit 2e1fce4 into main Oct 23, 2026
14 checks passed
@linroid
linroid deleted the fix/torrent-lsd-answer branch October 23, 2026 18: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