You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "Aias00 (via GitHub)" <gi...@apache.org> on 2026/06/17 06:35:09 UTC
[PR] fix(loadbalance): fix data race in RoundRobin setWeight and increaseCurrent [dubbo-go]
Aias00 opened a new pull request, #3435:
URL: https://github.com/apache/dubbo-go/pull/3435
## Problem
`weightedRoundRobin` 的 `weight` 和 `current` 字段存在数据竞争:同一个字段混用了原子和非原子操作。
### 具体问题
| 方法 | 字段 | 操作 | 问题 |
|------|------|------|------|
| `setWeight()` | `weight` | `robin.weight = weight` | 非原子写,与 `Weight()` 的 `atomic.LoadInt64` 竞争 |
| `setWeight()` | `current` | `robin.current = 0` | 非原子写,与 `increaseCurrent()`/`Current()` 的 `atomic.AddInt64` 竞争 |
| `increaseCurrent()` | `weight` | `robin.weight` | 非原子读,与 `setWeight()` 的写操作竞争 |
## Fix
将所有非原子访问替换为对应的原子操作:
- `setWeight()`: `robin.weight = weight` → `atomic.StoreInt64(&robin.weight, weight)`
- `setWeight()`: `robin.current = 0` → `atomic.StoreInt64(&robin.current, 0)`
- `increaseCurrent()`: `robin.weight` → `atomic.LoadInt64(&robin.weight)`
## Verification
`go test ./cluster/loadbalance/roundrobin/ -race -count=5` 通过,无数据竞争报告。
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] fix(loadbalance): fix data race in RoundRobin setWeight and increaseCurrent [dubbo-go]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3435:
URL: https://github.com/apache/dubbo-go/pull/3435#discussion_r3426063157
##########
cluster/loadbalance/roundrobin/loadbalance.go:
##########
@@ -144,8 +144,8 @@ func (robin *weightedRoundRobin) Weight() int64 {
}
func (robin *weightedRoundRobin) setWeight(weight int64) {
- robin.weight = weight
- robin.current = 0
+ atomic.StoreInt64(&robin.weight, weight)
+ atomic.StoreInt64(&robin.current, 0)
Review Comment:
`setWeight` updates `weight` and resets `current` via two independent atomic stores. If `increaseCurrent()` runs between these two stores, it can (a) increment using the old/new weight unexpectedly and/or (b) have its increment wiped out by the subsequent `current` reset. If the intent is that weight updates are applied atomically with the current reset, consider guarding `setWeight` and selection/increment operations with a lock (or otherwise ensuring these mutations happen under a single critical section), or redesigning state so the update can be applied as one atomic unit.
##########
cluster/loadbalance/roundrobin/loadbalance.go:
##########
@@ -157,7 +157,7 @@ func (robin *weightedRoundRobin) setLastUpdate(time *time.Time) {
}
func (robin *weightedRoundRobin) increaseCurrent() int64 {
- return atomic.AddInt64(&robin.current, robin.weight)
+ return atomic.AddInt64(&robin.current, atomic.LoadInt64(&robin.weight))
Review Comment:
`setWeight` updates `weight` and resets `current` via two independent atomic stores. If `increaseCurrent()` runs between these two stores, it can (a) increment using the old/new weight unexpectedly and/or (b) have its increment wiped out by the subsequent `current` reset. If the intent is that weight updates are applied atomically with the current reset, consider guarding `setWeight` and selection/increment operations with a lock (or otherwise ensuring these mutations happen under a single critical section), or redesigning state so the update can be applied as one atomic unit.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] fix(loadbalance): fix data race in RoundRobin setWeight and increaseCurrent [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3435:
URL: https://github.com/apache/dubbo-go/pull/3435#issuecomment-4726760565
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3435) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3435&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3435&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3435&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3435&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3435&metric=new_duplicated_lines_density&view=list)
<!-- sqra-placement-anchor -->
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3435)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] fix(loadbalance): fix data race in RoundRobin setWeight and increaseCurrent [dubbo-go]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #3435:
URL: https://github.com/apache/dubbo-go/pull/3435#issuecomment-4726786204
## [Codecov](https://app.codecov.io/gh/apache/dubbo-go/pull/3435?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 53.54%. Comparing base ([`a7b6ee6`](https://app.codecov.io/gh/apache/dubbo-go/commit/a7b6ee632a2d2e4c908cd5c10434d062c5040fe8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`fc0def5`](https://app.codecov.io/gh/apache/dubbo-go/commit/fc0def5ea7f25adb0e8bba28180a4e3b2f6aa081?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 13 commits behind head on develop.
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## develop #3435 +/- ##
===========================================
+ Coverage 53.17% 53.54% +0.36%
===========================================
Files 493 493
Lines 38245 38386 +141
===========================================
+ Hits 20338 20555 +217
+ Misses 16271 16178 -93
- Partials 1636 1653 +17
```
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/dubbo-go/pull/3435?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] fix(loadbalance): fix data race in RoundRobin setWeight and increaseCurrent [dubbo-go]
Posted by "Alanxtl (via GitHub)" <gi...@apache.org>.
Alanxtl merged PR #3435:
URL: https://github.com/apache/dubbo-go/pull/3435
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org