From 725c8d9e43554ee3ce34b44c36fc55642e4f25cf Mon Sep 17 00:00:00 2001 From: Ridwan Sharif Date: Mon, 20 Nov 2023 19:57:19 +0000 Subject: [PATCH] scrape_test: add scrape offset in test scraper to avoid race This change adds an offset to the test scraper so a scrape is not initiated when the context is cancelled. Without this change, there was a race between the context cancellation and the scrape jitter wait and this change fixes that. Can verify using: go test -run ^TestScrapeLoopStopBeforeRun$ github.com/prometheus/prometheus/scrape -race -count 1000 Signed-off-by: Ridwan Sharif --- scrape/scrape_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index ad319d1cd8..354b7a9859 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -647,6 +647,10 @@ func TestScrapeLoopStopBeforeRun(t *testing.T) { nil, ) + // The loop must terminate during the initial offset if the context + // is canceled. + scraper.offsetDur = time.Hour + // The scrape pool synchronizes on stopping scrape loops. However, new scrape // loops are started asynchronously. Thus it's possible, that a loop is stopped // again before having started properly.