Skip to content

fix: PMTUD correlation, lock ordering, and enrichment dedup - #104

Merged
lance0 merged 4 commits into
masterfrom
fix/concurrency-correctness
Jul 1, 2026
Merged

lance0 merged 4 commits into
masterfrom
fix/concurrency-correctness

Conversation

@lance0

@lance0 lance0 commented Jul 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes PMTUD probe correlation correctness, documents lock ordering, and deduplicates enrichment lookup targets.

Changes

#3 (high): PMTUD Echo Reply correlation via on-wire payload marker

PMTUD probes and normal probes share the same ICMP identifier and ProbeId sequence space. When both a normal and PMTUD pending entry existed for the same (ProbeId, target), the receiver couldn't distinguish which type of probe an Echo Reply was for. It preferred the normal entry, causing the PMTUD probe to time out and record_failure() to be called — leading to MTU under-estimation.

Fix: Write a PMTUD marker byte (0x50) at payload byte 8 of ICMP Echo Requests. Echo Replies echo the payload verbatim, so the receiver detects PMTUD success via parsed.is_pmtud. For Frag Needed / Packet Too Big responses (which quote the original packet, possibly truncated to 8 bytes per RFC 792), the response type is used as a fallback discriminator: is_pmtud_response = parsed.is_pmtud || matches!(response_type, DestUnreachable(4) | PacketTooBig).

  • build_echo_request gains a pmtud: bool parameter; when true, payload byte 8 is set to PMTUD_MARKER (0x50) instead of the normal pattern fill (0x00)
  • ParsedResponse gains an is_pmtud: bool field, set by checking the marker in Echo Reply payload via is_pmtud_payload()
  • remove_pending_by_flow_hint routes to the PMTUD pending entry when is_pmtud_response is true

#10 (medium): Lock ordering documentation

Explicitly scopes the sessions.read() guard in a nested block so it is dropped before pending.write() is acquired in the receiver drain loop. Documents the lock order (sessions before pending, never nested) to prevent future deadlock regressions.

#12 (medium): Enrichment IP deduplication

Deduplicates IPs via HashSet before take(MAX_CONCURRENT_LOOKUPS) in all four enrichment workers (ASN, rDNS, GeoIP, IX). Previously, duplicate responder IPs across targets could occupy multiple concurrent lookup slots, wasting resources on redundant lookups.

Testing

  • 511 tests pass (18 new); cargo clippy --all-targets -- -D warnings clean.
  • New tests include:
    • test_pmtud_marker_set_in_payload / test_normal_probe_has_no_pmtud_marker — builder tests
    • test_parse_pmtud_echo_reply_v4_dgram / test_parse_normal_echo_reply_v4_dgram / test_parse_pmtud_echo_reply_v4_raw — end-to-end parse tests that build a real Echo Request via build_echo_request, convert to Echo Reply, and verify parsed.is_pmtud
    • test_remove_pending_unknown_flow_pmtud_echo_reply_prefers_pmtud_entry / test_remove_pending_unknown_flow_pmtud_echo_reply_no_flow_hint — pending removal routing tests
    • test_remove_pending_explicit_flow_pmtud_response_prefers_pmtud — existing test updated for new signature

Risk

Medium — the PMTUD correlation change touches the receiver's probe-matching logic and adds a new on-wire byte (payload[8]). Normal probes set this to 0x00 (pattern fill), so there's no wire-protocol conflict. The marker is only checked for Echo Replies, not error responses (which may truncate the quoted payload).

lance0 added 2 commits July 1, 2026 15:25
- Pass is_pmtud_response to remove_pending_by_flow_hint so Frag Needed /
  Packet Too Big responses prefer the PMTUD pending entry over normal,
  preventing PMTUD probe from timing out and converging to wrong MTU (#3)
- Explicitly scope sessions read guard before acquiring pending write lock
  in receiver drain loop, documenting the lock order to prevent deadlock (#10)
- Deduplicate IPs via HashSet before take(MAX_CONCURRENT) in all four
  enrichment workers (ASN, rDNS, GeoIP, IX) to avoid redundant lookups (#12)

All 493 tests pass; clippy clean.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves traceroute receiver correctness and background enrichment efficiency by (1) preferring PMTUD pending probes when handling PMTUD-related ICMP responses, (2) documenting and attempting to enforce a safer lock acquisition order in the receiver drain loop, and (3) deduplicating enrichment lookup targets so concurrent lookup capacity isn’t wasted on repeat IPs.

Changes:

  • Adjust pending-probe correlation to prefer PMTUD entries for Frag Needed / Packet Too Big responses, including an added unit test.
  • Refactor the receiver drain loop to drop sessions.read() before taking pending.write(), and document intended lock ordering.
  • Deduplicate enrichment worker IP batches before applying the concurrency cap.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/trace/receiver.rs Updates probe matching to prefer PMTUD pending entries and adds a PMTUD preference test; adds lock-order documentation and scopes sessions.read() in the drain loop.
src/lookup/asn.rs Deduplicates IPs prior to take(MAX_CONCURRENT_LOOKUPS) for ASN enrichment batch selection.
src/lookup/rdns.rs Deduplicates IPs prior to take(MAX_CONCURRENT_LOOKUPS) for reverse DNS enrichment batch selection.
src/lookup/geo.rs Deduplicates IPs prior to take(MAX_CONCURRENT_LOOKUPS) for GeoIP enrichment batch selection.
src/lookup/ix.rs Deduplicates IPs prior to take(MAX_CONCURRENT_LOOKUPS) for IX enrichment batch selection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/trace/receiver.rs Outdated
Comment on lines +220 to +226
// Collect target list BEFORE acquiring the pending lock.
// Lock order: sessions.read() is always acquired and released
// before pending.write() to prevent deadlock.
let fallback_targets: Vec<IpAddr> = {
let sessions = self.sessions.read();
sessions.keys().copied().collect()
};
Comment thread src/lookup/rdns.rs Outdated
Comment on lines 131 to 136
let batch: Vec<IpAddr> = ips_to_lookup
.into_iter()
.collect::<std::collections::HashSet<_>>()
.into_iter()
.take(MAX_CONCURRENT_LOOKUPS)
.collect();
Comment thread src/lookup/asn.rs Outdated
Comment on lines 245 to 251
// Perform parallel ASN lookups (limited batch size)
let batch: Vec<IpAddr> = ips_to_lookup
.into_iter()
.collect::<std::collections::HashSet<_>>()
.into_iter()
.take(MAX_CONCURRENT_LOOKUPS)
.collect();
Comment thread src/lookup/geo.rs Outdated
Comment on lines 167 to 173
// GeoIP lookups are fast (local file), so we can do more at once
let batch: Vec<IpAddr> = ips_to_lookup
.into_iter()
.collect::<std::collections::HashSet<_>>()
.into_iter()
.take(MAX_CONCURRENT_LOOKUPS)
.collect();
Comment thread src/lookup/ix.rs Outdated
Comment on lines 672 to 678
// Perform parallel IX lookups (limited batch size)
let batch: Vec<IpAddr> = ips_to_lookup
.into_iter()
.collect::<std::collections::HashSet<_>>()
.into_iter()
.take(MAX_CONCURRENT_LOOKUPS)
.collect();

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread src/trace/receiver.rs
Comment on lines +532 to +535
fn flush_pending_as_timeouts(&self) {
let timed_out: Vec<_> = self.pending.write().drain().collect();
self.record_pending_timeouts(timed_out, false, false);
}
PMTUD probes and normal probes share the same ICMP identifier and
ProbeId sequence space, so the receiver couldn't tell an Echo Reply
for a PMTUD probe from one for a normal probe. When both a normal and
PMTUD pending entry existed for the same (ProbeId, target), the
receiver consumed the normal entry, causing the PMTUD probe to time
out and the normal hop to receive spurious data.

Fix: write a PMTUD marker (0x50) at payload byte 8 of ICMP Echo
Requests. Echo Replies echo the payload verbatim, so the receiver
can detect PMTUD success via parsed.is_pmtud. For Frag Needed / Packet
Too Big (which quote the original packet, possibly truncated), the
response type is still used as a fallback discriminator.

- Add pmtud: bool parameter to build_echo_request
- Add PMTUD_MARKER constant and is_pmtud field to ParsedResponse
- Add is_pmtud_payload() helper to check marker in Echo Reply payload
- Combine parsed.is_pmtud || response_type check for is_pmtud_response
- Add 8 new tests: marker set/absent, min payload, Echo Reply routing,
  and end-to-end parse tests for DGRAM + RAW paths
@lance0
lance0 merged commit 1c5cefe into master Jul 1, 2026
5 checks passed
@lance0
lance0 deleted the fix/concurrency-correctness branch July 1, 2026 20:23
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.

2 participants