Fix data race in quota group routing by cloning auth in selector - #20
Conversation
Co-authored-by: nghyane <59473462+nghyane@users.noreply.github.com>
Co-authored-by: nghyane <59473462+nghyane@users.noreply.github.com>
|
@copilot về cơ chế routing thì sao đã kiểm tra chưa? |
Đã kiểm tra đầy đủ cơ chế routing, bao gồm: 1. Provider Selection (selectProviders)
2. Auth Selection (pickNext → selector.Pick)
3. Retry & Failover
4. Quota Group Propagation
Data race đã fix (commit 56e8078):
Tất cả tests pass với |
There was a problem hiding this comment.
Pull request overview
This PR fixes a data race in quota group routing where concurrent access to auth.Runtime occurred between MarkResult operations (which modify quota state) and selector operations (which read quota state). The fix ensures thread-safety by cloning auth objects before passing them to selectors.
Key changes:
- Clone auth objects in manager's pickNext method to provide selectors with immutable snapshots
- Add comprehensive race detection tests covering concurrent quota operations
- Document locking requirements for quota group index operations
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/provider/manager.go | Clones auth objects under RLock before passing to selector, preventing race on Runtime pointer |
| internal/provider/quota_group.go | Documents that getOrCreateQuotaGroupIndex requires Manager lock protection |
| internal/provider/quota_group_race_test.go | Adds 4 comprehensive race tests validating Manager lock protection, concurrent quota propagation, quota group consistency, and end-to-end selector workflow |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot Phân tích so sánh với các best practice roubin trong go, các repo api tương tự |
* Initial plan * Add race detection test exposing quota group index data race Co-authored-by: nghyane <59473462+nghyane@users.noreply.github.com> * Fix race condition in quota group routing by cloning auth in pickNext Co-authored-by: nghyane <59473462+nghyane@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nghyane <59473462+nghyane@users.noreply.github.com>
Race detector revealed concurrent access to
auth.Runtimeduring quota group routing. Manager passed raw auth pointers to selector while MarkResult concurrently modified quota state.Changes
manager.go:671 - Clone auth objects before passing to selector
quota_group.go - Document that
getOrCreateQuotaGroupIndexrequires Manager lock (no code change)quota_group_race_test.go - Add race detection tests simulating production patterns:
TestQuotaGroupIndexManagerProtection- Verifies proper RLock/Lock usageTestConcurrentQuotaPropagation- Tests parallel quota updatesTestQuotaGroupIndexConsistency- Validates quota index synchronizationTestSelectorWithQuotaGroups- End-to-end concurrent workflowImpact
Minimal performance overhead (shallow clone + map copies). Eliminates potential crashes and incorrect routing decisions under concurrent load.
All tests pass with
-raceflag enabled.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.