Repository navigation
fix(config): reject bind_port == metrics_port; warn on well-known ports (#267) - #290
Conversation
…ts (#267) A shared port number across network.bind_port (QUIC/UDP) and observability.metrics_port (HTTP/TCP) wouldn't actually collide at bind time because of the UDP/TCP split, but it's almost certainly an operator typo — they'll file a bug when the two log streams get confusing — so fail fast at config resolution instead. Ports below 1024 get a stderr warning (not an error): operators legitimately bind QUIC on 443 or use CAP_NET_BIND_SERVICE, but flagging the well-known range surfaces the common "ran as non-root, wondered why bind failed" case. The new check lives next to `ensure_region_when_publishing_global` — it is inherently cross-section (inputs come from `network` and `observability`) so it belongs at the resolver boundary, not inside either sub-resolver. Closes #267. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces a validation step for node port assignments to prevent collisions between the network bind port and the metrics port, while also providing warnings for ports in the well-known range (< 1024). The feedback suggests refining this logic to correctly handle port 0, which signifies ephemeral port allocation; specifically, the collision check should allow both ports to be 0, and the well-known port warning should be suppressed for port 0 to avoid misleading messages about elevated privileges.
| anyhow::ensure!( | ||
| network.bind_port != observability.metrics_port, | ||
| "network.bind_port ({}) must differ from observability.metrics_port ({}); \ | ||
| QUIC (UDP) and metrics (TCP) would not collide at bind time, but sharing \ | ||
| the same port number is almost certainly an operator typo", | ||
| network.bind_port, | ||
| observability.metrics_port, | ||
| ); |
There was a problem hiding this comment.
The collision check currently rejects configurations where both ports are set to 0. In networking, port 0 is a special value that instructs the OS to allocate an ephemeral (random) port. If both bind_port and metrics_port are set to 0, the OS will allocate two distinct ports, so they won't actually collide. This check should be relaxed to allow port 0, which is common in testing or dynamic environments.
| anyhow::ensure!( | |
| network.bind_port != observability.metrics_port, | |
| "network.bind_port ({}) must differ from observability.metrics_port ({}); \ | |
| QUIC (UDP) and metrics (TCP) would not collide at bind time, but sharing \ | |
| the same port number is almost certainly an operator typo", | |
| network.bind_port, | |
| observability.metrics_port, | |
| ); | |
| anyhow::ensure!( | |
| network.bind_port == 0 || observability.metrics_port == 0 || network.bind_port != observability.metrics_port, | |
| "network.bind_port ({}) must differ from observability.metrics_port ({}); \ | |
| QUIC (UDP) and metrics (TCP) would not collide at bind time, but sharing \ | |
| the same port number is almost certainly an operator typo", | |
| network.bind_port, | |
| observability.metrics_port, | |
| ); |
There was a problem hiding this comment.
Agreed — port 0 is "let the OS pick", so two zeros resolve to distinct ephemeral ports and don't collide. Fixed in 3e47f14: the equality check now short-circuits when either side is 0. New test validate_port_layout_allows_both_zero pins the contract.
I went with an if network.bind_port != 0 && observability.metrics_port != 0 guard around the ensure! rather than the inline disjunction — same semantics, but keeps the error message generator from reading "must differ from" predicates on an inherently-symmetric 0==0 case.
| ("network.bind_port", network.bind_port), | ||
| ("observability.metrics_port", observability.metrics_port), | ||
| ] { | ||
| if port < 1024 { |
There was a problem hiding this comment.
The warning for well-known ports (< 1024) currently triggers for port 0. However, port 0 is not a well-known port and does not require elevated privileges to bind (it's a request for an ephemeral port). The warning message about requiring elevated privileges is misleading in this case. The check should be updated to exclude port 0.
| if port < 1024 { | |
| if port > 0 && port < 1024 { |
There was a problem hiding this comment.
Agreed — port 0 isn't well-known and needs no elevated privilege, so the warning text was misleading. Fixed in 3e47f14 using (1..1024).contains(&port) to exclude 0. Same behavior as the suggested port > 0 && port < 1024, just the idiomatic-Rust range form.
Port 0 is the sentinel for OS-assigned ephemeral ports: two zeros resolve to distinct ports at bind time (no collision), and port 0 needs no elevated privilege (not a well-known port). - Skip the bind==metrics equality check when either side is 0 - Skip the <1024 well-known warning for port 0 (the warning's "requires elevated privilege" text was misleading for it) Adds two regression tests: both-zero accepted, zero-with-nonzero accepted. Addresses review comments on PR #290. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Summary
Adds a cross-section port-layout check to
resolve_config:network.bind_port == observability.metrics_port. QUIC (UDP) and the metrics server (TCP) wouldn't actually collide at the OS bind layer, but setting the two equal is almost certainly an operator typo — the check fails fast at config load with both field names in the message.CAP_NET_BIND_SERVICE— but the warning surfaces the "ran as non-root, wondered why bind failed" case that's otherwise a cryptic startup error.Lives next to
ensure_region_when_publishing_globalbecause it's inherently cross-section: inputs come fromnetworkandobservability, so the rule belongs at the resolver boundary rather than inside either sub-resolver.Closes #267.
Why
eprintln!for the warningConfig resolution runs before
tracing_subscriberis initialized (seecommands::run— config first, theninit_tracing).tracing::warn!at this point would be eaten by the default no-op subscriber.eprintln!matches the existing pattern already used atcommands.rsfor the "ignoring malformed RUST_LOG" warning.Test plan
cargo clippy -p decdn-node --all-targets -- -D warnings→ cleancargo test -p decdn-node --lib config::tests::validate_port_layout→ 3 new tests passvalidate_port_layout_rejects_bind_equal_metricsvalidate_port_layout_accepts_distinct_portsvalidate_port_layout_allows_well_known_port(pins the "warn, don't fail" contract for <1024)cargo test -p decdn-node→ full suite 57 passed🤖 Generated with Claude Code