From 6461d2ee83067bc8790335aa384234f523f2f9ff Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Sat, 14 Nov 2026 16:55:53 +0000 Subject: [PATCH] [management] Fix data race on ctx in HTTP metrics middleware The context.AfterFunc callback read the local ctx while the handler reassigned it after ServeHTTP, so a request context cancelled as the handler finished raced with that write. Capture the request context in its own variable for the callback, use a separate variable for the post-ServeHTTP context, and stop the AfterFunc once the handler returns. --- management/server/telemetry/http_api_metrics.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/management/server/telemetry/http_api_metrics.go b/management/server/telemetry/http_api_metrics.go index 360d369496a..8483f8e433f 100644 --- a/management/server/telemetry/http_api_metrics.go +++ b/management/server/telemetry/http_api_metrics.go @@ -189,27 +189,29 @@ func (m *HTTPMiddleware) Handler(h http.Handler) http.Handler { w := WrapResponseWriter(rw) + reqCtx := ctx handlerDone := make(chan struct{}) - context.AfterFunc(ctx, func() { + stopAfter := context.AfterFunc(reqCtx, func() { select { case <-handlerDone: default: log.Debugf("HTTP request context canceled mid-flight: %v %v (reqID=%s, after %v, cause: %v)", - r.Method, r.URL.Path, reqID, time.Since(reqStart), context.Cause(ctx)) + r.Method, r.URL.Path, reqID, time.Since(reqStart), context.Cause(reqCtx)) } }) // Hold on to req so auth's in-place ctx update is visible after ServeHTTP. - req := r.WithContext(ctx) + req := r.WithContext(reqCtx) h.ServeHTTP(w, req) close(handlerDone) + stopAfter() - ctx = req.Context() + respCtx := req.Context() if w.Status() > 399 { - log.WithContext(ctx).Errorf("HTTP response %v: %v %v status %v", reqID, r.Method, r.URL, w.Status()) + log.WithContext(respCtx).Errorf("HTTP response %v: %v %v status %v", reqID, r.Method, r.URL, w.Status()) } else { - log.WithContext(ctx).Tracef("HTTP response %v: %v %v status %v", reqID, r.Method, r.URL, w.Status()) + log.WithContext(respCtx).Tracef("HTTP response %v: %v %v status %v", reqID, r.Method, r.URL, w.Status()) } statusCodeAttr := attribute.Int("code", w.Status()) @@ -220,7 +222,7 @@ func (m *HTTPMiddleware) Handler(h http.Handler) http.Handler { reqTook := time.Since(reqStart) m.httpRequestDuration.Record(m.ctx, reqTook.Milliseconds(), metric.WithAttributes(endpointAttr, methodAttr)) - log.WithContext(ctx).Debugf("request %s %s took %d ms and finished with status %d", r.Method, r.URL.Path, reqTook.Milliseconds(), w.Status()) + log.WithContext(respCtx).Debugf("request %s %s took %d ms and finished with status %d", r.Method, r.URL.Path, reqTook.Milliseconds(), w.Status()) if w.Status() == 200 && (r.Method == http.MethodPut || r.Method == http.MethodPost || r.Method == http.MethodDelete) { opts := metric.WithAttributeSet(attribute.NewSet(attribute.String("type", "write")))