reverse-proxy icon indicating copy to clipboard operation
reverse-proxy copied to clipboard

LoadBalancingMode.RoundRobin - Possible: System.IndexOutOfRangeException

Open Rickinio opened this issue 6 years ago • 3 comments

Describe the bug

In LoadBalancer.cs file there is the following code:

https://github.com/microsoft/reverse-proxy/blob/20d50978e3aea16b0e5e56f9fd1bdb4f57a28c03/src/ReverseProxy/Service/Proxy/LoadBalancer.cs#L43-L45

but when the RoundRobinState int state get too big and start getting negative values then the remainder from offset % endpoints.Count will be negative number and you'll get System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

To Reproduce

https://github.com/microsoft/reverse-proxy/blob/20d50978e3aea16b0e5e56f9fd1bdb4f57a28c03/src/ReverseProxy/Service/RuntimeModel/BackendConfig.cs#L78-L90

Inside the constructor of BackendLoadBalancingOptions initialize the AtomicCounter with the Value 2147483647. Then you will get the System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

Rickinio avatar May 22 '20 20:05 Rickinio

Yeah, that's a bug. I thought I'd checked that...

Tratcher avatar May 22 '20 21:05 Tratcher

I can take a look at this. One option is to mask offset before computing the slot (see 2816dd1).

The other option is to add an AtomicUnsignedCounter and use that for RoundRobinState. A bit larger change, but it would eliminate the need for the additional mask per request.

rwkarg avatar May 22 '20 22:05 rwkarg

@rwkarg The masking looks plausible. Include a comment why it's being done.

There's potential for a jump in the round robin rotation as you cross over the boundary, right? But at least with this option it won't start counting backwards like using an absolute value would.

Tratcher avatar May 22 '20 22:05 Tratcher