Skip to content

Fix probe accounting across stats reset, added targets and replay events - #179

Merged
lance0 merged 10 commits into
masterfrom
fix/accounting-edge-cases
Nov 14, 2026
Merged

lance0 merged 10 commits into
masterfrom
fix/accounting-edge-cases

Conversation

@lance0

@lance0 lance0 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Probe-accounting fixes held out of #171 and #173: stats reset, targets added from the TUI, and probe events that disagreed with the live stats. Each fix is its own commit.

1. Pending probes survived a stats reset

Symptom. After r, replies and timeouts for probes sent before the reset were counted against the fresh stats. A hop showed a reply before any new probe could have been answered, and a timeout long before the probe timeout.

Root cause. reset_stats cleared the counters but left the shared pending map alone, and nothing checked whether a probe came before the reset.

Fix. Session::predates_reset(sent_at) compares the probe's send time with the session's reset instant, which reset_stats already sets. The receiver's reply path, the timeout sweep and the engine's IPv6 Echo Reply poll skip such probes. The receiver's batch handling moved into apply_responses so it can be tested directly. This move accounts for most of the receiver.rs diff.

2. Targets added with o and -c

Symptom. finish_engines only knew the initial engines. With -c, the run ended once the initial targets finished, so an added target lost its remaining rounds.

Fix. The target manager now sends each engine it spawns to finish_engines on a channel. After each reply window, finish_engines picks up engines added in the meantime and keeps the run going until they finish. After a quit it joins whatever is still running, which is what the manager used to do. The empty-TUI behavior is unchanged: the run is still left to the user.

3. Esc while an added target is resolving

Symptom. Press Enter, then Esc while "Resolving…" is shown. The target was still added and probed for the rest of the run, but it never appeared in the TUI. Its session also disabled the receiver's single-target DNAT match. The only visible sign was that adding the host again reported "Already tracing".

Root cause. Esc only dropped the TUI's reply receiver. The manager ignored that and inserted the session anyway, and it ignored the failed reply send too.

Fix. The dropped receiver is the cancel signal: the manager checks reply.is_closed() after resolution and skips the request. The manager now replies before it spawns the engine. If Esc lands between that check and the reply, the send fails, and the manager removes the session and spawns no engine. There is no target-removal key, so removing an added target was not a case to cover.

4. late_reply in replay

Symptom. Animated replay (--replay --animate) counted a late_reply event as a reply with the recorded rtt_us: 0. The live run had already counted that probe's timeout and ignored the late reply. The replayed hop therefore showed a timeout plus a 0 ms reply for one probe, a 0 ms minimum RTT, and stats that disagreed with the hops saved in the same file.

Fix. Replay now ignores late_reply, matching the live stats. The receiver also stops recording late_reply for a reply that matched nothing only because several flows of its probe were still pending (an unknown flow). That probe later timed out, so the recording held two outcomes for it. docs/FEATURES.md now states that late_reply is informational. --replay --report/--csv/--diff read the stored hops, not the events, so only animated replay was affected.

5. IPv6 Echo Replies read by the engine produced no event

Symptom. poll_ipv6_echo_reply (Linux) updated the counters, completion and PMTUD but recorded no ProbeEvent. In the ICMPv6 datagram fallback, --stream-json emitted no destination reply while its summary said complete: true, and a TUI export had zero events, so animated replay lost the destination. In raw mode, the engine and the receiver both get each Echo Reply. When the engine read it first, the receiver found nothing pending and recorded the reply as late_reply, so every destination reply after the first round looked late.

Fix. The engine poll records the same reply event as the receiver. Whichever side removes the pending entry records the event, so it is never recorded twice. On Linux the receiver no longer records late_reply for IPv6 Echo Replies (ponytail: note: a genuinely late one goes unrecorded there). Recordings from earlier versions stored those replies only as late_reply, so their animated replay now shows fewer destination replies; the CHANGELOG says so.

Tests

All gates pass: cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo clippy --all-targets --no-default-features -- -D warnings, and cargo test (411 lib, 433 bin, 33 integration, plus replay_size and doctest).

Each test below fails with its fix removed. I checked that by disabling the specific guard and running the test:

  • trace::receiver::tests::probes_sent_before_a_stats_reset_are_not_counted: a reply and a timeout for pre-reset probes are ignored, and post-reset ones are counted.
  • tests::finish_engines_waits_for_targets_added_mid_run (paused clock): engines added mid-run and during a reply window are waited for, and the run ends at 19 s. Without picking up added engines, it ends at 4.5 s.
  • tests::canceled_add_target_request_adds_nothing: a request whose receiver is dropped adds no session and spawns no engine. With both guards removed, the session stays and an engine is spawned, as on master.
  • trace::receiver::tests::ambiguous_match_is_not_recorded_as_late_reply
  • test_replay_late_reply_matches_live_accounting (integration, replaces the old test that asserted the double count): timeout plus late_reply replays as sent 1, received 0, timeouts 1, no responders.
  • trace::engine::tests::final_round_ipv6_echo_reply_is_drained now also asserts the reply event.
  • trace::receiver::tests::ipv6_echo_reply_copy_is_not_recorded_as_late_reply (Linux).

Live verification

All runs compare release builds of master and this branch in ubuntu:24.04 containers.

Reset (TUI, container with netem delay 2000ms on egress, --timeout 5 -m 8 1.1.1.1; p, r, wait 4 s, p, then sample every 0.25 s):

  • master: 0.75 s after unpausing, hop 1 shows Sent 1 with a 2000 ms reply, which no new probe could have produced yet. Hop 5 shows 100% loss after 0.75 s, against a 5 s timeout.
  • branch: rows stay – until the first post-reset replies arrive at about 2.5 s.

-c with an added target (TUI -c 10 -m 12 1.1.1.1, add 8.8.8.8 with o at 7 s; two runs each):

build TUI exits after added target hop 1 sent
master 13.8 s, 13.6 s 7, 7
branch 20.9 s, 21.5 s 10, 10

Esc while resolving (egress delay 1500 ms so DNS is slow; o a.root-servers.net Enter, Esc after 0.2 s, then add it again):

  • master: "Already tracing a.root-servers.net (198.41.0.4)", so the canceled add had created a hidden session.
  • branch: "Added target a.root-servers.net (198.41.0.4)".

IPv6 events (-6 --stream-json -c 3 to a container on a Docker IPv6 bridge; datagram fallback via a seccomp profile denying socket(AF_INET6, SOCK_RAW); events of the destination hop):

build raw (2 runs) datagram
master seq 0 reply, seq 1–2 late_reply no events (summary complete: true)
branch seq 0–2 reply seq 0–2 reply

TUI export in datagram mode: master's file has 7 received and 0 events, and --replay --animate has nothing to animate. The branch file has 7 events, and animated replay shows the destination at hop 1 with 5 sent.

Recheck on the current head (master de26309 vs this branch, ubuntu:24.04):

  • -6 --stream-json -c 3 -i 0.5 ::1, raw: master records seq 1 and 2 as late_reply, the branch records them as reply. Datagram fallback (seccomp denies raw ICMPv6): master emits no events, the branch emits 5 reply events. Both summaries say complete: true.
  • --json -c 5 1.1.1.1 8.8.8.8 and --stream-json -c 3 with the same two targets: shutdown timing matches master (engines done at 2.5 s, cancel after one 3.5 s reply window). Every hop has 5 sent, and each stream target's reply plus timeout events add up to its total_sent.

Not verified

  • macOS, FreeBSD and Windows builds. The cross C toolchains for ring/aws-lc-sys are missing locally, so CI covers them. The new event imports in engine.rs are scoped to the Linux-only function and test.
  • One leftover race: a reset that lands inside the send syscall (after sent_at, before record_sent) leaves that one probe counted as sent with no outcome. The window is microseconds, and it is marked with a ponytail: comment.

Replies and timeouts for probes still in flight when r reset the stats
were counted against the fresh stats. The receiver, the timeout sweep
and the IPv6 Echo Reply poll now skip a probe sent before the session's
reset instant. The receiver's response handling moves into
apply_responses so it can be tested directly.
finish_engines only knew the engines started with the run, so with -c
the TUI closed once the initial targets were done and cut targets added
with o short. The target manager now hands each engine it spawns to
finish_engines, which keeps the run going until those are done too and
joins any still running after a quit.
Esc while a target was resolving only dropped the TUI's reply receiver.
The target manager still created the session and started probing, so
the target was probed for the rest of the run without showing up in the
TUI, and its session disabled the single-target DNAT match in the
receiver. The manager now skips a request whose reply receiver is gone,
and if Esc lands after that check, it stops the new engine through its
own child token and removes the session.
A late reply arrives after the receiver counted the probe's timeout,
and the live stats ignore it. Animated replay counted it as a reply
with the recorded 0 ms RTT, so the replayed hop had a timeout and a
reply for one probe, a 0 ms minimum RTT, and stats that disagreed with
the hops saved in the same file. Replay now leaves late_reply out of
the stats.

The receiver also recorded late_reply for a reply that matched no
pending probe only because several flows of that probe were still
pending (an unknown flow). That probe later timed out, so the events
held two outcomes for it. Such ambiguous matches are no longer recorded
as late replies.
poll_ipv6_echo_reply updated the hop counters, completion and PMTUD but
recorded no ProbeEvent, unlike the receiver. When the engine reads the
destination's Echo Reply (always in the Linux ICMPv6 datagram fallback),
--stream-json emitted no reply for the destination and animated replay
lost the destination hop. Whichever of the two removes the pending entry
records the event, so there is no duplicate.
On Linux both the engine's send socket and the receiver get each IPv6
Echo Reply. When the engine read it first, the receiver found nothing
pending and recorded a late_reply, so raw-mode recordings showed every
destination reply after the first round as late. The receiver no
longer records late replies for IPv6 Echo Replies on Linux.
A canceled add now removes the session before any engine starts, so the
engine needs no token of its own and never sends probes for a target the
TUI will not show.
@lance0
lance0 marked this pull request as ready for review November 14, 2026 20:43
@lance0
lance0 merged commit 186df35 into master Nov 14, 2026
7 checks passed
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