Skip to content

Commit 25eeb8a

Browse files
fix: replace os.Exit in goroutine with context cancellation for graceful shutdown
The goroutine in main.go that waits for webhook readiness and sets up controllers was calling os.Exit(1) on failure, which kills the process immediately without releasing the leader election lease. Other replicas cannot acquire leadership until the lease TTL expires (default 15s). Additionally, WaitReady() was an infinite loop with no context or timeout, risking a zombie leader state if the webhook health check never passes. This commit: - Wraps the signal handler context with context.WithCancel - Replaces os.Exit(1) in the goroutine with cancel() + return, triggering the manager's graceful shutdown path - Updates WaitReady to accept a context.Context so it can exit when the context is cancelled Fixes #2401
1 parent 749e8f2 commit 25eeb8a

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"flag"
2122
"net/http"
2223
_ "net/http/pprof"
@@ -164,7 +165,9 @@ func main() {
164165
})
165166
}
166167

167-
ctx := ctrl.SetupSignalHandler()
168+
signalCtx := ctrl.SetupSignalHandler()
169+
ctx, cancel := context.WithCancel(signalCtx)
170+
defer cancel()
168171
cfg := ctrl.GetConfigOrDie()
169172
setRestConfig(cfg)
170173
cfg.UserAgent = "kruise-manager"
@@ -256,15 +259,17 @@ func main() {
256259

257260
go func() {
258261
setupLog.Info("wait webhook ready")
259-
if err = webhook.WaitReady(); err != nil {
262+
if err = webhook.WaitReady(ctx); err != nil {
260263
setupLog.Error(err, "unable to wait webhook ready")
261-
os.Exit(1)
264+
cancel()
265+
return
262266
}
263267

264268
setupLog.Info("setup controllers")
265269
if err = controller.SetupWithManager(mgr); err != nil {
266270
setupLog.Error(err, "unable to setup controllers")
267-
os.Exit(1)
271+
cancel()
272+
return
268273
}
269274
}()
270275

pkg/webhook/server.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,16 @@ func Checker(req *http.Request) error {
133133
return health.Checker(req)
134134
}
135135

136-
func WaitReady() error {
136+
func WaitReady(ctx context.Context) error {
137137
startTS := time.Now()
138138
var err error
139139
for {
140+
select {
141+
case <-ctx.Done():
142+
return fmt.Errorf("context cancelled while waiting for webhook ready: %v", ctx.Err())
143+
default:
144+
}
145+
140146
duration := time.Since(startTS)
141147
if err = Checker(nil); err == nil {
142148
return nil
@@ -147,5 +153,4 @@ func WaitReady() error {
147153
}
148154
time.Sleep(time.Second * 2)
149155
}
150-
151156
}

0 commit comments

Comments
 (0)