AlexStocks commented on code in PR #993:
URL: https://github.com/apache/dubbo-go-pixiu/pull/993#discussion_r4505146749
##########
pkg/server/listener_manager.go:
##########
@@ -117,31 +117,138 @@ func (lm *ListenerManager) gracefulShutdownInit() {
sig := <-signals
logger.Infof("get signal %s, dubbo-go-pixiu will start
shutdown.", sig)
- time.AfterFunc(timeout, func() {
- logger.Warn("Shutdown gracefully timeout, listeners
will shutdown immediately. ")
- os.Exit(0)
+ shutdownErrors, timedOut := lm.handleShutdownSignal(sig,
timeout)
+
+ if len(shutdownErrors) > 0 {
+ logger.Errorf("Shutdown completed with %d errors",
len(shutdownErrors))
+ }
+ if timedOut {
+ logger.Warn("Shutdown gracefully timeout, some
listeners may not have shut down cleanly")
+ }
+ os.Exit(shutdownExitCode(shutdownErrors, timedOut))
Review Comment:
🟡 **退出码从「恒 0」变成「错误即 1」,这是用户可见的行为变更,需要显式决策。**
`os.Exit(shutdownExitCode(...))` 在「任一 listener 返回错误」或「整体超时」时给 1。历史行为是恒 0(旧的
`time.AfterFunc(timeout, os.Exit(0))` 兜底也是 0)。
风险在编排侧:k8s 会把 SIGTERM 后的非零退出码记成 `Error`(`lastState.terminated.exitCode:
1`),systemd 会进 `failed` 状态。滚动发布时只要有一个 listener 的 drain
出点小问题,就会出现「明明正常下线却触发告警/重启计数上升」。
如果保留 1:请在 PR 描述的 release-note 里明确写「关闭期间出现错误时进程以非零码退出」,并在部署文档里提醒排查方式。
如果想要 0:把 `shutdownExitCode` 去掉,错误只写日志即可(错误信息本身已经在 `handleShutdownSignal`
里聚合记录了)。
##########
pkg/server/listener_manager.go:
##########
@@ -117,31 +117,138 @@ func (lm *ListenerManager) gracefulShutdownInit() {
sig := <-signals
logger.Infof("get signal %s, dubbo-go-pixiu will start
shutdown.", sig)
- time.AfterFunc(timeout, func() {
- logger.Warn("Shutdown gracefully timeout, listeners
will shutdown immediately. ")
- os.Exit(0)
+ shutdownErrors, timedOut := lm.handleShutdownSignal(sig,
timeout)
+
+ if len(shutdownErrors) > 0 {
+ logger.Errorf("Shutdown completed with %d errors",
len(shutdownErrors))
+ }
+ if timedOut {
+ logger.Warn("Shutdown gracefully timeout, some
listeners may not have shut down cleanly")
+ }
+ os.Exit(shutdownExitCode(shutdownErrors, timedOut))
+ }()
+}
+
+func shutdownExitCode(shutdownErrors []error, timedOut bool) int {
+ if timedOut || len(shutdownErrors) > 0 {
+ return 1
+ }
+ return 0
+}
+
+// handleShutdownSignal processes a shutdown signal by coordinating listener
shutdowns.
+// It returns a slice of errors from failed shutdowns and a boolean indicating
timeout.
+// This function is extracted from gracefulShutdownInit for testability.
+func (lm *ListenerManager) handleShutdownSignal(sig os.Signal, timeout
time.Duration) ([]error, bool) {
+ lm.rwLock.RLock()
+ listeners := make([]*wrapListenerService, 0,
len(lm.activeListenerService))
+ for _, listener := range lm.activeListenerService {
+ listeners = append(listeners, listener)
+ }
+ lm.rwLock.RUnlock()
+
+ shutdownFuncs := make([]shutdownFunc, 0, len(listeners))
+ for _, listener := range listeners {
+ shutdownFuncs = append(shutdownFuncs, func() error {
+ // Preserve the legacy plugin argument without sharing
completion counts.
+ // shutdownListeners coordinates by return, not by this
WaitGroup.
+ var shutdownWG sync.WaitGroup
Review Comment:
💭 这个「每个 listener 一个新 WaitGroup、`Add(1)` 之后就地丢弃」的写法,功能上没问题(协调已改由
`doneCh`/`allDone` 承担),但它把一份「看起来还有约束力、实际没人等」的契约交给了插件作者。
具体表现:`ListenerService.ShutDown(any)` 是导出接口,内置实现按「调用方 Add、我自己
Done」配平;第三方插件如果实现成「`timeout <= 0` 直接 return、不调 Done」(在旧 base 里是安全的,因为旧 base 也从不
`Wait()` 它),在新代码里同样无害——但接口注释(`listener.go:44-46`)却说「必须在所有返回路径上恰好 Done
一次」。注释在描述一个已不存在的约束。
建议二选一:
- 保留参数、注释改成「返回值是唯一契约;WaitGroup 仅为兼容既有插件保留,不参与协调」;
- 或者排进下一个 major,把签名换成 `ShutDown() error` —— 现在正是最便宜的时刻,因为逻辑已经与 WG 完全解耦。
--
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]