Repository navigation
Conversation
Signed-off-by: Timon Engelke <timon.engelke@inovex.de>
Author
friedrichg
requested changes
Dec 3, 2026
friedrichg
left a comment
Member
There was a problem hiding this comment.
I was able to trigger data race with this test
package alertmanager
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"
"time"
"github.com/go-kit/log"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)
// TestRaceReproApplyConfigVsGetAlerts reloads the config while API requests
// read alerts. GET /api/v2/alerts calls the status callback for each alert,
// and the callback reads am.inhibitor, which ApplyConfig writes.
func TestRaceReproApplyConfigVsGetAlerts(t *testing.T) {
const user = "test"
am, err := New(&Config{
UserID: user,
Logger: log.NewNopLogger(),
Limits: &mockAlertManagerLimits{},
TenantDataDir: t.TempDir(),
ExternalURL: &url.URL{Path: "/am"},
ShardingEnabled: false,
GCInterval: 30 * time.Minute,
}, prometheus.NewPedanticRegistry())
require.NoError(t, err)
defer am.StopAndWait()
cfgRaw := `receivers:
- name: 'prod'
route:
group_by: ['alertname']
group_wait: 10ms
group_interval: 10ms
receiver: 'prod'`
cfg, err := config.Load(cfgRaw)
require.NoError(t, err)
require.NoError(t, am.ApplyConfig(user, cfg, cfgRaw))
now := time.Now().UTC()
b, err := json.Marshal([]map[string]any{{
"labels": map[string]string{"alertname": "A"},
"startsAt": now.Add(-time.Minute).Format(time.RFC3339),
"endsAt": now.Add(time.Hour).Format(time.RFC3339),
}})
require.NoError(t, err)
req := httptest.NewRequest(http.MethodPost, "http://alertmanager/am/api/v2/alerts", bytes.NewReader(b))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
am.mux.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
stop := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-stop:
return
default:
}
rec := httptest.NewRecorder()
am.mux.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "http://alertmanager/am/api/v2/alerts", nil))
}
}()
for range 20 {
require.NoError(t, am.ApplyConfig(user, cfg, cfgRaw))
}
close(stop)
wg.Wait()
}
go test -race -tags "netgo slicelabels" -count=1 -v -run '^TestRaceReproApplyConfigVsGetAlerts$
=== RUN TestRaceReproApplyConfigVsGetAlerts
==================
WARNING: DATA RACE
Write at 0x00c000168228 by goroutine 42:
github.com/cortexproject/cortex/pkg/alertmanager.(*Alertmanager).ApplyConfig()
/Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/alertmanager.go:353 +0x41c
github.com/cortexproject/cortex/pkg/alertmanager.(*Alertmanager).ApplyConfig()
/Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/alertmanager.go:350 +0x2f8
github.com/cortexproject/cortex/pkg/alertmanager.TestRaceReproApplyConfigVsGetAlerts()
/Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/race_repro_test.go:79 +0xd18
net/http.HandlerFunc.ServeHTTP()
/opt/homebrew/Cellar/go/1.27.0/libexec/src/net/http/server.go:2338 +0x48
github.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerDuration.func1()
/Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/vendor/github.com/prometheus/client_golang/prometheus/promhttp/instrument_server.go:109 +0xc4
net/http.HandlerFunc.ServeHTTP()
/opt/homebrew/Cellar/go/1.27.0/libexec/src/net/http/server.go:2338 +0x48
github.com/prometheus/alertmanager/api.(*API).instrumentHandler.func1()
/Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/vendor/github.com/prometheus/alertmanager/api/api.go:240 +0x204
net/http.HandlerFunc.ServeHTTP()
/opt/homebrew/Cellar/go/1.27.0/libexec/src/net/http/server.go:2338 +0x48
net/http.(*ServeMux).ServeHTTP()
/opt/homebrew/Cellar/go/1.27.0/libexec/src/net/http/server.go:2903 +0x1a8
github.com/cortexproject/cortex/pkg/alertmanager.TestRaceReproApplyConfigVsGetAlerts()
/Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/race_repro_test.go:59 +0xacc
github.com/cortexproject/cortex/pkg/alertmanager.TestRaceReproApplyConfigVsGetAlerts()
/Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/race_repro_test.go:47 +0x4b0
sort.insertionSort()
Author
|
@friedrichg I could reproduce it and committed a fix |
Signed-off-by: Timon Engelke <timon.engelke@inovex.de>
timonegk
force-pushed
the
fix-alertmanager2
branch
from
December 4, 2026 10:08
1f3e568 to
0db6691
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does:
There is a regression in Cortex 1.22 that shows alerts as "unprocessed" in Grafana. This is due to an API breaking change in alertmanager 0.33.
Checklist
CHANGELOG.mdupdated - the order of entries should be[CHANGE],[FEATURE],[ENHANCEMENT],[BUGFIX]docs/configuration/v1-guarantees.mdupdated if this PR introduces experimental flags