Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive: race condition in handler Close() when stopped early #7087

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#7011](https://github.com/thanos-io/thanos/pull/7011) Query Frontend: queries with negative offset should check whether it is cacheable or not.
- [#6874](https://github.com/thanos-io/thanos/pull/6874) Sidecar: fix labels returned by 'api/v1/series' in presence of conflicting external and inner labels.
- [#7009](https://github.com/thanos-io/thanos/pull/7009) Rule: Fix spacing error in URL.
- [#7080](https://github.com/thanos-io/thanos/pull/7080) Receive: race condition in handler Close() when stopped early

### Added

Expand Down
39 changes: 18 additions & 21 deletions pkg/receive/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ type Options struct {

// Handler serves a Prometheus remote write receiving HTTP endpoint.
type Handler struct {
logger log.Logger
writer *Writer
router *route.Router
options *Options
listener net.Listener
logger log.Logger
writer *Writer
router *route.Router
options *Options
httpSrv *http.Server

mtx sync.RWMutex
hashring Hashring
Expand Down Expand Up @@ -241,6 +241,14 @@ func NewHandler(logger log.Logger, o *Options) *Handler {
})
statusAPI.Register(h.router, o.Tracer, logger, ins, logging.NewHTTPServerMiddleware(logger))

errlog := stdlog.New(log.NewStdlibAdapter(level.Error(h.logger)), "", 0)

h.httpSrv = &http.Server{
Handler: h.router,
ErrorLog: errlog,
TLSConfig: h.options.TLSConfig,
}

return h
}

Expand Down Expand Up @@ -364,42 +372,31 @@ func (h *Handler) getStats(r *http.Request, statsByLabelName string) ([]statusap

// Close stops the Handler.
func (h *Handler) Close() {
if h.listener != nil {
runutil.CloseWithLogOnErr(h.logger, h.listener, "receive HTTP listener")
}
runutil.CloseWithLogOnErr(h.logger, h.httpSrv, "receive HTTP server")
GiedriusS marked this conversation as resolved.
Show resolved Hide resolved
}

// Run serves the HTTP endpoints.
func (h *Handler) Run() error {
level.Info(h.logger).Log("msg", "Start listening for connections", "address", h.options.ListenAddress)

var err error
h.listener, err = net.Listen("tcp", h.options.ListenAddress)
listener, err := net.Listen("tcp", h.options.ListenAddress)
if err != nil {
return err
}

// Monitor incoming connections with conntrack.
h.listener = conntrack.NewListener(h.listener,
listener = conntrack.NewListener(listener,
conntrack.TrackWithName("http"),
conntrack.TrackWithTracing())

errlog := stdlog.New(log.NewStdlibAdapter(level.Error(h.logger)), "", 0)

httpSrv := &http.Server{
Handler: h.router,
ErrorLog: errlog,
TLSConfig: h.options.TLSConfig,
}

if h.options.TLSConfig != nil {
level.Info(h.logger).Log("msg", "Serving HTTPS", "address", h.options.ListenAddress)
// Cert & Key are already being passed in via TLSConfig.
return httpSrv.ServeTLS(h.listener, "", "")
return h.httpSrv.ServeTLS(listener, "", "")
}

level.Info(h.logger).Log("msg", "Serving plain HTTP", "address", h.options.ListenAddress)
return httpSrv.Serve(h.listener)
return h.httpSrv.Serve(listener)
}

// replica encapsulates the replica number of a request and if the request is
Expand Down
9 changes: 9 additions & 0 deletions pkg/receive/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1645,3 +1645,12 @@ func TestHashringChangeCallsClose(t *testing.T) {
pg := allHandlers[0].peers.(*fakePeersGroup)
testutil.Assert(t, len(pg.closeCalled) > 0)
}

func TestHandlerEarlyStop(t *testing.T) {
h := NewHandler(nil, &Options{})
h.Close()

err := h.Run()
testutil.NotOk(t, err)
testutil.Equals(t, "http: Server closed", err.Error())
}
Loading