Skip to content

fix(p2p): prevent underflow panic in gossip rate limiter timestamp math#1445

Merged
JesseTheRobot merged 1 commit into
release/3.xfrom
hotfix/p2p-rate-limiter-underflow
Jun 10, 2026
Merged

fix(p2p): prevent underflow panic in gossip rate limiter timestamp math#1445
JesseTheRobot merged 1 commit into
release/3.xfrom
hotfix/p2p-rate-limiter-underflow

Conversation

@JesseTheRobot

@JesseTheRobot JesseTheRobot commented Jun 10, 2026

Copy link
Copy Markdown
Member

What happened

mainnet-node-1 (genesis) panicked on 2026-06-10 11:34:30 UTC (running release/mainnet/3.0.2 @ e30f336af) and was restarted by pm2:

Message:  attempt to subtract with overflow
Location: crates/p2p/src/rate_limiting.rs:232

Reached from inbound gossip traffic: POST /gossip/pull_datahandle_get_data_syncDataRequestTracker::check_requestcleanup_expired_entries.

Root cause

The tracker stamps per-peer timestamps (window_start, last_request, last_cleanup) from the non-monotonic wall clock (SystemTime::now() as epoch millis) and computes elapsed time by unsigned subtraction (now - stored). Because the workspace sets overflow-checks = true in [profile.release], an underflow is a hard panic in every shipped profile.

A stored timestamp can exceed the captured now via either mechanism:

  1. Concurrent insert during cleanup iteration (most likely trigger): cleanup_expired_entries captures now, then iterates the DashMap (not a snapshot). A concurrent check_request inserts a fresh record stamped a moment after the captured now; the iterator visits it and now - window_start underflows. Rare per-event, inevitable over time on a busy node.
  2. Backward wall-clock movement (NTP correction, VM clock sync).

Fix

saturating_sub at all four subtraction sites (is_window_expired, is_duplicate_request, cleanup_expired_entries, cleanup_if_needed). An out-of-order timestamp now reads as elapsed 0 — "just happened / not yet aged / not expired" — which is the safe, intended behavior at each call site. No rate-limiting thresholds, windows, or signatures change.

Regression tests were written first and confirmed to panic with attempt to subtract with overflow at the exact production lines on the unfixed code; they pass after the fix.

Note: this is a surgical hotfix — the proper fix is PR #1402

#1402 (fix: clock drift/monotonic timers) is the proper fix: it rewrites this tracker to use the monotonic std::time::Instant (whose arithmetic saturates by design), eliminating the wall-clock dependency entirely. That PR is open against master and should be revived and landed there. This PR is the minimal, low-risk hotfix appropriate for the release line so deployed nodes stop panicking now; #1402 supersedes it on master when it lands.

Per the shared-code hotfix process, this fix originates on release/3.x and must be cherry-picked up to master after landing (or arrive there via #1402's rewrite).

Verification

  • cargo test -p irys-p2p --lib rate_limiting — 9/9 pass on this branch (new tests red before the fix)
  • cargo build -p irys-p2p --profile debug-release — the overflow-checks profile builds clean
  • cargo fmt / cargo clippy -p irys-p2p --tests --all-targets — clean

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved rate limiting stability to safely handle edge cases when system timestamps are unusual or adjusted.
  • Tests

    • Added test coverage for rate limiting behavior under backward clock and future timestamp scenarios.

The DataRequestTracker stamps per-peer timestamps from the non-monotonic
wall clock (SystemTime) and computed elapsed times by unsigned
subtraction. When a stored timestamp lands after the captured "now" —
either a concurrent insert during DashMap iteration in cleanup, or a
backward wall-clock step — the subtraction underflows. With
overflow-checks = true in the release profile this is a hard panic,
observed killing mainnet-node-1 on 2026-06-10 via POST /gossip/pull_data.

Use saturating_sub at all four subtraction sites so an out-of-order
timestamp reads as elapsed 0 ("just happened"), which is the safe and
intended behavior at each call site. Adds regression tests that panic
on the unfixed code.
@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f1d5b59d-e577-4640-8c92-944711e28de4

📥 Commits

Reviewing files that changed from the base of the PR and between 137af79 and 0c6f958.

📒 Files selected for processing (1)
  • crates/p2p/src/rate_limiting.rs

📝 Walkthrough

Walkthrough

Rate-limiting code in crates/p2p/src/rate_limiting.rs now defends against system clock skew by using saturating_sub in all timestamp comparisons. Window-expiry, deduplication, cleanup age, and cleanup scheduling checks replace direct subtraction to safely handle future timestamps and prevent panics. New tests confirm correctness under backward-clock scenarios.

Changes

Clock-skew defensive timing

Layer / File(s) Summary
Saturating subtraction in rate-limiting checks
crates/p2p/src/rate_limiting.rs
DataRequestRecord methods is_window_expired and is_duplicate_request now use saturating_sub for timestamp comparisons; cleanup_expired_entries uses saturating_sub for age calculation; cleanup_if_needed uses saturating_sub for elapsed time since last cleanup—all preventing underflow when timestamps are ahead of the current wall clock.
Future-timestamp scenario tests
crates/p2p/src/rate_limiting.rs
New unit tests verify cleanup does not evict entries with future window_start, record expiry and deduplication checks do not panic on future timestamps, and cleanup-if-needed does not panic when last_cleanup is ahead of current time.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: preventing an underflow panic in the gossip rate limiter's timestamp math, which matches the core fix of replacing subtraction with saturating_sub.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch hotfix/p2p-rate-limiter-underflow

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown
Contributor

@JesseTheRobot JesseTheRobot merged commit 7a715ce into release/3.x Jun 10, 2026
21 checks passed
@JesseTheRobot JesseTheRobot deleted the hotfix/p2p-rate-limiter-underflow branch June 10, 2026 18:22
JesseTheRobot added a commit that referenced this pull request Jun 11, 2026
…th (#1445)

The DataRequestTracker stamps per-peer timestamps from the non-monotonic
wall clock (SystemTime) and computed elapsed times by unsigned
subtraction. When a stored timestamp lands after the captured "now" —
either a concurrent insert during DashMap iteration in cleanup, or a
backward wall-clock step — the subtraction underflows. With
overflow-checks = true in the release profile this is a hard panic,
observed killing mainnet-node-1 on 2026-06-10 via POST /gossip/pull_data.

Use saturating_sub at all four subtraction sites so an out-of-order
timestamp reads as elapsed 0 ("just happened"), which is the safe and
intended behavior at each call site. Adds regression tests that panic
on the unfixed code.
@coderabbitai coderabbitai Bot mentioned this pull request Jun 15, 2026
3 tasks
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