Repository navigation
fix(torrent): pick a listen port UDP has free too - #593
Conversation
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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
There was a problem hiding this comment.
💡 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".
Problem
UtpEndpointTest.engine_utpSwitchedOff_refusesNewUtpConnectionsflaked onwindows-latest(run 39083701755, attempt 1, on #591) withuTP 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_failsAfterItsSynRetriestakes 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),udpPortquietly 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 uselistenPort = 0, and port mapping also forwards UDP to that port.Fix
KotlinTorrentEngine.startnow useslistenForPeers(). 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
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.