@@ -100,7 +100,7 @@ func Execute() {
100100 ctx , cancel := context .WithCancel (context .Background ())
101101
102102 go serveMetrics (cfg .MetricsAddress )
103- go handleSigterm (cancel )
103+ setupSigtermHandler (cancel )
104104
105105 endpointsSource , err := buildSource (ctx , cfg )
106106 if err != nil {
@@ -470,14 +470,23 @@ func createDomainFilter(cfg *externaldns.Config) *endpoint.DomainFilter {
470470
471471// handleSigterm listens for a SIGTERM signal and triggers the provided cancel function
472472// to gracefully terminate the application. It logs a message when the signal is received.
473- func handleSigterm (cancel func ()) {
473+ // The setupCh channel is used to signal when the signal handler is ready to receive signals.
474+ func handleSigterm (cancel func (), setupCh chan struct {}) {
474475 signals := make (chan os.Signal , 1 )
475476 signal .Notify (signals , syscall .SIGTERM )
477+ close (setupCh )
476478 <- signals
477479 log .Info ("Received SIGTERM. Terminating..." )
478480 cancel ()
479481}
480482
483+ // setupSigtermHandler initializes the SIGTERM handler in a separate goroutine and waits for it to be ready.
484+ func setupSigtermHandler (cancel func ()) {
485+ setupCh := make (chan struct {})
486+ go handleSigterm (cancel , setupCh )
487+ <- setupCh
488+ }
489+
481490// serveMetrics starts an HTTP server that serves health and metrics endpoints.
482491// The /healthz endpoint returns a 200 OK status to indicate the service is healthy.
483492// The /metrics endpoint serves Prometheus metrics.
0 commit comments