Skip to content

fix(torrent): pick a listen port UDP has free too - #593

Merged
linroid merged 3 commits into
mainfrom
fix/torrent-udp-shares-listen-port
Oct 18, 2026
Merged

linroid merged 3 commits into
mainfrom
fix/torrent-udp-shares-listen-port

Conversation

@linroid

@linroid linroid commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Problem

UtpEndpointTest.engine_utpSwitchedOff_refusesNewUtpConnections flaked on windows-latest (run 39083701755, attempt 1, on #591) with uTP connect failed: No uTP answer. It failed on the first dial, the one made while uTP is on, and took 3.08 s. dial_silentPort_failsAfterItsSynRetries takes the same time, so the engine never answered either SYN. One lost packet wouldn't explain that.

Cause: the engine listens on a port the system picks (listenPort = 0) and then binds DHT/uTP over UDP on the same port number. If that number is already held for UDP (by another socket, or on Windows by a UDP-only excluded port range), udpPort quietly falls back to any free port. Peers that dial the listen port over uTP then reach nothing. The apps hit this too, since they all use listenPort = 0, and port mapping also forwards UDP to that port.

Fix

KotlinTorrentEngine.start now uses listenForPeers(). When the system picks the port, it checks whether that UDP port can be bound. If not, it listens again on a new port, up to 8 tries, and after that keeps the last port as before. A port set in config (ketch server) is used as it is.

Tests

  • New engine_listenPortTakenForUdp_picksAPortFreeForBoth: a network that refuses UDP on the first port TCP got. It fails without the fix and passes with it on any OS.
  • ./gradlew :library:torrent:jvmTest --tests '*UtpEndpointTest*' --rerun: 5 runs in a row, all passed.
  • ./gradlew :library:torrent:jvmTest: passed.

When the system picks the listen port, the TCP port it hands out can be
held for UDP by another socket, or on Windows fall in a UDP excluded port
range. DHT and uTP then bound another port, so peers dialing the listen
port over uTP reached nothing; this flaked UtpEndpointTest's
engine_utpSwitchedOff_refusesNewUtpConnections on windows-latest with
"No uTP answer". The engine now tries up to 8 system-picked ports for one
whose UDP port is free.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 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-18T01:43:19.500938Z e29fd5b 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.

@claude

claude Bot commented Oct 18, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@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: e29fd5bd45

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

@linroid
linroid merged commit 6c0c9f9 into main Oct 18, 2026
11 checks passed
@linroid
linroid deleted the fix/torrent-udp-shares-listen-port branch October 18, 2026 02:21
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