Skip to content

fix(torrent): keep silent PCP probes from holding up port mapping removal - #590

Merged
linroid merged 1 commit into
mainfrom
fix/torrent-portmap-removal
Oct 18, 2026
Merged

linroid merged 1 commit into
mainfrom
fix/torrent-portmap-removal

Conversation

@linroid

@linroid linroid commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Summary

A review of #587 found two problems in how the torrent port mapper removes its mappings when the engine stops. Both were older than #587.

The router's mapping is removed within the 2 s budget

  • Before: stop() removed every requested mapping one at a time, outermost layer first, within one 2 s window.
    • mapPcp records a request before every PCP MAP probe it sends to a guessed outer gateway, including gateways that never answer.
    • Removing such a probe waited its whole 250 + 500 + 1000 ms retry schedule. With two probes at layer 1, the window ran out before the home router's layer-0 mapping was asked to go, so the router kept forwarding the port for up to the hour-long lease.
  • Now: all removals go out at the same time (unmapAll), so a gateway that does not answer delays none of the others. The removal order made no difference to safety. A pass that prunes stale requests removes them the same way, and forgets a request only after its removal was sent, so a stop() that interrupts the prune sends it again.
  • Each request now records whether its gateway has answered (Requested.answered):
    • NAT-PMP and UPnP gateways answered during discovery, before any request.
    • A PCP gateway counts once a MAP gets its reply.
  • Until a gateway has answered, its removal is sent once and waits only the first retry window (PcpClient.map(once = true), 250 ms).

The removal log counts only real layers

  • Before: the log counted a layer for every request, PCP probes included. A stop during a probe reported a NAT layer that does not exist, for example "1 of 2 NAT layer(s) confirmed it; the others may still forward the port".
  • Now: a layer counts only when its gateway answered, either before the stop or by confirming the removal. Unanswered probes are left out of the log.

Tests

  • New NatLayersTest case: a NAT-PMP home router whose WAN address is in carrier-grade NAT space (100.64.0.2), a silent PCP guess outside it, and a stop during that probe. The layer-0 mapping is removed within the default 2 s budget, the log reads "Removed the mapping of port 6881 through 1 NAT layer(s)", and the probe's removal is sent once. Against the old mapper this case fails with the "1 of 2 NAT layer(s)" line.
  • Existing tests that checked the removal order now accept the removals in any order.
  • :library:torrent:jvmTest and :library:torrent:compileTestKotlinIosSimulatorArm64 pass locally.

AGENTS.md and the port mapping details in docs/torrent.md are updated.

…oval

stop() removed every requested mapping one after another, outermost first,
within one 2 s budget. PCP MAP probes to guessed outer gateways are noted
before they go out, and a removal sent to a gateway that never answered
waited its whole 250 + 500 + 1000 ms schedule, so two of them used up the
budget before the home router's mapping was asked to go, and the router kept
forwarding the port until its lease ended. The probes also counted as NAT
layers in the removal log, which then reported a layer that does not exist
and said it may still forward the port.

- Removals (at stop and when a pass prunes stale requests) go out side by
  side, so a silent gateway holds none of the others up. A pruned request is
  forgotten only once asked to go, so a stop that cuts it short sends it again.
- Each request records whether its gateway answered: NAT-PMP and UPnP
  gateways did through discovery, a PCP gateway once a MAP got its reply.
  Until then its removal goes out once (PcpClient.map(once = true)).
- The removal log counts only layers whose gateway answered, before or by
  confirming the removal.
@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-18T00:29:37.354343Z ad36af7 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.

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 2 101 files   2 129 suites   41m 52s ⏱️
14 435 tests 14 169 ✅ 266 💤 0 ❌
17 584 runs  17 318 ✅ 266 💤 0 ❌

Results for commit ad36af7.

♻️ This comment has been updated with latest results.

@linroid
linroid merged commit ad6751e into main Oct 18, 2026
25 of 28 checks passed
@linroid
linroid deleted the fix/torrent-portmap-removal branch October 18, 2026 01:39
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