Repository navigation
fix: prevent duplicate budget alert emails on concurrent threshold crossings - #32011
Conversation
Greptile SummaryThis PR fixes duplicate budget alert emails under concurrent threshold crossings by replacing the read-check-send-write dedup with an atomic
Confidence Score: 5/5Safe to merge; the atomic increment correctly prevents duplicate sends across all three alert branches, and the new All three previously identified blocking issues (unguarded No files require special attention.
|
| Filename | Overview |
|---|---|
| enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.py | Replaces the check-then-send-then-mark dedup pattern with atomic async_increment_cache across all three budget-alert branches; adds _release_budget_alert_claim helper that wraps async_delete_cache in its own try/except, eliminating the previously reported unguarded delete path. |
| tests/test_litellm/enterprise/enterprise_callbacks/send_emails/test_base_email.py | All existing mock setups updated from async_get_cache/async_set_cache to async_increment_cache; adds three new parametrised tests covering concurrent-crossing dedup, failed-send claim release, and cache-delete failure propagation across all three alert branches. |
Reviews (4): Last reviewed commit: "fix: harden budget alert claim release a..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…ossings Budget alert emails were sent more than once for a single threshold crossing. The email dedup guard read the "already sent" marker, awaited the send, then wrote the marker, so concurrent requests crossing the same threshold within the send window all saw no marker and each sent. This affected the multi-threshold path (default_key_max_budget_alert_emails), the legacy single-threshold path (EMAIL_BUDGET_ALERT_MAX_SPEND_ALERT_PERCENTAGE), and the soft budget path, all in EmailBaseCallback.budget_alerts All three branches now claim the send slot atomically before sending via async_increment_cache, which is atomic per event loop for the in-memory cache and across workers via Redis INCR; only the caller that observes a count of 1 sends. On send failure the marker is released with async_delete_cache so a transient failure does not suppress the alert for the full 24h TTL
d3f1ebc to
850d26f
Compare
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 850d26f. Configure here.
1 similar comment
Addresses review feedback on the claim-before-send change. The claim release in each send-failure handler now logs the send error first and releases the claim best-effort through a shared helper, so a transient cache error during async_delete_cache cannot propagate out of the fire-and-forget budget_alerts task, drop the send-failure log, and leave the claim stuck for the full 24h TTL. In the multi-threshold branch the increment claim now runs before the WebhookEvent is built, so skipped concurrent crossings no longer construct and discard the event, matching the single-threshold and soft budget branches
639e183 to
4d7cffc
Compare
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 4d7cffc. Configure here.
The dedup claim added in #32011 lives in DualCache, which is memory-only when Redis is not configured, so each replica wins its own claim and sends its own copy of the same alert. It is also lost on restart and expires with EMAIL_BUDGET_ALERT_TTL, so a long-crossed threshold keeps re-alerting. Record the claim in a new LiteLLM_BudgetAlertSent table instead. The unique constraint makes the insert an atomic cross-replica claim, and the stored budget window re-arms the alert when the budget period resets or max_budget changes rather than on a timer. Falls open when the claim cannot be recorded. Resolves LIT-4172
Relevant issues
Linear ticket
Resolves LIT-4172
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewScreenshots / Proof of Fix
Budget alert emails were being delivered more than once for a single threshold crossing. Reproduced live against a proxy with Postgres, Redis (shared dedup cache, as in a multi-replica deployment), and an SMTP server, hitting real
openai/gpt-4.1-mini. A local SMTP catcher counts delivered alert emails; it holds each connection open briefly to model a real mail server and widen the send window.Config (
config.yaml) matches the reported setup:Launched with
SMTP_HOST/SMTP_PORTpointed at the catcher andSMTP_TLS=False.Steps (identical against unfixed and fixed code):
Before (unfixed) - three duplicate emails for one 50% crossing, seconds apart:
After (fixed) - exactly one email for the same burst:
The shared Redis marker after the fixed run confirms all eight concurrent requests reached the dedup guard, yet only the one that atomically claimed the slot sent:
A repeat burst within the alert TTL still delivers no further emails, so dedup persists as before.
Type
🐛 Bug Fix
Changes
EmailBaseCallback.budget_alertsguarded against duplicate sends by reading an "already sent" cache marker, awaiting the email send, then writing the marker. Because the marker was written only after theawait, two requests that crossed the same threshold within the send window both read no marker and both sent. The same shape existed in all three branches of the method: the multi-threshold path (default_key_max_budget_alert_emails), the legacy single-threshold path (EMAIL_BUDGET_ALERT_MAX_SPEND_ALERT_PERCENTAGE), and the soft budget path. Redis narrowed but did not close the window because the write still landed after the send.All three branches now claim the send slot atomically before sending with
async_increment_cache, the same primitive the alerting code already uses for its counters. The increment is atomic per event loop for the in-memory cache and across workers via RedisINCR, so only the caller that observes a count of 1 proceeds to send; concurrent callers observe a higher count and return. Therefresh_ttl=Falsedefault preserves the 24h window, keyed per entity and threshold exactly as before. On send failure the marker is released withasync_delete_cacheso a transient email backend error does not suppress the alert for the full TTL, matching the previous behavior where a failed send left no marker.The live proof above exercises the multi-threshold path end to end. The single-threshold and soft budget paths share the identical mechanism and are each covered by concurrent-crossing and failed-send regression tests (parametrized over all three branches) that fail when the fix is reverted.
The Slack alert channel (
SlackAlerting.budget_alerts) has an analogous get-send-set guard for its own dedup. That is a separate subsystem from the reported email symptom and is tracked as a follow-up on LIT-4172 rather than changed here, to keep this PR scoped to the one bugNote
Medium Risk
Touches enterprise alerting dedup semantics under Redis/multi-replica concurrency; behavior change is intentional but mis-increment or release logic could suppress or duplicate alerts.
Overview
Fixes duplicate budget alert emails when many requests cross the same spend threshold at once (LIT-4172).
BaseEmailLogger.budget_alertsno longer uses read-cache → send → write"SENT". Soft budget, legacy max-budget, and multi-threshold max-budget paths nowasync_increment_cacheon the same TTL keys before sending; only the caller that sees count ≤ 1 sends. Failed sends call new_release_budget_alert_claim(async_delete_cache) so a transient SMTP error does not block retries for the full TTL.Unit tests mock
async_increment_cacheinstead of get/set; new parametrized tests cover concurrent crossing (exactly one send) and failed-send retry across all three branches.Reviewed by Cursor Bugbot for commit 4d7cffc. Bugbot is set up for automated code reviews on this repo. Configure here.