Fix: add asyncio timeout guard for DuckDuckGo search

DDGS's internal `timeout=10` relies on `requests` read-timeout semantics,
which only measure the gap between bytes — not total wall-clock time.
When the underlying HTTP connection enters CLOSE-WAIT or the server
dribbles data slowly, this timeout never fires, causing `ddgs.text` to
hang indefinitely via `asyncio.to_thread`.

Since `asyncio.to_thread` cannot cancel the underlying OS thread, the
agent's session lock is never released, blocking all subsequent messages
on the same session (observed: 8+ hours of unresponsiveness).

Fix:
- Add `timeout` field to `WebSearchConfig` (default: 30s, configurable
  via config.json or NANOBOT_TOOLS__WEB__SEARCH__TIMEOUT env var)
- Wrap `asyncio.to_thread` with `asyncio.wait_for` to enforce a hard
  wall-clock deadline

Closes #2804

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hoaresky 2026-04-05 09:12:49 +08:00 committed by Xubin Ren
parent 90caf5ce51
commit 6bd2950b99
2 changed files with 5 additions and 1 deletions

View File

@ -207,7 +207,10 @@ class WebSearchTool(Tool):
from ddgs import DDGS
ddgs = DDGS(timeout=10)
raw = await asyncio.to_thread(ddgs.text, query, max_results=n)
raw = await asyncio.wait_for(
asyncio.to_thread(ddgs.text, query, max_results=n),
timeout=self.config.timeout,
)
if not raw:
return f"No results for: {query}"
items = [

View File

@ -155,6 +155,7 @@ class WebSearchConfig(Base):
api_key: str = ""
base_url: str = "" # SearXNG base URL
max_results: int = 5
timeout: int = 30 # Wall-clock timeout (seconds) for search operations
class WebToolsConfig(Base):