AlexStocks commented on code in PR #3438:
URL: https://github.com/apache/dubbo-go/pull/3438#discussion_r4513463394
##########
remoting/exchange_client.go:
##########
@@ -82,20 +93,52 @@ func NewExchangeClient(url *common.URL, client Client,
connectTimeout time.Durat
}
func (cl *ExchangeClient) doInit(url *common.URL) error {
- if cl.init {
+ // Fast path: already initialized.
+ if cl.initState.Load() == initInitialized {
return nil
}
+ // Try to become the initializer.
+ if !cl.initState.CAS(initUninitialized, initInitializing) {
+ // Another goroutine is initializing. Wait for it to complete.
+ cl.initCond.L.Lock()
+ for cl.initState.Load() == initInitializing {
+ cl.initCond.Wait()
+ }
+ err := cl.initErr
Review Comment:
[P2] `Close()` 与等待者交叉时,「未初始化」会被当成「初始化成功」
触发条件:G1 进入 `doInit` 并 CAS 成功(状态 `initInitializing`),此时阻塞在
`cl.client.Connect(url)`(`ConnectTimeout` 上限内,失败后还有 100ms + 第二次 `Connect`);G2
同时调用 `doInit`,在 104-106 行进入 `Wait()`;此刻 G3 调用 `Close()`(241-247 行)把状态写回
`initUninitialized`、把 `initErr` 清成 `nil`,随后 `Broadcast()`。G2 被唤醒,发现状态已不是
`initInitializing`,跳出循环,读到 `initErr == nil`,在第 107-108 行返回 `nil`。
实际影响:`doInit` 的返回契约是「`nil` 表示初始化已完成」,但这里 G2
拿到的是「状态被外部重置且没有任何错误记录」,它与成功无法区分。调用方据此认为连接可用,继续走 `IsAvailable()` /
`Send()`,失败被推迟到发送阶段才暴露,错误归属从「初始化被中止」变成「请求失败」,重试与降级策略会因此走错分支。这与改动前不同:改动前没有等待者,并发调用会各自重新
`Connect`,不会凭空得到一次成功。
证据:`initErr` 只在失败路径被赋值(defer 中 `initErr != nil` 的分支),`Close()` 则显式执行
`client.initErr =
nil`;等待路径没有任何机制区分「我等待的那次初始化成功」和「状态被外部重置」。`exchange_client_test.go`
新增用例覆盖了并发成功、失败后重试与 panic 释放等待者,但没有 `Close()` 与等待者交叉的时序。
修复方向:等待退出后不要直接返回 `initErr`,而是再判一次终态——若 `initState == initUninitialized` 且
`initErr == nil`,返回一个明确的「初始化被中止」错误(或重新尝试 CAS 让自己成为新的初始化者);另一种做法是 `Close()`
里写入哨兵错误而非清空 `initErr`,让等待者拿到可判定的失败原因。
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]