AlexStocks commented on code in PR #3438:
URL: https://github.com/apache/dubbo-go/pull/3438#discussion_r4505078683


##########
remoting/exchange_client.go:
##########
@@ -82,20 +86,43 @@ 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() == 2 {
                return nil
        }
+       // Try to become the initializer: transition 0 -> 1 (uninitialized -> 
initializing).
+       if !cl.initState.CAS(0, 1) {
+               // Another goroutine is initializing. Wait for it to complete.
+               cl.initCond.L.Lock()
+               for cl.initState.Load() == 1 {

Review Comment:
   🟡 **初始化者 panic 会让所有后续调用永久阻塞。**
   
   现在状态机只有两个出口:成功 `Store(2)`、失败 `Store(0)`。如果 `cl.client.Connect(url)` 或其下面的 
transport 实现 panic(未捕获),状态会永远停在 `1`,之后任何调用者都会进到 `for cl.initState.Load() == 1 { 
Wait() }` —— **没有人会再 `Broadcast()`**,全部永久挂起。
   
   旧实现里 panic 只是让这次 `doInit` 异常退出,下一次调用还能重试。
   
   建议在拿到 CAS 之后立刻挂一个兜底:
   
   ```go
   if !cl.initState.CAS(0, 1) {
       ...wait...
   }
   // Ensure waiters are released and the state machine is consistent even if
   // Connect panics; callers will observe the panic (or a retry on next call).
   defer func() {
       if r := recover(); r != nil {
           cl.initCond.L.Lock()
           cl.initState.Store(0)
           cl.initErr = fmt.Errorf("exchange client init panic: %v", r)
           cl.initCond.L.Unlock()
           cl.initCond.Broadcast()
           panic(r) // keep the original crash semantics for this goroutine
       }
   }()
   ```
   
   (是否重新 panic 取决于你们对 `Connect` 的约定;关键是必须有人负责解锁等待者。)



##########
remoting/exchange_client.go:
##########
@@ -61,7 +62,9 @@ type ExchangeClient struct {
        ConnectTimeout time.Duration  // timeout for connecting server
        address        string         // server address for dialing. The 
format: ip:port
        client         Client         // dealing with the transport
-       init           bool           // the tag for init.
+       initState      uatomic.Int32  // init state: 0=uninitialized, 
1=initializing, 2=initialized

Review Comment:
   💭 `0/1/2` 三处魔法数字散落在 `doInit`/`Close` 中,建议提成具名常量:
   
   ```go
   const (
       initUninitialized int32 = iota
       initInitializing
       initInitialized
   )
   ```
   
   测试里也用同一组常量断言,避免以后新增第四个状态时测试还在断言「2」。



##########
remoting/exchange_client_test.go:
##########
@@ -112,6 +112,83 @@ func TestExchangeClientIsAvailable(t *testing.T) {
        assert.False(t, ec.IsAvailable())
 }
 
+func TestExchangeClientConcurrentDoInit(t *testing.T) {
+       // Verify that concurrent calls to doInit only result in a single 
Connect call.
+       m := &mockClient{available: true}
+       ec := NewExchangeClient(testURL(), m, 5*time.Second, true)
+
+       var wg sync.WaitGroup
+       const goroutines = 20
+       errCh := make(chan error, goroutines)
+       wg.Add(goroutines)
+       for i := 0; i < goroutines; i++ {
+               go func() {
+                       defer wg.Done()
+                       if err := ec.doInit(testURL()); err != nil {
+                               errCh <- err
+                       }
+               }()
+       }
+       wg.Wait()
+       close(errCh)
+
+       for err := range errCh {
+               t.Errorf("doInit failed: %v", err)
+       }
+
+       m.mu.Lock()

Review Comment:
   💭 并发用例建议把断言换成「`Connect` 恰好一次 + 所有调用都成功」,再加一条**超时保护**:当前 `wg.Wait()` 如果状态机有 
bug(比如等待者永不唤醒),整个测试会挂到 `go test` 的全局超时才发现,失败信息不指向这里。可以在 `wg.Wait()` 外面套 
`select` + `time.After(5s)` 让失败更早、更明确。



-- 
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]

Reply via email to