Skip to content

fix(torrent): remove port mappings a stopped mapper asked for - #579

Merged
linroid merged 2 commits into
mainfrom
fix/torrent-port-mapping-leaks
Oct 17, 2026
Merged

linroid merged 2 commits into
mainfrom
fix/torrent-port-mapping-leaks

Conversation

@linroid

@linroid linroid commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Problem

Ketch left NAT-PMP mappings on the router. TorrentPortMapper only 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, so stop() 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

  • The mapper notes every mapping request before sending it (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).
  • Each pass removes what it no longer holds (prune): layers renewal no longer reaches, ports a gateway changed, unanswered requests. This replaces the old renewing.drop(...) removal.
  • A UPnP request the gateway refused is forgotten without a delete, as the port may be another device's (error 718).
  • KotlinTorrentEngine.startPortMapping no longer starts a mapper once the engine stopped running, so a setPortMapping(true) racing stopRuntime cannot leave one behind.

Tests keep mapping ports; they just no longer leak them.

Testing

  • New PortMappingTest.mapper_stoppedBeforeTheGatewayReplies_removesWhatItAskedFor: a NAT-PMP gateway grants without replying, the mapper stops, the gateway sees the delete. Fails on main, passes here.
  • :library:torrent:jvmTest (full suite) and compileTestKotlinIosSimulatorArm64 pass.
  • Against a real router (miniupnpd), polling its UPnP mapping table every second while TorrentPublicV2WorkflowTest, KotlinTorrentSourceTest, TorrentSourceRunTest and KotlinRuntimeDhtTest ran: main left 8 mappings behind; this branch created 2 during the run and left none. The full suite left none either.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 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-17T15:53:16.213269Z fe830c9 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 17, 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: 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.
@linroid
linroid merged commit 2462373 into main Oct 17, 2026
11 checks passed
@linroid
linroid deleted the fix/torrent-port-mapping-leaks branch October 17, 2026 16:01
linroid added a commit that referenced this pull request Oct 17, 2026
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.
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