Files
tsunagi/vendor/iroh/src/portmapper.rs
T
Aleksandr Bogomiakov 2b0c938487 Bound iroh path retries to prevent multi-connection OOM
A long-running rc.33 daemon reached OOM with roughly 22 GiB resident and
24 GiB swapped. Live inspection found 40 GiB and 10/20 GiB anonymous
container mappings; the active worker stack and tracing metadata identified
iroh's scheduled path-open retry loop. In iroh 1.2.0 every failed connection
requeues the same path, while each retry visits every connection again.
Separate control and data connections multiply the queue geometrically.

Vendor the published iroh 1.2.0 source and select it with a workspace Cargo
patch. Deduplicate retry admission and cap distinct pending paths at 64,
evicting the oldest before inserting a new candidate. Keep the current and
next retry batches bounded without combining control/data connections or
changing wire protocols, identity derivation, authentication or state.
Document source provenance, the live evidence and removal conditions.
Upstream PR 4551 merged on October 6, but inspection of the official 1.3.0
crate still shows the unconditional enqueue, so that upgrade alone is not
a fix. No version bump, release tag or installed-service change.

The default workspace suite includes the exact vendored admission module.
Three regressions failed with the original enqueue and pass with the fix:
repeated failures across connections, duplicate admission at capacity, and
10,000 distinct candidates with bounded length/capacity and FIFO eviction.
Transport integration tests continue using real endpoints and encryption.

Checks on Linux in nix develop:
- cargo fmt --all -- --check
- cargo clippy --locked --workspace --all-targets -- -D warnings
- cargo test --locked --workspace --all-targets, with only the inherited
  TSUNAGI_CONTROL_SOCKET removed from the test environment
- git diff --cached --check

Initial full-suite runs failed six DNS readiness checks because the inherited
system socket path overrode their temporary state directories. Clearing that
environment variable resolves those failures without changing application code.
Root/network-dependent ignored tests and other operating systems were not run.
2026-10-14 23:28:53 +01:00

101 lines
3.3 KiB
Rust

//! Portmapper integration.
//!
//! Wraps the real [`portmapper`] crate when the `portmapper` feature is enabled,
//! or provides a no-op stub otherwise.
use std::net::SocketAddrV4;
use tokio::sync::watch;
/// Configuration for the portmapper service (UPnP, PCP, NAT-PMP).
///
/// Port mapping asks the local router to open an external port so peers can
/// reach this endpoint directly, improving connectivity behind NATs. The
/// discovery step (UPnP uses SSDP multicast) can, however, trigger firewall
/// prompts on some networks — see [`PortmapperConfig::Disabled`].
///
/// Used with [`crate::endpoint::Builder::portmapper_config`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum PortmapperConfig {
/// Enable portmapping with default settings.
///
/// This is the default.
#[non_exhaustive]
Enabled {},
/// Disable portmapping.
///
/// Skips the UPnP/PCP/NAT-PMP gateway probing. Use this to avoid the
/// SSDP multicast discovery that can raise firewall dialogs (notably on
/// macOS), at the cost of potentially worse direct connectivity behind
/// some NATs.
Disabled,
}
impl Default for PortmapperConfig {
fn default() -> Self {
PortmapperConfig::Enabled {}
}
}
pub(crate) fn create_client(config: &PortmapperConfig) -> Client {
match config {
#[cfg(all(not(wasm_browser), feature = "portmapper"))]
PortmapperConfig::Enabled {} => Client::Enabled(::portmapper::Client::default()),
_ => {
let (tx, rx) = watch::channel(None);
Client::Disabled { _tx: tx, rx }
}
}
}
/// Portmapper client: either the real implementation or a no-op.
///
/// The disabled variant is used when the `portmapper` feature is off, on wasm,
/// or when portmapping is disabled via [`PortmapperConfig::Disabled`].
#[derive(Debug)]
pub(crate) enum Client {
/// The real portmapper client (requires the `portmapper` feature).
#[cfg(all(not(wasm_browser), feature = "portmapper"))]
Enabled(::portmapper::Client),
/// No-op: keeps the sender alive so the receiver never closes.
Disabled {
_tx: watch::Sender<Option<SocketAddrV4>>,
rx: watch::Receiver<Option<SocketAddrV4>>,
},
}
impl Client {
pub(crate) fn procure_mapping(&self) {
match self {
#[cfg(all(not(wasm_browser), feature = "portmapper"))]
Client::Enabled(c) => c.procure_mapping(),
Client::Disabled { .. } => {}
}
}
pub(crate) fn update_local_port(&self, _port: std::num::NonZeroU16) {
match self {
#[cfg(all(not(wasm_browser), feature = "portmapper"))]
Client::Enabled(c) => c.update_local_port(_port),
Client::Disabled { .. } => {}
}
}
pub(crate) fn deactivate(&self) {
match self {
#[cfg(all(not(wasm_browser), feature = "portmapper"))]
Client::Enabled(c) => c.deactivate(),
Client::Disabled { .. } => {}
}
}
pub(crate) fn watch_external_address(&self) -> watch::Receiver<Option<SocketAddrV4>> {
match self {
#[cfg(all(not(wasm_browser), feature = "portmapper"))]
Client::Enabled(c) => c.watch_external_address(),
Client::Disabled { rx, .. } => rx.clone(),
}
}
}