Skip to content

Fix integer overflow in round-robin load balancing#79

Closed
dkropachev wants to merge 1 commit intomainfrom
fix/integer-overflow-round-robin
Closed

Fix integer overflow in round-robin load balancing#79
dkropachev wants to merge 1 commit intomainfrom
fix/integer-overflow-round-robin

Conversation

@dkropachev
Copy link
Collaborator

@dkropachev dkropachev commented Mar 4, 2026

Summary

  • Fix potential ArrayIndexOutOfBoundsException from AtomicInteger overflow in round-robin selection
  • Ensure safe index calculation even after billions of requests

Changes

  • Replace Math.abs() with bitwise AND operation to handle Integer.MIN_VALUE case
  • Use (counter & Integer.MAX_VALUE) to ensure non-negative index
  • Maintain round-robin distribution behavior while preventing crashes

Test plan

  • Verify compilation passes
  • Test round-robin distribution still works correctly
  • Verify no crashes when counter approaches Integer.MAX_VALUE
  • Confirm behavior with Integer.MIN_VALUE overflow case

Prevent ArrayIndexOutOfBoundsException from AtomicInteger overflow:
- Use bitwise AND with Integer.MAX_VALUE to ensure non-negative index
- Handle edge case where getAndIncrement() returns Integer.MIN_VALUE
- Maintain round-robin distribution while preventing crashes

This fixes potential crashes in long-running applications that make
billions of requests and experience counter overflow.
@dkropachev dkropachev force-pushed the fix/integer-overflow-round-robin branch from f2041e0 to f441c9c Compare March 4, 2026 14:51
@dkropachev dkropachev closed this Mar 4, 2026
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