Repository navigation
Fix macOS single-hop traces: per-probe send sockets for ICMP/UDP/TCP (#12) - #96
Merged
Merged
Conversation
On macOS, sendto() is asynchronous: the kernel stamps each queued datagram with the socket's current IP_TTL at drain time, not at the sendto() call. So rapid per-probe setsockopt(IP_TTL) calls on a shared DGRAM socket can all be transmitted with the final TTL, leaving only the destination/gateway visible. Apple's DTS confirms this (developer.apple.com/forums/thread/726398) and recommends a fresh socket per send over a timing delay. The prior fix was a fixed 500us inter-probe delay, which only masks the race and is insufficient on fast Apple Silicon — reproduced on the wire on an M2 (macOS 26.5.1): TTL 13 repeated 13x then 14..30, needing ~20ms spacing to clear, far more than the 500us default. run_icmp now builds a fresh send socket per probe on macOS via the new build_send_socket() helper (which also re-applies interface and source-IP binding). This is deterministic regardless of hardware speed. The shared socket is retained for the single PMTUD probe and Linux Echo Reply polling. Other platforms are unchanged. Correlation is unaffected: macOS already rewrites the ICMP identifier on DGRAM sockets, and the receiver matches on the probe sequence / payload-embedded identifier rather than the kernel identifier (correlate.rs payload fallback). The 500us delay is kept as a secondary safety margin and still covers the FreeBSD/NetBSD raw-socket paths, which are not converted here. Verification: macOS on-device confirmation still pending (the macOS send path is cfg-gated off when building on Linux).
The async-sendto TTL race is not ICMP-specific: any shared socket that mutates IP_TTL per probe is affected, since macOS applies the socket's current TTL when each queued datagram drains. run_udp and run_tcp had the same shared-socket + per-probe setsockopt(IP_TTL) pattern, so -p udp / -p tcp traces could still collapse to a single hop on fast macOS hardware (and the 500us floor is bypassed entirely when --rate sets a shorter delay). Both paths now send each probe from a fresh socket on macOS, via new build_udp_send_socket() / build_tcp_send_socket() helpers (shared once on other platforms, per-probe on macOS, so they stay type-checked everywhere): - UDP: re-binds the same per-flow source port each probe. create_udp_dgram_ socket_bound_full now sets SO_REUSEADDR before bind so rapid re-binding of that port doesn't fail with EADDRINUSE. Flow identification and NAT detection are preserved because the source port is unchanged. - TCP: the source port lives in the crafted SYN, not a socket bind, so the per-probe raw socket just re-applies source-IP binding and DSCP. Non-macOS behavior is unchanged (single shared socket / per-flow Vec reused). The 500us delay remains as a margin and for the FreeBSD/NetBSD raw paths. Verification: macOS on-device confirmation still pending (the macOS send paths are cfg-gated off when building on Linux). Linux: fmt, clippy -D warnings, 217 lib tests pass.
There was a problem hiding this comment.
Pull request overview
This PR addresses a macOS-specific traceroute correctness issue where rapid setsockopt(IP_TTL); sendto() loops can result in probes being transmitted with the wrong TTL (collapsing traces to a single hop). The fix makes macOS probe sending deterministic by using per-probe send sockets across ICMP/UDP/TCP, while keeping other platforms’ behavior unchanged.
Changes:
- Add per-probe send socket creation on macOS for ICMP/UDP/TCP to eliminate TTL stamping races caused by async
sendto(). - Add macOS “preflight” socket creation/binding checks for UDP/TCP to fail fast on misconfiguration.
- Update documentation/CHANGELOG to describe the macOS race and the per-probe socket mitigation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/trace/engine.rs | Introduces macOS per-probe sockets for ICMP/UDP/TCP send paths and adds macOS preflight validation. |
| src/probe/udp.rs | Adds macOS-only SO_REUSEADDR to allow rapid re-binding of per-flow UDP source ports when using per-probe sockets. |
| CHANGELOG.md | Documents the macOS single-hop trace fix and its rationale. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+82
to
+85
| /// macOS no longer depends on this delay for correctness: the ICMP path sends each | ||
| /// probe from a fresh socket (`build_send_socket`), which removes the race entirely. | ||
| /// The delay is retained as a small safety margin and still covers the FreeBSD/NetBSD | ||
| /// raw-socket send paths, which have not been converted to per-probe sockets. |
Comment on lines
+94
to
+96
| // Minimum inter-probe spacing so a queued datagram drains (and is stamped with | ||
| // its intended TTL) before the next send. This is a margin, not a guarantee; | ||
| // the deterministic fix on macOS is the per-probe socket in run_icmp. |
On macOS, UDP and TCP create their send socket inside the probe loop (for the per-probe TTL fix). A fatal misconfiguration — invalid --source-ip, address family mismatch, port/interface bind failure — would then be logged and `continue`d on every probe: an infinite silent spin with no sent counters in headless/TUI runs, where non-macOS fails fast via `?`. (ICMP was already safe; its shared socket is still created pre-loop.) Add a macOS preflight that creates/binds the send socket(s) once before the loop with `?` — per UDP flow and once for TCP — so configuration errors abort at startup like other platforms. The loop still builds a fresh socket per probe; its log+continue now only tolerates genuinely transient failures. Also narrow the SO_REUSEADDR added for rapid per-probe re-binding to macOS only; other platforms keep their previous UDP socket options unchanged. Linux: fmt, clippy -D warnings, 217 lib tests pass.
lance0
force-pushed
the
fix/macos-ttl-per-probe-socket
branch
from
June 27, 2026 01:43
60ccba7 to
91addd3
Compare
- udp.rs: propagate set_reuse_address failure with `?` instead of ignoring it. macOS per-probe rebinding of the same flow source port depends on it, so a failure should fail fast at the preflight rather than surface as a loop-time EADDRINUSE. SO_REUSEADDR alone is sufficient (verified on macOS 26.5.1); SO_REUSEPORT is not needed since each socket is closed before the next bind. - engine.rs: apply_rate_limit comments described the deterministic fix as ICMP-only; it now covers ICMP, UDP, and TCP. - CHANGELOG: the on-wire macOS capture confirmed the root cause, not the fix — reworded to avoid overclaiming.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #12 — on macOS, traces can collapse to a single hop (only the destination or gateway visible), intermittently and more often on fast Apple Silicon.
Root cause (confirmed by Apple DTS, thread 726398):
sendto()is asynchronous. The kernel stamps each queued datagram with the socket's currentIP_TTLwhen it drains from the send queue — not at thesendto()call. So a tight loop ofsetsockopt(IP_TTL=n); sendon a shared socket can transmit every queued probe with the final TTL, collapsing the trace to one hop.The previous mitigation was a fixed 500µs inter-probe delay. That only masks the race and is hardware-dependent. Reproduced on the wire on an M2 / macOS 26.5.1: outgoing
ttl 13repeated 13× then 14→30, with full correctness only returning at--rate 50(~20ms spacing) — 40× the current default.Fix
Each probe is now sent from a fresh socket on macOS (Apple's recommended approach over a timing delay). One socket carries exactly one datagram, so its TTL is always correct — deterministic regardless of CPU speed. The race is not ICMP-specific, so all three probe modes are covered:
run_icmp): per-probe socket viabuild_send_socket. The shared socket is retained for the single PMTUD probe and Linux Echo Reply polling.run_udp): per-probe socket viabuild_udp_send_socket, re-bound to the flow's source port so Paris/Dublin flow identification and NAT detection are preserved.SO_REUSEADDR(macOS-only) lets the same port re-bind rapidly.run_tcp): per-probe raw socket viabuild_tcp_send_socket; the source port lives in the crafted SYN, so nothing flow-specific is lost.Each path also does a macOS preflight that creates/binds the socket(s) once before the loop with
?, so a misconfiguration (bad--source-ip, interface, port) fails fast at startup instead of spinning silently — matching non-macOS behavior.Other platforms are unchanged (single shared socket / per-flow Vec reused; no new socket options). The 500µs delay is kept as a margin and still covers the FreeBSD/NetBSD raw paths.
Correlation is unaffected: macOS already rewrites the ICMP identifier on DGRAM sockets, and the receiver matches on the probe sequence / payload-embedded identifier rather than the kernel identifier (
correlate.rspayload fallback) — verified across both the Echo Reply and Time Exceeded match paths.Verification
cargo fmt --check,clippy --all-targets -D warnings, and the test suite all pass — the macOS job compiles and lints the macOS-only send paths.-p icmp/udp/tcp(force the old behavior first by temporarily disabling the 500µs delay), ideally confirmed on the affected M2.Known edge
If per-probe socket creation fails transiently mid-run (e.g. fd exhaustion) after the preflight passes, the loop logs and continues rather than aborting — the same degradation as a non-macOS mid-run send failure.