fix(pool): prevent panic on Instant subtraction underflow - #1457
fix(pool): prevent panic on Instant subtraction underflow#1457SunnyYYLin wants to merge 1 commit into
Conversation
Replace Instant::now() - ttl with saturating_duration_since to avoid overflow panic when the monotonic clock age is smaller than the TTL (e.g. fresh boot with 48h session_ttl_hours). Add regression test for the edge case.
c0bd992 to
2690bd3
Compare
|
LGTM ✅ - The change removes the idle-cleanup panic without changing TTL semantics. Note No blocking correctness, safety, or integration issues were found in the exact-SHA review. What This PR DoesIt fixes a panic in idle-session cleanup when the configured TTL is longer than the elapsed monotonic-clock time. The implementation replaces panic-prone How It Works
Findings
Finding Details🟢 F1: Safe elapsed-time classificationThe new calculation is equivalent to the old cutoff comparison whenever Baseline Check
Addressing External Reviewer FeedbackNo external review comments or threads were present for this round. Independent Review Summary
Validation
5. Three Reasons We Might Not Need This PR
These are decision considerations, not blocking findings; the current patch is minimal and correct. |
There was a problem hiding this comment.
LGTM ✅ - No blocking findings; F1 is praise only.
Consolidated review: #1457 (comment)
What problem does this solve?
Instant::now() - ttlpanics when the monotonic clock has not yet elapsedttlworth of time. This is a logic error in the original code, not a platform-specific bug.Closes #1459
Discord Discussion URL: https://discordapp.com/channels/1491295327620169908/1530676804996825088
Review Contract
Goal
Fix the panic caused by
Instant::now() - ttlwhen the elapsed time since the monotonic clock epoch is less thanttl(e.g.session_ttl_hours=48on a machine with < 48h uptime).Non-goals
No behavioral change. No change to TTL semantics.
Accepted Residual Risks
None -
saturating_duration_sincereturnsDuration::ZEROon underflow, which correctly classifies the session as fresh (not idle).Acceptance Criteria
cargo test -p openab-core classify_idle --libpasses (3 tests)cargo clippy --workspace --features unified -- -D warningscleannow > last_active + ttl(normal case)Follow-ups
No deferred work - the fix is complete and self-contained.
Why was this never reported?
Instant - Durationin Rust callschecked_sub().expect()- it panics on underflow. The original code computesInstant::now() - ttlto get a cutoff point, then compareslast_active < cutoff.This panics whenever the monotonic clock has not elapsed
ttlsince its epoch. On all platforms the monotonic clock starts at boot, so the condition is uptime < TTL. With the defaultsession_ttl_hours=48, this means any machine that has been up for less than 48 hours will panic on the first reaper tick.It went unreported because:
The bug is platform-agnostic. A freshly booted Linux machine with
session_ttl_hours=48would panic too.Proposed Solution
Replace
let cutoff = Instant::now() - ttl; last_active < cutoffwithnow.saturating_duration_since(last_active) > ttl.last_activeis always <=now, sosaturating_duration_sincenever underflows.At a Glance
Prior Art & Industry Research
Not applicable - trivial arithmetic safety fix.
Alternatives Considered
checked_sub+ unwrap_or: more verbose, same effect.Instant::now().checked_duration_since(last_active): returns Option, requires extra branching.Validation
cargo test -p openab-core classify_idle --lib- 3 passedcargo clippy --workspace --features unified -- -D warnings- clean