-
Notifications
You must be signed in to change notification settings - Fork 51
Fix router panic when all target model weights are zero #727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: WHOIM1205 <[email protected]>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello @WHOIM1205, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical vulnerability in the router's weighted model selection mechanism. Previously, if all configured target model weights were zero, the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
/assign @hzxuzhonghu |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively addresses a critical panic caused by a divide-by-zero error in selectFromWeightedSlice when all model weights are zero. The fix is minimal and correct, and the new unit tests provide good coverage for the edge cases.
I've identified a related latent issue where handling an empty slice of weights could lead to a panic in the calling function. I've provided two high-severity comments with suggestions to make the implementation more robust by explicitly handling this case and updating the corresponding test.
FAUST-BENCHOU
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway we should wait for maintainer's advice
| // Guard against divide-by-zero when all weights are zero | ||
| if totalWeight == 0 { | ||
| return 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check 0 idea is reasonable.But i dont think we should return zero here(It will cover up configuration errors and directly select the first target.)
for example:
rules:
- name: my-rule
targetModels:
- modelServerName: server-a
weight: 0
- modelServerName: server-b
weight: 0
kthena/pkg/kthena-router/datastore/store.go
Lines 941 to 950 in a8c9193
| func (s *store) selectDestination(targets []*aiv1alpha1.TargetModel) (*aiv1alpha1.TargetModel, error) { | |
| weightedSlice, err := toWeightedSlice(targets) | |
| if err != nil { | |
| return nil, err | |
| } | |
| index := selectFromWeightedSlice(weightedSlice) | |
| return targets[index], nil | |
| } |
selectDestination will select targets[0] (server-a)
Maybe we can add check in toWeightedSlice to throw an error
kthena/pkg/kthena-router/datastore/store.go
Lines 952 to 974 in a8c9193
| func toWeightedSlice(targets []*aiv1alpha1.TargetModel) ([]uint32, error) { | |
| var isWeighted bool | |
| if targets[0].Weight != nil { | |
| isWeighted = true | |
| } | |
| res := make([]uint32, len(targets)) | |
| for i, target := range targets { | |
| if (isWeighted && target.Weight == nil) || (!isWeighted && target.Weight != nil) { | |
| return nil, fmt.Errorf("the weight field in targetModel must be either fully specified or not specified") | |
| } | |
| if isWeighted { | |
| res[i] = *target.Weight | |
| } else { | |
| // If weight is not specified, set to 1. | |
| res[i] = 1 | |
| } | |
| } | |
| return res, nil | |
| } |
if isWeighted && totalWeight == 0 {
return nil, fmt.Errorf("the sum of weights in targetModels must be greater than 0")
}
to inform users that their coniguration is not right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the suggestion
for this pr i’m intentionally keeping the fix scoped to preventing the runtime panic on the request path returning a deterministic fallback here avoids crashing the router for edgecase or transitional configurations
validating or rejecting misconfiguration earlier (e.g.,, at config or controller level) makes sense but i’d prefer to keep this change defensive and minimal rather than introducing a new panic in the routing path
Summary
This PR fixes a critical router panic caused by a divide-by-zero error when selecting a target model from a weighted list where all weights are set to zero
A minimal defensive guard has been added to prevent
rand.Intn(0)from panicking, along with focused unit tests to cover the affected edge cases.Issue Description
When a
ModelRouteis configured withTargetModelentries whose weights all evaluate to0, the router crashes at runtime.This happens because
selectFromWeightedSlicecomputes a total weight of0and passes it directly torand.Intn, which panics immediately.Such configurations are valid and realistic during canary rollbacks, maintenance windows, or temporary target disablement.
Since this code runs on the request hot path, the panic causes a full router crash and impacts all traffic.
What Was Fixed
selectFromWeightedSliceto safely handle the case where the total weight is zeroThe fix is intentionally minimal and localized.
Impact of the Fix
Code Changes
Files modified:
pkg/kthena-router/datastore/store.goselectFromWeightedSliceto handletotalWeight == 0pkg/kthena-router/datastore/store_test.goTest Verification
Unit tests were added using Go’s table-driven testing pattern, covering: