[🐛 Bug]: KeepProvider mutates the shared fingerprint_fields list across alerts in a foreach batch, corrupting fingerprints
Describe the bug
KeepProvider._build_alert mutates the caller's fingerprint_fields list in place instead of treating it as read-only:
# keep/providers/keep_provider/keep_provider.py, line 177
fingerprint_fields.append("workflowId")
_notify_alert loops over every alert in a batch (foreach, or multiple items matching an if condition) and passes that same list object into _build_alert on every iteration instead of a fresh copy:
# keep/providers/keep_provider/keep_provider.py, lines 549-570
for alert_result in trigger_alerts:
...
alert_dto = self._build_alert(
alert_result, fingerprint_fields or [], **rendered_alert_data
)
So if a workflow's keep - create-alert action sets an explicit fingerprint_fields and produces more than one alert per invocation, every alert after the first in that batch gets an extra duplicate "workflowId" appended to its fingerprint fields, which changes the resulting SHA-256 fingerprint.
To Reproduce
With a real workflow:
- A step whose result is a list of 2+ items (or a
foreachover 2+ items). - A
keep - create-alertaction on that step with an explicitfingerprint_fields, e.g.:
actions:
- name: create-alert
foreach: "{{ steps.query.results }}"
provider:
type: keep
with:
fingerprint_fields: ["labels.service"]
name: "disk full"
labels:
service: "{{ foreach.value.service }}"
- Run it with 2+ results in one execution and compare the computed fingerprints — the 2nd, 3rd, etc. alerts get a different (wrong) fingerprint than the 1st, even though they should all be computed the same way (just with different field values).
To make this concrete without needing the full app/DB stack, here's a small script that copies the exact control flow above verbatim (including the real hashing logic from BaseProvider.get_alert_fingerprint) and runs standalone:
repro_fingerprint_mutation.py
import hashlib
def get_alert_fingerprint(event_dict, fingerprint_fields):
"""Verbatim logic of BaseProvider.get_alert_fingerprint."""
if not fingerprint_fields:
return event_dict["name"]
fingerprint = hashlib.sha256()
for fingerprint_field in fingerprint_fields:
keys = fingerprint_field.split(".")
value = event_dict
for key in keys:
if isinstance(value, dict):
value = value.get(key, None)
else:
value = None
break
if value is not None:
fingerprint.update(str(value).encode())
return fingerprint.hexdigest()
def build_alert(alert_data, fingerprint_fields, workflow_id, name, labels):
"""Verbatim logic of KeepProvider._build_alert (lines 139-179)."""
event_dict = {"name": name, "labels": labels, "workflowId": workflow_id}
if not fingerprint_fields:
fingerprint_fields = ["labels." + label for label in list(labels.keys())]
fingerprint_fields.append("workflowId") # <-- mutates the caller's list in place
event_dict["fingerprint"] = get_alert_fingerprint(event_dict, fingerprint_fields)
return event_dict
def notify_alert(trigger_alerts, fingerprint_fields, workflow_id):
"""Verbatim logic of KeepProvider._notify_alert's per-alert loop (lines 549-570)."""
alert_dtos = []
for alert_result in trigger_alerts:
alert_dto = build_alert(
alert_result,
fingerprint_fields or [], # <-- same list object passed every iteration
workflow_id=workflow_id,
name=alert_result["name"],
labels=alert_result.get("labels", {}),
)
alert_dtos.append(alert_dto)
return alert_dtos
if __name__ == "__main__":
workflow_configured_fingerprint_fields = ["labels.service"]
alerts_from_one_query = [
{"name": "disk full", "labels": {"service": "db-primary"}},
{"name": "disk full", "labels": {"service": "db-replica"}},
{"name": "disk full", "labels": {"service": "cache"}},
]
results = notify_alert(
alerts_from_one_query, workflow_configured_fingerprint_fields, workflow_id="wf-123"
)
for i, r in enumerate(results, start=1):
print(f"alert {i} ({r['labels']['service']}): fingerprint = {r['fingerprint']}")
print()
print("fingerprint_fields after the run:", workflow_configured_fingerprint_fields)
correct = get_alert_fingerprint(
{"name": "disk full", "labels": {"service": "cache"}, "workflowId": "wf-123"},
["labels.service", "workflowId"],
)
print()
print("alert 3 fingerprint if fingerprint_fields had NOT been mutated:", correct)
print("alert 3 fingerprint actually produced: ", results[2]["fingerprint"])
Output:
alert 1 (db-primary): fingerprint = 0663a28c9b4b51c01b0b670aa8527b92d0f6c07072dd1d713dd13ee7a466600a
alert 2 (db-replica): fingerprint = 5b9f6e023a3676a55c7710ba7486d1b4e7e3e9c5292f1af7c139d5c563c4f1f8
alert 3 (cache): fingerprint = 953d8db96249397626403dacfaa7da39847eca6fda9598f17510dc0c1e017275
fingerprint_fields after the run: ['labels.service', 'workflowId', 'workflowId', 'workflowId']
alert 3 fingerprint if fingerprint_fields had NOT been mutated: 8e0bf2924725d79fc3532aef597e485ee73d14d7f7711b5514bef2e2f32c83c2
alert 3 fingerprint actually produced: 953d8db96249397626403dacfaa7da39847eca6fda9598f17510dc0c1e017275
Only 3 alerts were processed, and the shared list already grew from 1 entry to 4 (3 duplicate "workflowId"s), and alert 3's fingerprint doesn't match what it should be.
Expected behavior
Each alert in a batch should get its own independent copy of fingerprint_fields, so identically-configured alerts that only differ in field values get fingerprints computed the same, deterministic way — not one that depends on how many alerts were processed earlier in the same batch.
Additional context
- Verified against
upstream/mainat4003992272eeddd649e23e21d63f09892a149291. - If the same
fingerprint_fieldslist object also happens to be reused across separate workflow executions (rather than freshly built per run), this list would keep growing indefinitely across runs too — I didn't chase that part down since the single-invocation case above is already enough to demonstrate the bug. - Small fix: stop mutating the caller's list, e.g.
fingerprint_fields = fingerprint_fields + ["workflowId"]instead offingerprint_fields.append("workflowId")in_build_alert(keep_provider.py:177). Happy to send a PR for this if useful.
@Prabal864 great bug report — this is confirmed by the source. At line 177, fingerprint_fields.append("workflowId") mutates the list in place, and in _notify_alert the same list object is passed to _build_alert on every iteration without being copied. So "workflowId" accumulates as a duplicate for every alert after the first in a batch.
The fix you proposed is the right one — change line 177 from:
fingerprint_fields.append("workflowId")
to:
fingerprint_fields = fingerprint_fields + ["workflowId"]
This creates a new list local to _build_alert on each call and leaves the caller's list untouched. There's also the secondary issue you noted: _build_alert's default argument fingerprint_fields=[] on line 139 is a mutable default, which is another classic Python pitfall — though in practice the fingerprint_fields or [] expression on line 569 masks it for now.
A PR for this would be very welcome!
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
Went with exactly that fix — opened a PR: #6720.