Skip to content

fix(tools): wrap coroutines in create_task() in _run_async() to fix aiohttp timeout error - #38525

Closed
goodtiding5 wants to merge 1 commit into
NousResearch:mainfrom
goodtiding5:fix/run-async-create-task
Closed

fix(tools): wrap coroutines in create_task() in _run_async() to fix aiohttp timeout error#38525
goodtiding5 wants to merge 1 commit into
NousResearch:mainfrom
goodtiding5:fix/run-async-create-task

Conversation

@goodtiding5

Copy link
Copy Markdown

Wrap the coroutine in loop.create_task() before passing it to run_until_complete() in all three code paths of _run_async(). This ensures asyncio.current_task() returns a valid task, which aiohttp's TimerContext.__enter__() requires on Python 3.11+.

Without this, when aiohttp creates a TimerContext during an HTTP request, it calls asyncio.current_task() which returns None because run_until_complete() schedules the coroutine at the event loop's top level without creating an asyncio Task. This raises:

RuntimeError: Timeout context manager should be used inside a task

Affects any synchronous tool handler that bridges to async via _run_async() and uses aiohttp with ClientTimeout — including send_message for Mattermost, Matrix, Email, SMS, Bluebubbles, QQbot, HomeAssistant, DingTalk, Feishu, WeCom, and Yuanbao platforms.

The Weixin adapter previously worked around this per-platform (#29037) by disabling aiohttp's ClientTimeout and using asyncio.wait_for() instead. This fix addresses the root cause, resolving it for all platforms at once.

Fixes #37005

…iohttp timeout error

Wrap the coroutine in loop.create_task() before passing it to
run_until_complete() in all three code paths of _run_async(). This
ensures asyncio.current_task() returns a valid task, which aiohttp's
TimerContext.__enter__() requires on Python 3.11+.

Without this, when aiohttp creates a TimerContext during an HTTP
request, it calls asyncio.current_task() which returns None because
run_until_complete() schedules the coroutine at the event loop's top
level without creating an asyncio Task. This raises:

    RuntimeError: Timeout context manager should be used inside a task

Affects any synchronous tool handler that bridges to async via
_run_async() and uses aiohttp with ClientTimeout — including
send_message for Mattermost, Matrix, Email, SMS, Bluebubbles, QQbot,
HomeAssistant, DingTalk, Feishu, WeCom, and Yuanbao platforms.

The Weixin adapter previously worked around this per-platform
(NousResearch#29037) by disabling aiohttp's ClientTimeout and using
asyncio.wait_for() instead. This fix addresses the root cause,
resolving it for all platforms at once.

Refs: NousResearch#37005 (Mattermost)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tools Tool registry, model_tools, toolsets P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mattermost plugin: send_message fails with 'Timeout context manager should be used inside a task' (v0.15.x)

2 participants