harshita2626 commented on issue #3173:
URL: https://github.com/apache/hertzbeat/issues/3173#issuecomment-2757859449
This appears to be a bug in the HashedWheelTimer implementation where
canceled timeouts can cause incorrect pendingTimeouts counts, potentially
leading to negative values.
Root Cause
1.Multiple timeouts are scheduled for the same tick
2.These timeouts get transferred to their target bucket
3.They are canceled before the wheel actually processes that bucket
4.The pendingTimeouts counter is decremented for each cancellation, but
wasn't properly accounted for in this state
Impact
1.Incorrect Metrics: pendingTimeouts becomes negative, failing to accurately
reflect the actual state
2.Resource Control Bypass: If maxPendingTimeouts is set, this bug could
allow exceeding the intended limit
Suggested Fixes
Option 1: Modify the cancellation logic
```
public void cancel() {
// Only decrement if the timeout hasn't been transferred to a bucket yet
if (bucket == null) {
timer.pendingTimeouts.decrementAndGet();
}
```
// Rest of cancellation logic...
}
Option 2: Adjust bucket transfer accounting
```
// When transferring timeouts to buckets:
void transferTimeoutsToBuckets() {
for (int i = 0; i < 100000; i++) {
Timeout timeout = timeouts.poll();
if (timeout == null) {
break;
}
if (timeout.state() != CANCELLED) {
// Only count non-canceled timeouts
long calculated = timeout.deadline / tickDuration;
timeout.remainingRounds = (calculated - tick) / wheel.length;
final long ticks = Math.max(calculated, tick);
int stopIndex = (int) (ticks & mask);
Bucket bucket = wheel[stopIndex];
bucket.addTimeout(timeout);
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]