Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions astrbot/core/tools/web_search_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ async def get(self, provider_settings: dict) -> str:
)

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
Comment on lines 75 to 80

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 key
References
  1. 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.

Expand Down