Skip to content

weighted_round_robin does not validate total_weights #7806

Closed

Description

@vijayvenkatj

Issue Details

Issue Details

I noticed the weighted_round_robin in selectionpolicies allows for 0 as a valid weight. This might be intentional for testing/other purposes .But,

  1. When the total weight is 0, currentWeight := int(r.index.Add(1)) % r.totalWeight does integer division by 0.
  2. 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.
  3. currentWeight := int(r.index.Add(1)) % r.totalWeight selection cycle starts at slot 1 instead of slot 0.

Reproduction

  1. HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR instead of 503
localhost:8080 {
	reverse_proxy localhost:8081 localhost:8082  {
		lb_policy weighted_round_robin 0 0
	}
}
  1. Always selects the first upstream
localhost:8080 {
	reverse_proxy localhost:8081 localhost:8082  {
		lb_policy weighted_round_robin 0
	}
}

Proposed Fix:

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.

No response

Activity

  1. steadytao commented on Jun 7, 2026

    @steadytao
    Member

    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.

  2. added
    bug 🐞
    good first issue 🐤
    on Jun 7, 2026
  3. steadytao commented on Jun 7, 2026

    @steadytao
    Member

    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.

  4. vijayvenkatj commented on Jun 7, 2026

    @vijayvenkatj
    ContributorAuthor

    Sure, I'll work on this.

  5. vijayvenkatj commented on Jun 7, 2026

    @vijayvenkatj
    ContributorAuthor

    @steadytao So, Should the validation be done on the config adaption or Should there be a Validate func on the module ?

  6. steadytao commented on Jun 7, 2026

    @steadytao
    Member

    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.

  7. vijayvenkatj commented on Jun 10, 2026

    @vijayvenkatj
    ContributorAuthor

    @steadytao I believe this issue was fixed by #7807 . Is there anything else to work on for this issue?

  8. steadytao commented on Jun 10, 2026

    @steadytao
    Member

    @steadytao I believe this issue was fixed by #7807 . Is there anything else to work on for this issue?

    No, it was fixed. GitHub just didn't link it properly, thank you again for your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions