Skip to content

Commit 54c399d

Browse files
phacopsclaude
andcommitted
fix: Handle StopIteration locally in rate limit functions
StopIteration cannot be re-raised through the rate_limit() generator due to PEP 479 (converted to RuntimeError). Handle it locally by failing open without logging, while keeping the allocation policy handlers for StopIteration from other code paths. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c17d211 commit 54c399d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

snuba/state/rate_limit.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,11 @@ def rate_limit_start_request(
277277
concurrent = sum(next(pipe_results) for _ in range(rate_limit_shard_factor))
278278
else:
279279
concurrent = 0
280-
except (RedisTimeoutError, StopIteration):
280+
except RedisTimeoutError:
281281
raise
282+
except StopIteration:
283+
# Unexpected pipeline result count — fail open without logging
284+
return RateLimitStats(rate=-1, concurrent=-1)
282285
except Exception as ex:
283286
# if something goes wrong, we don't want to block the request,
284287
# set the values such that they pass under any limit
@@ -315,8 +318,10 @@ def rate_limit_finish_request(
315318
pipe.zincrby(query_bucket, -float(max_query_duration_s), query_id)
316319
pipe.expire(query_bucket, max_query_duration_s)
317320
pipe.execute()
318-
except (RedisTimeoutError, StopIteration):
321+
except RedisTimeoutError:
319322
raise
323+
except StopIteration:
324+
pass
320325
except Exception as ex:
321326
logger.exception(ex)
322327

0 commit comments

Comments
 (0)