Commit e8a2605
authored
fix: prevent panic in WeightedRandomKeySelector when all keys have zero weight (#1943)
## Summary
Fixes a panic in `WeightedRandomKeySelector` that occurs when all keys have zero weight by adding a fallback to uniform random selection.
## Changes
- Added zero weight check in `WeightedRandomKeySelector` before attempting weighted selection
- When `totalWeight` equals zero, the function now falls back to uniform random selection using `rand.Intn(len(keys))`
- This prevents division by zero or invalid range errors that would cause panics
## Type of change
- [x] Bug fix
- [ ] Feature
- [ ] Refactor
- [ ] Documentation
- [ ] Chore/CI
## Affected areas
- [x] Core (Go)
- [x] Transports (HTTP)
- [ ] Providers/Integrations
- [ ] Plugins
- [ ] UI (Next.js)
- [ ] Docs
## How to test
Test the fix by creating a scenario where all keys have zero weight and verify the selector doesn't panic:
```sh
# Core/Transports
go version
go test ./...
# Test specific scenario with zero weights
go test -run TestWeightedRandomKeySelector -v
```
Create test cases with keys that all have `Weight: 0.0` to ensure uniform random selection occurs without panics.
## Screenshots/Recordings
N/A
## Breaking changes
- [ ] Yes
- [x] No
## Related issues
N/A
## Security considerations
No security implications - this is a defensive programming fix that improves system stability.
## Checklist
- [ ] I read `docs/contributing/README.md` and followed the guidelines
- [ ] I added/updated tests where appropriate
- [ ] I updated documentation where needed
- [ ] I verified builds succeed (Go and UI)
- [ ] I verified the CI pipeline passes locally if applicable1 parent ff457b1 commit e8a2605
3 files changed
+7
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5945 | 5945 | | |
5946 | 5946 | | |
5947 | 5947 | | |
| 5948 | + | |
| 5949 | + | |
| 5950 | + | |
| 5951 | + | |
| 5952 | + | |
5948 | 5953 | | |
5949 | 5954 | | |
5950 | 5955 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
0 commit comments