vmalert: per-notifier alert_relabel_configs mutate the shared label slice when a static_configs entry has several targets (data race, corrupted labels on the second Alertmanager)
Describe the bug
When -notifier.config has a static_configs entry with more than one target and alert_relabel_configs on that entry, the relabeling is applied concurrently by several goroutines on the same []prompb.Label backing array, so the second target intermittently receives partially relabeled label sets.
Code path (identical in v1.142.0, v1.153.0 and master):
app/vmalert/notifier/init.goSend()buildslblss [][]prompb.Labelonce (after the global relabel) and fans out to every active notifier withwg.Go(func() { nt.Send(ctx, alertsToSend, lblss, ...) })— every goroutine gets the same slices.app/vmalert/notifier/alertmanager.go(*AlertManager).send()doeslbls := alertLabels[i]; lbls = am.relabelConfigs.Apply(lbls, 0)without copying.lib/promrelabelmutates in place:setLabelValuewriteslabel.Valuethrough a pointer into the shared array,appendmay reuse its capacity,removeEmptyLabelscompacts in place.
With one target per static_configs entry the problem does not exist (each AlertManager has its own config, but the slice is still shared — two entries with different alert_relabel_configs would race as well).
Observed effect on a 2-replica Alertmanager HA pair: alerts on replica -1 carry values that no sequential pass of the chain can produce — a leading separator that the last rule strips (teams=",team-a" instead of team-a), temporary labels that a labeldrop rule removes (__tmp_platform="1" still present), a label set by a middle rule missing entirely. Replica -0 is always clean; the same alert exists there with correct labels. Because the label sets differ, the fingerprints differ, Alertmanager HA deduplication does not apply, and downstream receivers get duplicate (and mis-routed) notifications.
To Reproduce
notifier.yaml:
static_configs:
- targets:
- http://alertmanager-0:9093
- http://alertmanager-1:9093
alert_relabel_configs:
- source_labels: [teams]
target_label: teams
replacement: ${1},team-a
- source_labels: [teams]
regex: ".*team-a.*"
target_label: __tmp_owned
replacement: "1"
- source_labels: [teams, __tmp_owned]
separator: ";"
regex: "(.*);"
target_label: teams
replacement: ${1},team-b
- regex: __tmp_owned
action: labeldrop
- source_labels: [teams]
target_label: teams
action: lowercase
- source_labels: [teams]
regex: ",(.*)"
target_label: teams
replacement: ${1}Run vmalert with -notifier.config=notifier.yaml and a rule group with a few dozen firing alerts (a Watchdog-style vector(1) rule per namespace is enough). Compare GET /api/v2/alerts on both Alertmanagers: on one of them a subset of alerts has teams=",team-a" and/or __tmp_owned="1"; the other one is clean. The affected subset changes between evaluation rounds. Building vmalert with -race and running the same config should report the race in promrelabel.setLabelValue / removeEmptyLabels called from (*AlertManager).send.
Real-world sample (same alert, two replicas, one evaluation round):
| alertname | AM-0 teams |
AM-1 teams |
|---|---|---|
| Watchdog | team-a |
,team-a |
| InfoInhibitor | team-a |
,team-a |
| PostgresqlHighDeadTuples | team-b |
,team-b |
Across 14 clusters with 2 Alertmanager replicas: 0 malformed alerts on replica -0, 2–8 per round on replica -1; across 8 clusters with a single replica: 0.
Version
vmalert v1.142.0 (also verified the code path is unchanged in v1.153.0 and master).
Logs
No errors are logged; the corruption is silent.
Screenshots
n/a
Used command-line flags
-notifier.config=/etc/vm/notifier/notifier.yaml
-rule=/etc/vmalert/config/*/*.yamlAdditional information
Suggested fix — copy the labels before per-target relabeling in (*AlertManager).send:
lbls := alertLabels[i]
if am.relabelConfigs != nil {
lbls = am.relabelConfigs.Apply(append(make([]prompb.Label, 0, len(lbls)), lbls...), 0)
}Happy to open a PR with the change and a regression test if that shape is acceptable.
Related: #5980 / #9736 introduced per-notifier alert_relabel_configs; #11635 / #11639 fixed the reload of the global alert_relabel_configs (which is applied once before the fan-out and is therefore not affected by this race).
Source: VictoriaMetrics/VictoriaMetrics