Skip to content

Commit

Permalink
Apply shutdown timeout to http server to limit reload delay (#14339)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carsonip authored Oct 15, 2024
1 parent 79de1ef commit 24329c8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelogs/head.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ https://github.com/elastic/apm-server/compare/8.15\...main[View commits]

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

[float]
==== Breaking Changes
Expand Down
4 changes: 2 additions & 2 deletions internal/beater/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ func (h *httpServer) start() error {
return h.Serve(h.httpListener)
}

func (h *httpServer) stop() {
func (h *httpServer) stop(ctx context.Context) {
h.logger.Infof("Stop listening on: %s", h.Server.Addr)
if err := h.Shutdown(context.Background()); err != nil {
if err := h.Shutdown(ctx); err != nil {
h.logger.Errorf("error stopping http server: %s", err.Error())
if err := h.Close(); err != nil {
h.logger.Errorf("error closing http server: %s", err.Error())
Expand Down
7 changes: 3 additions & 4 deletions internal/beater/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,10 @@ func (s server) run(ctx context.Context) error {
})
g.Go(func() error {
<-ctx.Done()
// httpServer should stop before grpcServer to avoid a panic caused by placing a new connection into
// a closed grpc connection channel during shutdown.
// See https://github.com/elastic/gmux/issues/13
s.httpServer.stop()
s.grpcServer.GracefulStop()
stopctx, cancel := context.WithTimeout(context.Background(), s.cfg.ShutdownTimeout)
defer cancel()
s.httpServer.stop(stopctx)
return nil
})
if err := g.Wait(); err != http.ErrServerClosed {
Expand Down

0 comments on commit 24329c8

Please sign in to comment.