AlexStocks commented on code in PR #1092:
URL: https://github.com/apache/dubbo-go-pixiu/pull/1092#discussion_r4585766672


##########
admin/core/xds.go:
##########
@@ -101,8 +101,18 @@ func StartxDsServer(ctx context.Context) error {
        )
 
        // Establishing the watch before rebuilding closes the startup gap: 
writes
-       // that race with the full read remain queued on the watch channel.
-       go watchConfigAndReload(ctx, publisher)
+       // that race with the full read remain queued on the watch channel. 
Keep the
+       // watcher within this server's lifecycle so the etcd client stays alive
+       // until any in-flight snapshot build has finished.
+       watcherDone := make(chan struct{})
+       go func() {
+               defer close(watcherDone)
+               watchConfigAndReload(ctx, publisher)
+       }()
+       defer func() {
+               cancel()
+               <-watcherDone

Review Comment:
   [P2] 关闭等待没有上界,且 cancel() 无法中断在途的 etcd 读
   
   触发条件:`StartxDsServer` 返回时,这里的 `<-watcherDone` 会一直等到 `watchConfigAndReload` 
退出。该 goroutine 若正好在 `consumeConfigWatch` → `publish()` → 
`SnapshotPublisher.Publish` → `SnapshotBuilder.Build` → 
`logic.BizGetListeners/BizGetClusters` 里,前面那行 `cancel()` 不会让它提前返回。
   
   证据(源码级,head eee462bc):
   - `admin/xds/snapshot.go` 的 `Build(version string)` 不接受 
context;`admin/logic/logic.go` 的读取最终走 
`adminconfig.Client.Get()/GetChildrenKVList()`,而 
`github.com/dubbogo/[email protected]/database/kv/etcd/v3/client.go` 里 `GetChildren` 
用的是 `rawClient.Get(c.ctx, ...)`——`c.ctx` 来自 `NewClient` 里的 
`context.WithCancel(context.Background())`,既无 deadline,也不受本函数的 `cancel()` 影响。
   - 因此等待时长只由 etcd RPC 决定:`admin/config/config.go` 的 `InitEtcdClient` 只把 20s 作为 
`DialTimeout`(`etcdv3.WithTimeout(20*time.Second)`),不是单次 Get 的请求级超时;`Build` 里 
listeners 与 clusters 是两次串行读,etcd 不可达时可拖到数十秒量级。
   
   实际影响:同一个进程里 HTTP 与 gRPC 的关闭都显式限了 
5s(`httpShutdownTimeout`、`grpcShutdownTimeout`),只有这里没有上界;`RunServer` 用 
`signal.NotifyContext`,ctx 已取消后再收到 SIGTERM 只会被投递到已无人接收的 channel,无法强制退出,运维侧只能等 
SIGKILL。
   
   修复方向:给等待加上界,例如 `select { case <-watcherDone: case 
<-time.After(grpcShutdownTimeout): }`;或把带 deadline 的 ctx 继续传进 
`Publish/Build`,让在途构建能被打断。建议补一个「etcd 阻塞时 `StartxDsServer` 能在 N 秒内返回」的用例。



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