fix: _KeyRotator index bounds check#9040
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a bounds check on self.index before accessing the keys list to prevent potential out-of-bounds errors. The reviewer pointed out that the async with self.lock: block is redundant because the synchronous code within it executes atomically in a single-threaded asyncio event loop, and suggested removing the lock to simplify the code and avoid unnecessary overhead.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| async with self.lock: | ||
| if self.index >= len(keys): | ||
| self.index = 0 | ||
| key = keys[self.index] | ||
| self.index = (self.index + 1) % len(keys) | ||
| return key |
There was a problem hiding this comment.
In a single-threaded asyncio event loop, synchronous code blocks (those without any await statements) are executed atomically and will not be interrupted by other coroutines. Therefore, the async with self.lock: block is redundant and can be safely removed to simplify the code and avoid unnecessary locking overhead.
if self.index >= len(keys):
self.index = 0
key = keys[self.index]
self.index = (self.index + 1) % len(keys)
return keyReferences
- In a single-threaded asyncio event loop, synchronous functions (code blocks without 'await') are executed atomically and will not be interrupted by other coroutines. Therefore, they are safe from race conditions when modifying shared state within that block.
[Bug] _KeyRotator IndexError when key list shrinks
What
_KeyRotator.get()inastrbot/core/tools/web_search_tools.pyraisesIndexErrorwhen the configured key list has fewer entries than the internalindexcounter expects.Same bug affects all four rotators:
_TAVILY_KEY_ROTATOR,_BOCHA_KEY_ROTATOR,_BRAVE_KEY_ROTATOR,_FIRECRAWL_KEY_ROTATOR.Why
_KeyRotatoris a module-level singleton.self.indexpersists across calls but only resets on bot restart. If someone configures 2 keys (index advances to 1), then later removes one (1 key left), theif not keysguard passes butkeys[self.index]still blows up.Fix
One-line bounds check before accessing
keys[self.index]:Tested
index=1, keys=["a"]→ was IndexError, now returns "a" and resets index to 0Closes #9039
Summary by Sourcery
Bug Fixes: