AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug] 运行多个 OneBot 适配器实例时出现不同 event loop 报错及任务堆积

Open muyni233 opened this issue 9 months ago • 6 comments

What happened / 发生了什么

在单个 Astrbot 实例中同时运行 4 个 OneBot 实例时,Bot 有时输出类似以下的异步逻辑报错: Error occurred while processing agent request: <Queue at 0x78842270 maxsize=5 _getters[5] tasks=38374> is bound to a different event loop

Reproduce / 如何复现?

  1. 部署 Astrbot
  2. 在 Astrbot 配置多个(4个) OneBot 实例并同时启用
  3. 将这些 Bot 加入到多个群聊
  4. 与 Bot 对话时,有时会输出报错

AstrBot version, deployment method (e.g., Windows Docker Desktop deployment), provider used, and messaging platform used. / AstrBot 版本、部署方式(如 Windows Docker Desktop 部署)、使用的提供商、使用的消息平台适配器

Astrbot Version v4.18.2 OneBot 适配器,通过 NapCat 连接 Python 3.13.10 Windows Server 2025 Standard | 版本号 24H2 | 版本26100.4061 通过源码部署,venv 安装依赖

OS

Windows

Logs / 报错日志

[2026-02-25 21:12:50] [DEBUG] [scheduler.py:93]: pipeline 执行完毕。
[2026-02-25 21:12:50] [DEBUG] [star_request.py:44]: plugin -> session_controller - handle_session_control_agent
[2026-02-25 21:12:50] [DEBUG] [star_request.py:44]: plugin -> session_controller - handle_empty_mention
[2026-02-25 21:12:50] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_recall_cancel - on_all_message
[2026-02-25 21:12:50] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_relationship - on_notice
[2026-02-25 21:12:50] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_relationship - on_request
[2026-02-25 21:12:50] [DEBUG] [star_request.py:44]: plugin -> spectrecore - on_group_message
[2026-02-25 21:12:51] [DEBUG] [star_request.py:44]: plugin -> session_controller - handle_session_control_agent
[2026-02-25 21:12:51] [DEBUG] [star_request.py:44]: plugin -> session_controller - handle_empty_mention
[2026-02-25 21:12:51] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_recall_cancel - on_all_message
[2026-02-25 21:12:51] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_relationship - on_notice
[2026-02-25 21:12:51] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_relationship - on_request
[2026-02-25 21:12:51] [DEBUG] [star_request.py:44]: plugin -> spectrecore - on_group_message
[2026-02-25 21:12:52] [DEBUG] [aiocqhttp_platform_adapter.py:129]: [aiocqhttp] RawMessage <Event, {'time': 1772025167, 'self_id': ******, 'post_type': 'notice', 'notice_type': 'group_recall', 'operator_id': ******, 'message_id': ******, 'group_id': ******, 'user_id': ******}>
[2026-02-25 21:12:52] [DEBUG] [aiocqhttp_platform_adapter.py:129]: [aiocqhttp] RawMessage <Event, {'time': 1772025167, 'self_id': ******, 'post_type': 'notice', 'notice_type': 'group_recall', 'operator_id': ******, 'message_id': ******, 'group_id': ******, 'user_id': ******}>
[2026-02-25 21:12:52] [DEBUG] [aiocqhttp_platform_adapter.py:129]: [aiocqhttp] RawMessage <Event, {'time': 1772025167, 'self_id': ******, 'post_type': 'notice', 'group_id': ******, 'user_id': ******, 'notice_type': 'group_recall', 'operator_id': ******, 'message_id': ******}>
[2026-02-25 21:12:52] [DEBUG] [aiocqhttp_platform_adapter.py:129]: [aiocqhttp] RawMessage <Event, {'time': 1772025167, 'self_id': ******, 'post_type': 'notice', 'group_id': ******, 'user_id': ******, 'notice_type': 'group_recall', 'operator_id': ******, 'message_id': ******}>
[2026-02-25 21:12:52] [DEBUG] [star_request.py:44]: plugin -> session_controller - handle_session_control_agent
[2026-02-25 21:12:52] [DEBUG] [star_request.py:44]: plugin -> session_controller - handle_empty_mention
[2026-02-25 21:12:52] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_recall_cancel - on_all_message
[2026-02-25 21:12:52] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_relationship - on_notice
[2026-02-25 21:12:52] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_relationship - on_request
[2026-02-25 21:12:52] [DEBUG] [star_request.py:44]: plugin -> spectrecore - on_group_message
[2026-02-25 21:12:53] [DEBUG] [internal.py:152]: ready to request llm provider
[2026-02-25 21:12:53] [DEBUG] [internal.py:158]: acquired session lock for llm request
[2026-02-25 21:12:53] [ERROR] [internal.py:337]: Error occurred while processing agent: <Queue at 0x78842270 maxsize=5 tasks=58815> is bound to a different event loop
[2026-02-25 21:12:53] [DEBUG] [star_request.py:44]: plugin -> session_controller - handle_session_control_agent
[2026-02-25 21:12:53] [DEBUG] [star_request.py:44]: plugin -> session_controller - handle_empty_mention
[2026-02-25 21:12:53] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_recall_cancel - on_all_message
[2026-02-25 21:12:53] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_relationship - on_notice
[2026-02-25 21:12:53] [DEBUG] [star_request.py:44]: plugin -> astrbot_plugin_relationship - on_request
[2026-02-25 21:12:53] [DEBUG] [star_request.py:44]: plugin -> spectrecore - on_group_message

Are you willing to submit a PR? / 你愿意提交 PR 吗?

  • [ ] Yes!

Code of Conduct

muyni233 avatar Feb 25 '26 13:02 muyni233

关注一下终端是否有更多的日志报错(不是webui控制台),如果有,可以发一下

Soulter avatar Mar 01 '26 07:03 Soulter

[18:26:28.719] [Core] [ERRO] [v4.18.3] [agent_sub_stages.internal:337]: Error occurred while processing agent: <Queue at 0x7881ecf0 maxsize=5 _getters[43] tasks=624183> is bound to a different event loop

Task exception was never retrieved
future: <Task finished name='Task-2043518' coro=<PipelineScheduler.execute() done, defined at E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\pipeline\scheduler.py:78> exception=TimeoutError('QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30.00')>
Traceback (most recent call last):
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\pipeline\scheduler.py", line 87, in execute
    await self._process_stages(event)
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\pipeline\scheduler.py", line 72, in _process_stages
    await coroutine
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\pipeline\waking_check\stage.py", line 228, in process
    activated_handlers = await SessionPluginManager.filter_handlers_by_session(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<2 lines>...
    )
    ^
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\star\session_plugin_manager.py", line 68, in filter_handlers_by_session
    session_plugin_config = await sp.get_async(
                            ^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
    )
    ^
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\utils\shared_preferences.py", line 51, in get_async
    result = await self.db_helper.get_preference(scope, scope_id, key)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\db\sqlite.py", line 986, in get_preference
    result = await session.execute(query)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\ext\asyncio\session.py", line 449, in execute
    result = await greenlet_spawn(
             ^^^^^^^^^^^^^^^^^^^^^
    ...<6 lines>...
    )
    ^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 201, in greenlet_spawn
    result = context.throw(*sys.exc_info())
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\session.py", line 2351, in execute
    return self._execute_internal(
           ~~~~~~~~~~~~~~~~~~~~~~^
        statement,
        ^^^^^^^^^^
    ...<4 lines>...
        _add_event=_add_event,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\session.py", line 2239, in _execute_internal
    conn = self._connection_for_bind(bind)
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\session.py", line 2108, in _connection_for_bind
    return trans._connection_for_bind(engine, execution_options)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in _connection_for_bind
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\state_changes.py", line 137, in _go
    ret_value = fn(self, *arg, **kw)
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\session.py", line 1187, in _connection_for_bind
    conn = bind.connect()
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\engine\base.py", line 3277, in connect
    return self._connection_cls(self)
           ~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\engine\base.py", line 143, in __init__
    self._dbapi_connection = engine.raw_connection()
                             ~~~~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\engine\base.py", line 3301, in raw_connection
    return self.pool.connect()
           ~~~~~~~~~~~~~~~~~^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\pool\base.py", line 447, in connect
    return _ConnectionFairy._checkout(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\pool\base.py", line 1264, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\pool\base.py", line 711, in checkout
    rec = pool._do_get()
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\pool\impl.py", line 166, in _do_get
    raise exc.TimeoutError(
    ...<4 lines>...
    )
sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/20/3o7r)

muyni233 avatar Mar 01 '26 10:03 muyni233

今天发现了一段与上次略有不同的 log,或许与上次的有些关联

Task exception was never retrieved
future: <Task finished name='Task-11174' coro=<PipelineScheduler.execute() done, defined at E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\
astrbot\core\pipeline\scheduler.py:78> exception=RuntimeError('<Queue at 0x78803620 maxsize=5 _getters[9] tasks=4145> is bound to a different event loop')>
Traceback (most recent call last):
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\pipeline\scheduler.py", line 87, in execute
    await self._process_stages(event)
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\pipeline\scheduler.py", line 61, in _process_stages
    await self._process_stages(event, i + 1)
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\pipeline\scheduler.py", line 52, in _process_stages
    async for _ in coroutine:
    ...<15 lines>...
            break
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\pipeline\result_decorate\stage.py", line 256, in process
    tts_provider = self.ctx.plugin_manager.context.get_using_tts_provider(
        event.unified_msg_origin,
    )
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\star\context.py", line 382, in get_using_tts_provider
    prov = self.provider_manager.get_using_provider(
        provider_type=ProviderType.TEXT_TO_SPEECH,
        umo=umo,
    )
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\provider\manager.py", line 228, in get_using_provider
    provider_id = sp.get(
        f"provider_perf_{provider_type.value}",
    ...<2 lines>...
        scope_id=umo,
    )
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\utils\shared_preferences.py", line 187, in get
    ).result()
      ~~~~~~^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\concurrent\futures\_base.py", line 456, in result
    return self.__get_result()
           ~~~~~~~~~~~~~~~~~^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\concurrent\futures\_base.py", line 401, in __get_result
    raise self._exception
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\utils\shared_preferences.py", line 51, in get_async
    result = await self.db_helper.get_preference(scope, scope_id, key)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\mcsmanager\daemon\data\InstanceData\9e89d7df54b54675ae63ba0233446c84\astrbot\core\db\sqlite.py", line 1002, in get_preference
    result = await session.execute(query)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\ext\asyncio\session.py", line 449, in execute
    result = await greenlet_spawn(
             ^^^^^^^^^^^^^^^^^^^^^
    ...<6 lines>...
    )
    ^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 201, in greenlet_spawn
    result = context.throw(*sys.exc_info())
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\session.py", line 2351, in execute
    return self._execute_internal(
           ~~~~~~~~~~~~~~~~~~~~~~^
        statement,
        ^^^^^^^^^^
    ...<4 lines>...
        _add_event=_add_event,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\session.py", line 2239, in _execute_internal
    conn = self._connection_for_bind(bind)
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\session.py", line 2108, in _connection_for_bind
    return trans._connection_for_bind(engine, execution_options)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in _connection_for_bind
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\state_changes.py", line 137, in _go
    ret_value = fn(self, *arg, **kw)
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\orm\session.py", line 1187, in _connection_for_bind
    conn = bind.connect()
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\engine\base.py", line 3277, in connect
    return self._connection_cls(self)
           ~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\engine\base.py", line 143, in __init__
    self._dbapi_connection = engine.raw_connection()
                             ~~~~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\engine\base.py", line 3301, in raw_connection
    return self.pool.connect()
           ~~~~~~~~~~~~~~~~~^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\pool\base.py", line 447, in connect
    return _ConnectionFairy._checkout(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\pool\base.py", line 1264, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\pool\base.py", line 711, in checkout
    rec = pool._do_get()
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\pool\impl.py", line 156, in _do_get
    return self._pool.get(wait, self._timeout)
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\util\queue.py", line 311, in get
    return self.await_(
           ~~~~~~~~~~~^
        asyncio.wait_for(self._queue.get(), timeout)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 132, in await_only
    return current.parent.switch(awaitable)  # type: ignore[no-any-return,attr-defined] # noqa: E501
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 196, in greenlet_spawn
    value = await result
            ^^^^^^^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\asyncio\tasks.py", line 507, in wait_for
    return await fut
           ^^^^^^^^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\asyncio\queues.py", line 183, in get
    getter = self._get_loop().create_future()
             ~~~~~~~~~~~~~~^^
  File "C:\Users\Server\AppData\Local\Programs\Python\Python313\Lib\asyncio\mixins.py", line 20, in _get_loop
    raise RuntimeError(f'{self!r} is bound to a different event loop')
RuntimeError: <Queue at 0x78803620 maxsize=5 _getters[9] tasks=4145> is bound to a different event loop
[20:19:03.816] [Core] [ERRO] [v4.19.2] [agent_sub_stages.internal:371]: Error occurred while processing agent: <Queue at 0x78803620 maxsize=5 _getters[14] tasks=414
7> is bound to a different event loop

muyni233 avatar Mar 05 '26 12:03 muyni233

请问你的python版本是多少

whatevertogo avatar Mar 05 '26 13:03 whatevertogo

问题描述: 在单个 AstrBot 实例中同时运行多个 OneBot 实例时,出现以下错误:
Error occurred while processing agent: <Queue at 0x78842270 maxsize=5
tasks=38374> is bound to a different event loop

根本原因分析:

  1. session_lock.py 核心问题: asyncio.Lock() 在模块级别的单例中创建,会绑定到创建时的 event loop。如果后续在不同的 event
    loop 中使用,会触发 "bound to a different event loop" 错误。
  2. main.py 多次 asyncio.run(): 第 121 行和第 130 行使用了两次 asyncio.run(),每次调用创建不同的 event loop。
  3. 已弃用的 API: 多个文件使用了 asyncio.get_event_loop(),在 Python 3.10+
    中可能导致问题。

whatevertogo avatar Mar 05 '26 13:03 whatevertogo

请问你的python版本是多少

3.13.10

muyni233 avatar Mar 05 '26 13:03 muyni233