From 5a15925bc124f722d58faf55d98af1a68209a53e Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Tue, 3 Feb 2026 15:32:34 +0100 Subject: [PATCH] tsdb: improve initTime busy-wait loop - Add runtime.Gosched() to yield CPU during the busy-wait loop - Reduce timeout from 500ms to 100ms (actual wait should be microseconds) - Add warning log when timeout occurs to aid debugging Signed-off-by: Arve Knudsen --- tsdb/head_append.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tsdb/head_append.go b/tsdb/head_append.go index 005d20b720..91134daa13 100644 --- a/tsdb/head_append.go +++ b/tsdb/head_append.go @@ -19,6 +19,7 @@ import ( "fmt" "log/slog" "math" + "runtime" "time" "github.com/prometheus/prometheus/model/exemplar" @@ -120,12 +121,18 @@ func (h *Head) initTime(t int64) { if !h.minTime.CompareAndSwap(math.MaxInt64, t) { // Concurrent appends that are initializing. // Wait until h.maxTime is swapped to avoid minTime/maxTime races. - antiDeadlockTimeout := time.After(500 * time.Millisecond) + // This should complete in microseconds under normal operation. + antiDeadlockTimeout := time.After(100 * time.Millisecond) for h.maxTime.Load() == math.MinInt64 { select { case <-antiDeadlockTimeout: + // This should never happen in normal operation. + // If it does, there may be a bug or the system is severely overloaded. + h.logger.Warn("initTime timeout waiting for maxTime initialization") return default: + // Yield to allow the initializing goroutine to complete. + runtime.Gosched() } } return