Skip to content

Commit 24329c8

Browse files
authored
Apply shutdown timeout to http server to limit reload delay (#14339)
httpServer.stop may block indefinitely, potentially due to misbehaving connections. Apply shutdown timeout to http server so that in a hot reload, the old and new server overlapping time is bounded.
1 parent 79de1ef commit 24329c8

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

changelogs/head.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ https://github.com/elastic/apm-server/compare/8.15\...main[View commits]
88

99
- Track all bulk request response status codes {pull}13574[13574]
1010
- Fix a concurrent map write panic in monitoring middleware {pull}14335[14335]
11+
- Apply shutdown timeout to http server {pull}14339[14339]
1112

1213
[float]
1314
==== Breaking Changes

internal/beater/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ func (h *httpServer) start() error {
106106
return h.Serve(h.httpListener)
107107
}
108108

109-
func (h *httpServer) stop() {
109+
func (h *httpServer) stop(ctx context.Context) {
110110
h.logger.Infof("Stop listening on: %s", h.Server.Addr)
111-
if err := h.Shutdown(context.Background()); err != nil {
111+
if err := h.Shutdown(ctx); err != nil {
112112
h.logger.Errorf("error stopping http server: %s", err.Error())
113113
if err := h.Close(); err != nil {
114114
h.logger.Errorf("error closing http server: %s", err.Error())

internal/beater/server.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,10 @@ func (s server) run(ctx context.Context) error {
222222
})
223223
g.Go(func() error {
224224
<-ctx.Done()
225-
// httpServer should stop before grpcServer to avoid a panic caused by placing a new connection into
226-
// a closed grpc connection channel during shutdown.
227-
// See https://github.com/elastic/gmux/issues/13
228-
s.httpServer.stop()
229225
s.grpcServer.GracefulStop()
226+
stopctx, cancel := context.WithTimeout(context.Background(), s.cfg.ShutdownTimeout)
227+
defer cancel()
228+
s.httpServer.stop(stopctx)
230229
return nil
231230
})
232231
if err := g.Wait(); err != http.ErrServerClosed {

0 commit comments

Comments
 (0)