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


##########
cluster/cluster/forking/cluster_invoker.go:
##########
@@ -72,16 +72,25 @@ func (invoker *forkingClusterInvoker) Invoke(ctx 
context.Context, invocation pro
        }
 
        resultQ := queue.New(1)
+       forkCtx, cancel := context.WithCancel(ctx)
+       defer cancel() // cancel forkCtx when Invoke returns, signaling all 
forked goroutines to stop

Review Comment:
   🟡 **这是一次 goroutine 泄漏修复,但没有任何回归测试锁住它。**
   
   当前 diff 只改了实现。建议补一个能失败的测试,最小形态:
   
   - 用一个 `protocolbase.Invoker` 桩,`Invoke` 阻塞在 `<-ctx.Done()`(或 channel)上;
   - 让 `Invoke` 的正常路径在 1ms 内返回(另一个桩立即返回结果);
   - 用 `runtime.NumGoroutine()` 前后对比,或 `uber-go/goleak` 断言测试结束时没有残留 goroutine。
   
   没有这个测试的话,`cancel()` 一旦在后续重构中被挪走/误删,行为会静默退化为原来的泄漏,而且 CI 不会有任何反应。
   
   另外说明一点:`cancel()` 只在 provider 尊重 ctx 时才真正终止下游调用;如果某个 protocol 实现忽略 ctx,这枚 
goroutine 仍会跑到自然结束。这属于既有约束,不需要在本 PR 解决,但值得在注释里写明「best-effort」。



##########
cluster/cluster/forking/cluster_invoker.go:
##########
@@ -72,16 +72,25 @@ func (invoker *forkingClusterInvoker) Invoke(ctx 
context.Context, invocation pro
        }
 
        resultQ := queue.New(1)
+       forkCtx, cancel := context.WithCancel(ctx)
+       defer cancel() // cancel forkCtx when Invoke returns, signaling all 
forked goroutines to stop
+
        for _, ivk := range selected {
                go func(k protocolbase.Invoker) {
-                       result := k.Invoke(ctx, invocation)
+                       result := k.Invoke(forkCtx, invocation)
                        if err := resultQ.Put(result); err != nil {
-                               logger.Errorf("[Cluster][Forking] resultQ put 
failed with exception err=%v", err)
+                               // ErrDisposed is expected when another 
goroutine's result was already
+                               // consumed and the queue disposed; log at 
debug level to avoid noise.
+                               logger.Debugf("[Cluster][Forking] resultQ put 
failed, the queue is probably disposed: %v", err)
                        }
                }(ivk)
        }
 
        rsps, err := resultQ.Poll(1, time.Millisecond*time.Duration(timeouts))
+       // Dispose the queue so that remaining goroutines' Put calls return 
ErrDisposed
+       // instead of succeeding into a discarded queue. Combined with the 
cancel() above,
+       // this ensures forked goroutines can exit promptly.
+       resultQ.Dispose()

Review Comment:
   💭 建议把 `Dispose()` 写成 `defer`:
   
   ```go
   resultQ := queue.New(1)
   forkCtx, cancel := context.WithCancel(ctx)
   defer cancel()
   defer resultQ.Dispose()
   ```
   
   现在这个位置是「当前所有 return 都恰好在它之后」,逻辑没错;但函数后续只要新增一个提前返回(比如参数校验、`len(rsps)==0` 
的更早检查),队列就不会被 dispose,退化成「结果静默写进被丢弃的队列」,而这正是本 PR 想消除的形态。放在 `defer` 
里可以让这条不变量与代码结构解耦。



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