You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
weighted_round_robin does not validate total_weights #7806
I noticed the weighted_round_robin in selectionpolicies allows for 0 as a valid weight. This might be intentional for testing/other purposes .But,
When the total weight is 0, currentWeight := int(r.index.Add(1)) % r.totalWeight does integer division by 0.
When there is only 1 weight and it is set to 0, if len(r.Weights) < 2 {return pool[0]} causes it to always return the first upstream which should not be the case.
currentWeight := int(r.index.Add(1)) % r.totalWeight selection cycle starts at slot 1 instead of slot 0.
Reproduction
HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR instead of 503
For 1 and 2, adding a Validation for total weight would solve it.
For the 3rd one: currentWeight := int(r.index.Add(1)-1) % r.totalWeight would make it start from the first slot
Assistance Disclosure
AI not used
If AI was used, describe the extent to which it was used.
This looks legitimate. 0 weights have seemingly been intentionally allowed since #6681 so an upstream can be skipped but the config still needs at least one positive weight. The minimal fix should be to reject weighted_round_robin configs whose total weight is 0, with tests for 0, 0 0 and a mixed case like 0 2 1 continuing to work.
I would leave the counter offset out of this fix. Existing tests intentionally cover the current rotation offset and the long-term weighted distribution is still correct.
Should be simple enough to be good for new contributors. @vijayvenkatj feel free to attempt a patch if you would like; otherwise, if it sits stale for a week or two, I will pick it up.
Should the validation be done on the config adaption or Should there be a Validate func on the module ?
I would prefer a Validate() method on WeightedRoundRobinSelection, not only Caddyfile adaptation because the policy can be configured directly through JSON too. Provision() can keep computing totalWeight then Validate() should reject totalWeight == 0; keep the Caddyfile parsers per-weight non-negative check, add the caddy.Validator interface guard and cover 0, 0 0 and valid 0 2 1 cases in tests.
Activity
steadytao commentedon Jun 7, 2026
This looks legitimate.
0weights have seemingly been intentionally allowed since #6681 so an upstream can be skipped but the config still needs at least one positive weight. The minimal fix should be to rejectweighted_round_robinconfigs whose total weight is0, with tests for0,0 0and a mixed case like0 2 1continuing to work.I would leave the counter offset out of this fix. Existing tests intentionally cover the current rotation offset and the long-term weighted distribution is still correct.
steadytao commentedon Jun 7, 2026
Should be simple enough to be good for new contributors. @vijayvenkatj feel free to attempt a patch if you would like; otherwise, if it sits stale for a week or two, I will pick it up.
vijayvenkatj commentedon Jun 7, 2026
Sure, I'll work on this.
vijayvenkatj commentedon Jun 7, 2026
@steadytao So, Should the validation be done on the config adaption or Should there be a Validate func on the module ?
steadytao commentedon Jun 7, 2026
I would prefer a
Validate()method onWeightedRoundRobinSelection, not only Caddyfile adaptation because the policy can be configured directly through JSON too.Provision()can keep computingtotalWeightthenValidate()should rejecttotalWeight == 0; keep the Caddyfile parsers per-weight non-negative check, add thecaddy.Validatorinterface guard and cover0,0 0and valid0 2 1cases in tests.vijayvenkatj commentedon Jun 10, 2026
@steadytao I believe this issue was fixed by #7807 . Is there anything else to work on for this issue?
steadytao commentedon Jun 10, 2026
No, it was fixed. GitHub just didn't link it properly, thank you again for your PR.