Repository navigation
fix(torrent): remove port mappings a stopped mapper asked for - #579
Conversation
The mapper only remembered mappings once a whole pass finished, so an engine stopped mid-pass (tests stop engines within seconds) left what the router had already granted for the rest of its hour-long lease. Every request is now noted before it is sent, and stop() removes them all, outermost layer first, including ones the router never answered; each pass removes what it no longer holds. A UPnP request the router refused is forgotten without a delete, as the port may be another device's. The engine also no longer starts a mapper once it is stopping.
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: fe830c9958
ℹ️ 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".
A refused renewal forgot the entry the original mapping was tracked under, so neither the next pass nor stop() removed it. Only an entry this request added is forgotten now.
The removal log added in the previous round only looked at the layers of `current`, while stop() removes every mapping in `requested`, as #579 made it do: also those of a first pass, or of a newly found outer layer, whose request went out but whose pass never finished. With `current` empty nothing was logged, however the removals went, and with an inner layer mapped a confirmed inner removal was logged as "Removed the mapping ... through 1 NAT layer(s)" while the outer gateway's removal went unconfirmed. Those are the mappings a router most likely keeps forwarding, since nothing ever confirmed them either way. The log now covers what stop() removes: the requests, grouped by the layer they were asked of, a layer counting as confirmed only when every one of its removals was, and it is written whenever anything was asked for. Whether a mapping may outlive its lease comes from the request: one asked for with UPnP's lease 0, the 725 fallback included, is marked permanent before the request goes out, so a lease granted without a reply is named too. As an unconfirmed removal may also be one the gateway carried out, the lines now say the others "may still forward the port". A PortMappingTest stops a mapper during its first pass against a NAT-PMP gateway that grants and removes without replying, and a NatLayersTest stops one while the NAT outside the router maps silently after the router confirmed its layer; both logged nothing before and now say the port may still be forwarded, the second through 1 of 2 layers.
Problem
Ketch left NAT-PMP mappings on the router.
TorrentPortMapperonly recorded what a gateway granted once a whole mapping pass finished (current = layers). An engine stopped mid-pass — test engines stop within seconds — cancelled the pass after the router had already created the mapping, sostop()had nothing to remove and the mapping stayed for its one-hour lease. On a real router, a few torrent test classes left 8 TCP+UDP pairs per run.Fix
requested, with its NAT layer).stop()removes all of them, outermost layer first and in request order within a layer, including requests the gateway never answered (NAT-PMP and PCP removal is keyed by internal port and our own client, so removing an unanswered one is harmless).prune): layers renewal no longer reaches, ports a gateway changed, unanswered requests. This replaces the oldrenewing.drop(...)removal.KotlinTorrentEngine.startPortMappingno longer starts a mapper once the engine stopped running, so asetPortMapping(true)racingstopRuntimecannot leave one behind.Tests keep mapping ports; they just no longer leak them.
Testing
PortMappingTest.mapper_stoppedBeforeTheGatewayReplies_removesWhatItAskedFor: a NAT-PMP gateway grants without replying, the mapper stops, the gateway sees the delete. Fails onmain, passes here.:library:torrent:jvmTest(full suite) andcompileTestKotlinIosSimulatorArm64pass.TorrentPublicV2WorkflowTest,KotlinTorrentSourceTest,TorrentSourceRunTestandKotlinRuntimeDhtTestran:mainleft 8 mappings behind; this branch created 2 during the run and left none. The full suite left none either.