Skip to content

Commit 7d5aff6

Browse files
committed
fix: zombie process on metrics server fail
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
1 parent b4fc89c commit 7d5aff6

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

flagd/pkg/service/flag-evaluation/connect_service.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ func (s *ConnectService) Serve(ctx context.Context, svcConf service.Configuratio
102102
s.readinessEnabled = true
103103

104104
g.Go(func() error {
105-
return s.startServer(svcConf)
105+
return s.startServer(gCtx, svcConf)
106106
})
107107
g.Go(func() error {
108-
return s.startMetricsServer(svcConf)
108+
return s.startMetricsServer(gCtx, svcConf)
109109
})
110110
g.Go(func() error {
111111
<-gCtx.Done()
@@ -251,7 +251,7 @@ func (s *ConnectService) Shutdown() {
251251
})
252252
}
253253

254-
func (s *ConnectService) startServer(svcConf service.Configuration) error {
254+
func (s *ConnectService) startServer(ctx context.Context, svcConf service.Configuration) error {
255255
lis, err := s.setupServer(svcConf)
256256
if err != nil {
257257
return err
@@ -275,7 +275,7 @@ func (s *ConnectService) startServer(svcConf service.Configuration) error {
275275
return nil
276276
}
277277

278-
func (s *ConnectService) startMetricsServer(svcConf service.Configuration) error {
278+
func (s *ConnectService) startMetricsServer(ctx context.Context, svcConf service.Configuration) error {
279279
s.logger.Info(fmt.Sprintf("metrics and probes listening at %d", svcConf.ManagementPort))
280280

281281
srv := grpc.NewServer()
@@ -312,8 +312,21 @@ func (s *ConnectService) startMetricsServer(svcConf service.Configuration) error
312312
}
313313
s.metricsServerMtx.Unlock()
314314

315-
if err := s.metricsServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
316-
return fmt.Errorf("error returned from metrics server: %w", err)
315+
errChan := make(chan error, 1)
316+
go func() { errChan <- s.metricsServer.ListenAndServe() }()
317+
318+
select {
319+
case err := <-errChan:
320+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
321+
return fmt.Errorf("error returned from metrics server: %w", err)
322+
}
323+
return nil
324+
case <-ctx.Done():
325+
if err := s.metricsServer.Shutdown(ctx); err != nil {
326+
return fmt.Errorf("error shutting down metrics server: %w", err)
327+
}
328+
// wait for server to fully stop
329+
<-errChan
330+
return nil
317331
}
318-
return nil
319332
}

0 commit comments

Comments
 (0)