Skip to content

Fix -c ending other targets early and losing the final IPv6 Echo Reply - #173

Merged
lance0 merged 3 commits into
masterfrom
fix/count-shutdown-accounting
Nov 13, 2026
Merged

lance0 merged 3 commits into
masterfrom
fix/count-shutdown-accounting

Conversation

@lance0

@lance0 lance0 commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Two probe-accounting bugs held out of #171 because they change shutdown ordering and needed live testing.

1. -c completion cancelled every other target

Symptom. Each probe engine cancelled the token shared by all engines and the receivers as soon as its own -c rounds were done. With several targets (or --resolve-all), the first one to finish stopped the rest mid-run, and the receivers flushed their in-flight probes as timeouts. It also hit single-target runs: the receivers had already shut down before the batch settle window started, so a final-round reply slower than the interval was counted as loss.

Fix. Engines now just return after their rounds and never cancel the shared token. A new finish_engines helper in main.rs, used by batch, streaming and TUI modes, waits for every engine, gives the receivers timeout + 500ms for the last round, then cancels. Batch mode already had that wait; it just ran after the receivers were gone. Ctrl-C and quit still cancel at once, and batch modes no longer sit through the 3.5 s settle window after a Ctrl-C. An engine error no longer ends the run early: finish_engines reports it once the other engines are done.

2. Final-round IPv6 Echo Replies were never drained

Symptom. On Linux, when ttl falls back to ICMPv6 datagram (ping) sockets because IPv6 raw sockets are unavailable, the destination's Echo Replies reach only the engine's send socket. The engine read that socket right after sending each round and checked the -c limit before reading it again, so the last round's destination reply was never read. The destination always showed one lost probe.

Fix. Once the -c limit is reached, the IPv6 ICMP engine keeps reading the send socket (every 10 ms) until no probe at or past the destination TTL is pending for its target, the probe timeout passes, or the run is cancelled.

Scope note. With raw sockets, the normal Linux mode since check_permissions requires them, the receive socket also gets the Echo Replies, so the receiver records them with correct RTTs. Live runs on kernel 7.0 show no final-round loss there, before or after this change. The "only the send socket" comment on poll_ipv6_echo_reply holds for the datagram fallback only. In that fallback the engine also measures destination RTT as roughly one interval, because it reads replies only once per round. That is pre-existing and not changed here.

Tests

  • trace::engine::tests::count_completion_leaves_the_shared_token_alone runs a real engine (IPv6 UDP to ::1, which needs no raw socket) with -c 2 and asserts it sends 4 probes and leaves the shared token uncancelled. Fails on master (assertion failed: !cancel.is_cancelled()), checked by putting the self.cancel.cancel() calls back.
  • tests::finish_engines_waits_for_every_engine_then_the_reply_window (paused clock): the run ends 13.5 s in (slowest engine at 10 s plus the 3.5 s window), and a quit returns at once.
  • trace::engine::tests::final_round_ipv6_echo_reply_is_drained (Linux): queues a pending probe, runs the per-round poll before the reply exists, delivers an Echo Reply 50 ms later on a loopback UDP socket standing in for the send socket, and asserts the end-of-run drain records it. Fails with master's behavior (no read after the last round), checked by stubbing the drain to return immediately: assertion failed: pending.read().is_empty().
  • Gates: cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo clippy --all-targets --no-default-features -- -D warnings, and cargo test all pass.

Live verification

All runs used release builds of master (47638fd) and this branch in ubuntu:24.04 containers (NET_RAW).

Bug 1, multi-target batch (-c 10 --json --rate 10 1.1.1.1 8.8.8.8 9.9.9.9 192.0.2.1; --rate makes the unreachable 30-hop target's rounds longer than the interval):

target master rounds / sent branch rounds / sent
1.1.1.1 10 / 102 10 / 102
8.8.8.8 10 / 102 10 / 102
9.9.9.9 10 / 111 10 / 111
192.0.2.1 4 / 120 (1 probe never resolved) 10 / 300

Without --rate, all targets tick in lockstep and master happens to give every target 10 rounds. Hop 1 (home router) answers 2 to 8 of 10 in both builds because of ICMP rate limiting, so it is not counted as evidence here.

Bug 1, --resolve-all (-c 5 --json --rate 10 --resolve-all one.one.one.one): master gave 1.1.1.1 4 rounds (6 probes neither answered nor timed out) and 1.0.0.1 3 rounds. The branch gives all four addresses (two v4, two v6) 5 rounds, with sent = received + timeouts everywhere.

Bug 1, --stream-json (-c 5 --rate 10 1.1.1.1 192.0.2.1): master's summary shows 192.0.2.1 total_sent 90 (3 rounds). The branch shows 150 (5 rounds).

Bug 1, single target, slow reply (Docker bridge target with tc netem delay 1500ms, -c 5 --json): master gives the destination 4/5 with 1 timeout, and the branch gives 5/5.

Bug 2 (ICMPv6 datagram fallback: Docker IPv6 bridge network, a seccomp profile denying socket(AF_INET6, SOCK_RAW), net.ipv4.ping_group_range=0 2147483647, target container with netem delay 30ms, -c 5 --json; two runs each):

build destination received / sent
master 4/5, 4/5
bug 1 fix only 4/5, 4/5
branch 5/5, 5/5

Raw-socket control on the same network: master gives 5/5 with a 30 ms RTT. On the public internet, -c 5 --json 2606:4700:4700::1111 gives 5/5 at the destination for both builds, and the shutdown trace shows identical timing: engines done at 5.01 s, cancel at 8.51 s.

Shutdown

  • --report -c 30 with Ctrl-C at 4 s: master exits after 9.6 s, the branch after 5.0 s. Both print the report.
  • TUI q mid-run: exits in 0.19 s.
  • TUI with -c 3 --rate 10 1.1.1.1 192.0.2.1 closes by itself after 12.8 s on the branch (the slow target's three rounds plus the reply window). Master closes at 9.3 s, cutting the slow target short.

Not covered

  • Targets added from the TUI (o) are still outside finish_engines. With -c, the initial targets finishing ends the run, as before. Add-target cancellation was deferred to its own review in Review batch: CLI validation, replay read-only, export safety, docs and CI #171.
  • Windows, macOS and FreeBSD were not built locally because the cross C toolchains are missing. The new code is either platform-neutral or behind cfg(target_os = "linux"), and the engine test is behind cfg(unix). CI covers all three.

Each probe engine cancelled the token shared by every engine and the
receivers as soon as its own -c rounds were done. With several targets
(or --resolve-all) the first to finish stopped the others mid-run, and
the receivers flushed their in-flight probes as timeouts. Even with one
target, the receivers had already shut down before the batch settle
window began, so a final-round reply slower than the interval was
counted as loss.

Engines now just return after their rounds. A shared finish_engines
helper, used by batch, streaming and TUI modes, waits for every engine,
gives the receivers the probe timeout plus a margin for the last round,
then cancels. A quit still cancels immediately and skips that wait.
On Linux, ICMPv6 Echo Replies to a datagram (ping) socket reach only the
socket that sent the request, so the engine reads them from its send
socket. It polled that socket right after sending each round and checked
the -c limit before polling again, so the last round's destination
replies were never read and the destination always showed one lost
probe.

When the -c limit is reached, the IPv6 ICMP engine now keeps polling
the send socket until no probe an Echo Reply could answer is pending for
its target, the probe timeout passes, or the run is cancelled.
@lance0
lance0 marked this pull request as ready for review November 13, 2026 00:26
@lance0
lance0 merged commit 9ac93ce into master Nov 13, 2026
6 checks passed
@lance0
lance0 deleted the fix/count-shutdown-accounting branch November 13, 2026 00:26
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