From 79e66d975476127f4004bc4d1ede606b23838bf9 Mon Sep 17 00:00:00 2001 From: Deepanshu Date: Mon, 17 Aug 2026 12:31:12 -0400 Subject: [PATCH] fix(rate-limiting): stop leaving a permanent Redis key on an expired-reservation release Releasing a reservation whose key had already expired made INCRBY recreate it, and the floor-to-zero SET left that recreated key permanently in Redis (SET clears any TTL). Verified against a real Redis instance: DEL removes the key outright instead, which reads back identically to 0 everywhere this key is read. --- litellm/proxy/hooks/tag_rate_limiter.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/hooks/tag_rate_limiter.py b/litellm/proxy/hooks/tag_rate_limiter.py index 8b5425a137e..4906a6a841a 100644 --- a/litellm/proxy/hooks/tag_rate_limiter.py +++ b/litellm/proxy/hooks/tag_rate_limiter.py @@ -105,13 +105,17 @@ return { 1, new_value } # release a concurrency reservation -- floors at 0 so a decrement that can't # be attributed to the exact reservation that caused it (see # `_release_keys`'s docstring) degrades to under-counting rather than a -# negative counter that would admit unlimited requests. +# negative counter that would admit unlimited requests. Floors via DEL, not +# `SET key 0`: releasing a reservation whose key already expired makes +# INCRBY recreate it with no TTL, and a plain SET would leave that recreated +# key permanently in Redis (SET clears any TTL); DEL removes it outright, +# which reads back identically to 0 everywhere this key is read (`GET key or 0`). TAG_RL_DECR_FLOOR_ZERO_SCRIPT: Final = """ local key = KEYS[1] local delta = tonumber(ARGV[1]) local new_value = redis.call('INCRBY', key, delta) if new_value < 0 then - redis.call('SET', key, 0) + redis.call('DEL', key) new_value = 0 end return new_value