Skip to content

Commit 5c39e6f

Browse files
author
QTom
committed
fix(ops_alert): wg.Add 竞态修复 + leader lock release context 泄漏
1. Start() 中 wg.Add(1) 从 run() goroutine 内部移到 go s.run() 之前, 防止 Stop().wg.Wait() 在 Add 之前返回导致孤儿 goroutine。 2. tryAcquireLeaderLock 返回的 release 闭包改用独立的 context.Background()+5s 超时,避免捕获的 evaluateOnce ctx 在 defer 执行时已过期导致锁释放失败(最长阻塞 90s TTL)。
1 parent a225a24 commit 5c39e6f

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

backend/internal/service/ops_alert_evaluator_service.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (s *OpsAlertEvaluatorService) Start() {
8888
if s.stopCh == nil {
8989
s.stopCh = make(chan struct{})
9090
}
91+
s.wg.Add(1)
9192
go s.run()
9293
})
9394
}
@@ -105,7 +106,6 @@ func (s *OpsAlertEvaluatorService) Stop() {
105106
}
106107

107108
func (s *OpsAlertEvaluatorService) run() {
108-
s.wg.Add(1)
109109
defer s.wg.Done()
110110

111111
// Start immediately to produce early feedback in ops dashboard.
@@ -848,7 +848,9 @@ func (s *OpsAlertEvaluatorService) tryAcquireLeaderLock(ctx context.Context, loc
848848
return nil, false
849849
}
850850
return func() {
851-
_, _ = opsAlertEvaluatorReleaseScript.Run(ctx, s.redisClient, []string{key}, s.instanceID).Result()
851+
releaseCtx, releaseCancel := context.WithTimeout(context.Background(), 5*time.Second)
852+
defer releaseCancel()
853+
_, _ = opsAlertEvaluatorReleaseScript.Run(releaseCtx, s.redisClient, []string{key}, s.instanceID).Result()
852854
}, true
853855
}
854856

0 commit comments

Comments
 (0)