Skip to content

Bound per-hop responders, the live event log and PeeringDB reads - #178

Merged
lance0 merged 5 commits into
masterfrom
fix/bound-long-run-memory
Nov 14, 2026
Merged

lance0 merged 5 commits into
masterfrom
fix/bound-long-run-memory

Conversation

@lance0

@lance0 lance0 commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Bounds three structures that grew for the whole run in the modes that run for days (--daemon, --prometheus, long TUI sessions). One commit each.

1. Per-hop responder map

Symptom. Every distinct source address that answered at a hop got a permanent ResponderStats entry, about 5 KB each because of its pre-allocated sample buffers. Per-packet balancing behind CGNAT, a load balancer with many source addresses, or a box answering each probe from a spoofed source grew memory steadily, and slowed everything that walks the map: every TUI snapshot clones it, every Prometheus scrape and ECMP classification iterates it, and update_primary scans it on every reply. Each new address was also handed to the rDNS/ASN/GeoIP/IX workers.

Root cause. Hop::record_response_with_extensions did responders.entry(ip).or_insert_with(...) with no bound. Only reset_stats ever cleared it. #155 did not touch this; it only stopped recording replay events in headless runs.

Fix. At most Hop::MAX_RESPONDERS = 64 per hop. When a new address arrives at the cap, the responder with the fewest replies is dropped, never the primary or the flap-tracking primary. I chose fewest replies over least recently seen on purpose: one-off sources then churn through a single slot instead of pushing out real ECMP paths, which LRU would let them do. Each flow's responder_counts forgets the evicted address too, and the flow primary is recomputed if needed, so flow maps stay within the same bound.

What does not change:

  • Hop sent/received/timeouts, and therefore loss, are hop-level counters.
  • The primary is the max-received responder and is never evicted, so the primary, its RTT stats and route-flap tracking are unchanged.
  • The ECMP primary ratio is primary.received / hop.received. Both values are unaffected, so classification is the same as on master.

The eviction count is visible as .hops[].responders_evicted in JSON (present only when non-zero), the ttl_hop_responders_evicted_total Prometheus counter, and an "Evicted N low-count responders (keeps 64)" line in the hop details popup.

Enrichment workers take their addresses only from this map (collect_lookup_batch), so the cap also bounds what they are handed. Each tick was already limited to MAX_CONCURRENT_LOOKUPS.

2. Live probe-event log

Symptom. A live TUI session records every probe event for e export and --animate, about 30 events/s on a 30-hop path, or roughly 5 MB per hour per target. e clones all of it under the session read lock.

Already bounded on master (#155): headless runs record only for --stream-json, which drains every tick. Batch modes require -c, so their length is fixed. The TUI was the remaining unbounded case.

Fix. record_event keeps the newest Session::MAX_EVENT_LOG = 100_000 events, about an hour of a 30-hop path at the default interval and about 5 MB. When the log is full it drops the oldest quarter at once, so the shift is amortized O(1). It adds what it dropped to a new dropped_events field, which is serialized when non-zero and cleared by r. When --animate replays a truncated recording, it prints a note and starts at the first kept event instead of waiting out an hour of missing time. Final hop statistics in the export cover the whole run. Batch --json still carries a replayable events array.

Because the log is now bounded, e still clones the session (a few MB at most) rather than switching to a separate snapshot path.

3. PeeringDB response and cache size

Symptom. The three limit=0 PeeringDB dumps were read with .json(), and ~/.cache/ttl/peeringdb/ix_cache.json was read with read_to_string, both with no size bound.

Fix. Responses are streamed through a 16 MiB cap, checked against Content-Length first. The cache file is read through File::take, the same pattern as read_session_limited. The real dumps are under 1.5 MB today (ix 1.45 MB, ixlan 0.39 MB, ixpfx 0.49 MB), and the cache is about 0.37 MB. An oversized body or cache is refused with an error that names the limit. The three per-endpoint fetchers are merged into one generic helper. There is no gzip feature, so there is no decompression path to bound.

Tests

  • responder_map_stays_bounded_without_changing_hop_stats: two real per-packet paths plus 640 one-off sources and some timeouts. It checks that the map stays at 64, the eviction count is exact, the real alternate path survives, the primary and its count are unchanged, loss stays at 20%, there are no false flaps, flow maps stay bounded, classification is still PerPacket, and the JSON field is present only when non-zero.
  • live_event_log_keeps_the_newest_events_and_counts_the_rest: records 3x the cap. It checks the bound, that dropped + kept == total, that the oldest events are dropped in order, that dropped_events serializes and survives the render snapshot, and that reset clears it.
  • evicted_responders_are_reported: the hop details popup shows the eviction line only when there were evictions.
  • oversized_peeringdb_bodies_are_refused: a localhost HTTP server sends an over-cap Content-Length, an over-cap chunked body with no length, and an at-cap body that is accepted.
  • oversized_cache_file_is_refused: a valid cache padded past 16 MiB is refused.

Fails on master: the new tests reference new fields, so they do not compile there. To confirm the behavior, I disabled the eviction call and the drain on this branch. The two state tests then fail (responder map at 642 instead of 64; event log over the cap). The cache test's padded file is valid JSON that master loads whole.

Gates: cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo clippy --all-targets --no-default-features -- -D warnings and cargo test all pass. The first commit also passes cargo test --lib on its own.

Live check (docker, NET_RAW, 1.1.1.1, 8 hops)

  • --daemon --prometheus for about 75 s, branch and master side by side. Hops 2 to 8 match exactly (78 sent each, the same timeouts, 1 responder per hop). ttl_hop_responders_evicted_total is 0 on every hop, and the scrape is 319 lines against 297 on master (the new counter, one per hop). RSS was 5.8 MiB on the branch and 5.3 MiB on master. Hop 1 is the home gateway, which ICMP-rate-limits; its timeouts differed (54 vs 18) because both daemons shared its budget.
  • Hop 1 control: four alternating -c 15 --report runs per build, each run alone. Hops 2 to 8 were identical in every run. Hop 1 loss moved between 0% and 93% on both builds (master 6.7/0/53/0, branch 6.7/93/93/0), so that variation is rate limiting, not this change.
  • Truncated --animate: a real -c 3 recording, with offsets shifted by an hour and dropped_events: 75000, printed the note and played all 8 hops within seconds at --speed 1.

Not verified

  • A real many-responder hop. I have no CGNAT or spoofing hop to test against, so this path is covered only by the unit test above.
  • An hours-long TUI session reaching the event cap. The truncated-replay path was checked only with the crafted file above.
  • A live PeeringDB fetch over the new path. The cap logic is covered by the localhost test, and the current real dump sizes are listed above.

Every distinct source address that answered at a hop got a permanent
ResponderStats entry (~5 KB with its sample buffers). Per-packet balancing
behind CGNAT, or a box replying from spoofed sources, grew long daemon,
Prometheus and TUI runs without bound and slowed every render, scrape and
enrichment pass over the map.

A hop now tracks at most 64 responders. When a new one arrives at the cap,
the responder with the fewest replies is dropped (never the primary or the
flap-tracking primary), and flows forget it too. Hop sent/received/timeouts
are hop-level, so loss is unchanged, as are the primary and its RTT. The
eviction count is exported as responders_evicted (JSON),
ttl_hop_responders_evicted_total (Prometheus) and shown in hop details.
A live TUI session recorded every probe event for e export and --animate
for the whole session (about 5 MB an hour per target), and e copied all of
it under the session lock. Headless runs were already bounded: only JSON
streams record, and they drain every tick.

record_event now keeps the newest 100,000 events, dropping the oldest
quarter at once when full so the shift stays amortized O(1), and counts
what it dropped in dropped_events (serialized when non-zero, cleared by
reset). Animated replay of a truncated recording says so and starts at the
first kept event instead of idling through the missing part.
The three PeeringDB limit=0 dumps were read with .json() and the IX cache
with read_to_string, so a broken or hostile response, or a replaced
ix_cache.json, was read into memory whole. Real dumps are under 1.5 MB.

Responses now stream through a 16 MiB cap (checked against Content-Length
first) and the cache is read through File::take, like replay loading; an
oversized body or cache is refused with an error naming the limit. The
three per-endpoint fetchers collapse into one generic helper.
# Conflicts:
#	CHANGELOG.md
#	src/state/session.rs
@lance0
lance0 marked this pull request as ready for review November 14, 2026 20:38
@lance0
lance0 merged commit 0443470 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