AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug] Astrbot + QQ官方机器人组合长时间运行后,用户会单方向和Bot断联

Open DFGHJ43 opened this issue 2 weeks ago • 4 comments

问题描述

运行Astrbot加QQ官方机器人,大概30个小时左右用户会单方向和Bot断联,此时用户向bot发消息会提示“机器人的灵魂不在线”,但Bot的主动消息类插件仍然能向用户发送消息,此时日志里反复出现[QQOfficial] Websocket session starting. 但是starting不起来,用户主动在dashboard-机器人-启用 开关手动重启机器人平台后恢复,但大概30-40小时后又会原样复发。运行一百小时内触发了三次

出错时不断尝试 第二次单方向失联-1113-0103 第三次失联 第一次失联

如何复现?

使用uv tool install 部署并启动 接入QQ官方机器人 等待或正常使用30-40小时 问题出现

AstrBot 版本

4.29.4

操作系统

Linux

部署方式

uv tool install, Fedora45, python 3.15.2, uv 0.13.5

使用的消息平台适配器

QQ官方机器人(WebSocket)

错误日志

问题出现时没开Debug级别日志,只有info 级

[2026-11-12 23:25:36.288] [astrbot_plugin_fish_heart] [INFO] [astrbot_plugin_fish_heart.main:4719]: [鱼的心] 主动说话已发送(想他):喂,你会不会嫌我话多啊?有点想你了,想让你坐过来陪我说说话。
[2026-11-12 23:26:14.296] [Core] [INFO] [core.event_bus:155]: [default] [default(qq_official)] 13E646FD25BD142269F2B12BBA419986: [At:qq_official] 话多不觉得,肉麻是真的有点
[2026-11-12 23:26:15.589] [astrbot_plugin_topic_router_pro] [INFO] [astrbot_plugin_topic_router_pro.router:1370]: [astrbot_plugin_topic_router_pro] hook_llm_request_v017 被调用 | v0.8.6 | has_event=True | has_request=True | args=['QQOfficialMessageEvent', 'ProviderRequest'] | kwargs=[]
[2026-11-12 23:26:16.990] [astrbot_plugin_topic_router_pro] [INFO] [astrbot_plugin_topic_router_pro.router:868]: [astrbot_plugin_topic_router_pro] CONTINUE | default:FriendMessage:13E646FD25BD142269F2B12BBA419986 | confidence=0.55 | source=llm | topic=考试与考后心情 | msg=话多不觉得,肉麻是真的有点 | 延续闲聊的吐槽回应,未开启独立新话题(启发式:距上一条消息已过 73 分钟(阈值 45):时间长只是弱信号,交模型判下一句是否延续) | {'heuristic': 'continue', 'heuristic_confidence': 0.5, 'needs_llm': True, 'llm_used': True, 'mode': 'mark', 'has_reply': False, 'cost_ms': 1400}
[2026-11-12 23:26:17.023] [astrbot_plugin_LLMPerception] [INFO] [astrbot_plugin_LLMPerception.main:430]: 已添加感知信息: 发送时间: 2026-11-12 23:26:17 | 周四, 工作日, 深夜 | 平台: qq_official, 私聊
[2026-11-12 23:26:17.053] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [主链] 已约束被动回复的 send_message_to_user 仅用于媒体投递: session=default:FriendMessage:13E646FD25BD142269F2B12BBA419986
[2026-11-12 23:26:17.141] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [LLM工具动作] [ReactionExpression] {"trace_id":"29f43af92771","stage":"authorization","decision":"scoped","reason":"media_tools_scoped","scope":"private"}
[2026-11-12 23:26:17.142] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [被动状态管线] 已注入被动状态提示词到 default:FriendMessage:13E646FD25BD142269F2B12BBA419986: mode=light state_mode=delta reason=changed placement=system_prompt+extra_user_content_parts chars=2035 状态=心理能量=75/100|情绪底色=平稳;当前日程=(暂无);已核实当前活动=(暂无);当前计划时段=(暂无)
[2026-11-12 23:26:17.143] [astrbot_plugin_fish_heart] [INFO] [astrbot_plugin_fish_heart.main:5729]: [鱼的心][影响] {"ts": "2026-11-12 23:26:17", "channel": "private", "source": "owner", "speaker": "13E646FD25BD142269F2", "sender_id": "13E646FD25BD142269F2B12BBA419986", "is_owner": true, "bound_owner": true, "text": "话多不觉得,肉麻是真的有点", "weight": 1.0, "events": ["reply_fast", "user_msg"], "events_cn": ["及时回", "说话"], "event_log": [{"event": "reply_fast", "event_cn": "及时回", "applied": {"attachment": 0.9003350391021598, "stress": -3.6798123093980344, "intimacy": 0.5710418685255689, "expectation": -40.0, "joy": 1.556270301683746}}, {"event": "user_msg", "event_cn": "说话", "applied": {"social": 0.7749422128142243, "joy": 0.3735048724040979, "curiosity": 0.21001233042183998, "fatigue": 0.520454505849937, "intimacy": 0.02798105155775943}}], "skipped": [], "thought_settled": {"kind": "praise", "mood": "被他夸了,心里甜甜的"}, "woken": false, "negative": false, "before": {"blame": 0.0, "expectation": 40.0, "mode": "calm", "mood": "黏人", "desire": 0.0, "desire_awakened": false, "drives": {"attachment": 77.49, "curiosity": 58.0, "reflection": 50.18, "duty": 50.03, "social": 56.95, "fatigue": 26.52, "intimacy": 71.45, "stress": 3.68, "joy": 61.09}}, "after": {"blame": 0.0, "expectation": 0.0, "mode": "calm", "mood": "黏人", "desire": 0.0, "desire_awakened": false, "drives": {"attachment": 79.39, "curiosity": 58.21, "reflection": 50.18, "duty": 50.03, "social": 57.72, "fatigue": 27.04, "intimacy": 75.05, "stress": 0.0, "joy": 68.02}}, "applied_total": {"blame": 0.0, "expectation": -40.0, "desire": 0.0, "drives": {"attachment": 1.9, "curiosity": 0.21, "social": 0.77, "fatigue": 0.52, "intimacy": 3.6, "stress": -3.68, "joy": 6.93}}, "turn_mode_before": "calm", "turn_mode_after": "calm"}
[2026-11-12 23:26:23.493] [astrbot_plugin_memory_companion] [INFO] [core.service:5045]: [MemoryCompanion] 已补齐记忆向量索引: provider=Qwen/Qwen3-VL-Embedding-8B count=1
[2026-11-12 23:26:23.787] [astrbot_plugin_memory_companion] [INFO] [core.service:10027]: [MemoryCompanion] 已注入当前轮记忆: session=default:FriendMessage:13E646FD25BD142269F2B12BBA419986 snapshot=context:c99b9cefd826:9ab03c2f0d5a1d63 count=3 chars=1421
[2026-11-12 23:26:23.809] [astrbot_plugin_memory_companion] [INFO] [core.service:7018]: [MemoryCompanion] recall guidance offered: turn=69d93e1cfcbf002655f209a950a3fcaf592e03ece04175ea25f251aee77b6667 request_tools_at_memory_hook=memory_companion_events,memory_companion_navigate,memory_companion_query,memory_companion_recall,memory_companion_sources
[2026-11-12 23:26:23.840] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [回复持久化] [SendTracking] send wrapper installed: event=140409368125008
[2026-11-12 23:26:23.841] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [回复持久化] [SendTracking] send_streaming wrapper installed: event=140409368125008
[2026-11-12 23:26:25.438] [meme_manager] [INFO] [mixins.event_handlers:2101]: [meme_manager] 去重后的最终表情列表: []
[2026-11-12 23:26:26.127] [Core] [INFO] [respond.stage:237]: Prepare to send - /13E646FD25BD142269F2B12BBA419986: 嗯,那我把肉麻的收走了,免得你一晚上不自在。 我是在理抽屉,结果越理越乱,算了。
[2026-11-12 23:26:31.047] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [回复持久化] 已将实际发送回复交给 AstrBot 核心保存: default:FriendMessage:13E646FD25BD142269F2B12BBA419986
[2026-11-12 23:58:38.384] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:11:17.175] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:15:29.180] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:21:05.180] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:21:10.269] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:21:15.301] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:21:20.540] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:21:26.450] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:21:31.554] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:21:36.752] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 00:21:41.785] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.

40 minutes later……

[2026-11-13 01:01:21.521] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:01:26.550] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:01:31.581] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:01:39.732] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:01:46.674] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:01:51.706] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:01:56.736] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:02:01.785] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:02:07.050] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:02:10.929] [Core] [WARN] [v4.29.4] [sources.request_retry:68]: [OpenAI] Request failed with retryable error; retrying (2/5): APITimeoutError: Request timed out.
[2026-11-13 01:02:12.081] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:02:13.782] [Core] [WARN] [v4.29.4] [sources.request_retry:68]: [OpenAI] Request failed with retryable error; retrying (3/5): APIConnectionError: Connection error.
[2026-11-13 01:02:16.028] [Core] [WARN] [v4.29.4] [sources.request_retry:68]: [OpenAI] Request failed with retryable error; retrying (4/5): APIConnectionError: Connection error.
[2026-11-13 01:02:18.803] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:02:20.046] [Core] [WARN] [v4.29.4] [sources.request_retry:68]: [OpenAI] Request failed with retryable error; retrying (5/5): APIConnectionError: Connection error.
[2026-11-13 01:02:23.840] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:02:33.612] [astrbot_plugin_fish_heart] [INFO] [astrbot_plugin_fish_heart.main:4719]: [鱼的心] 主动说话已发送(想他):你还不困吧?嗯……今天就是有点想让你陪着,坐一会儿就好。
[2026-11-13 01:04:30.938] [Core] [INFO] [platform.manager:300]: Attempting to terminate platform adapter default ...
[2026-11-13 01:04:30.939] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:1092]: QQ 官方机器人接口 适配器已被关闭 (此处人工重启机器人)
[2026-11-13 01:04:34.652] [Core] [INFO] [platform.manager:144]: Loading IM platform adapter qq_official(default) ...
[2026-11-13 01:04:35.440] [Core] [INFO] [qqofficial.qqofficial_platform_adapter:304]: [QQOfficial] Websocket session starting.
[2026-11-13 01:04:43.612] [Core] [INFO] [core.event_bus:155]: [default] [default(qq_official)] 13E646FD25BD142269F2B12BBA419986: [At:qq_official] 在吗
[2026-11-13 01:04:44.935] [astrbot_plugin_topic_router_pro] [INFO] [astrbot_plugin_topic_router_pro.router:1370]: [astrbot_plugin_topic_router_pro] hook_llm_request_v017 被调用 | v0.8.6 | has_event=True | has_request=True | args=['QQOfficialMessageEvent', 'ProviderRequest'] | kwargs=[]
[2026-11-13 01:04:46.130] [astrbot_plugin_topic_router_pro] [INFO] [astrbot_plugin_topic_router_pro.router:868]: [astrbot_plugin_topic_router_pro] CONTINUE | default:FriendMessage:13E646FD25BD142269F2B12BBA419986 | confidence=0.60 | source=llm | topic=考试与考后闲聊 | msg=在吗 | 「在吗」只是确认在线的招呼,未开启新话题(启发式:距上一条消息已过 98 分钟(阈值 45):时间长只是弱信号,交模型判下一句是否延续) | {'heuristic': 'continue', 'heuristic_confidence': 0.5, 'needs_llm': True, 'llm_used': True, 'mode': 'mark', 'has_reply': False, 'cost_ms': 1192}
[2026-11-13 01:04:46.179] [astrbot_plugin_LLMPerception] [INFO] [astrbot_plugin_LLMPerception.main:430]: 已添加感知信息: 发送时间: 2026-11-13 01:04:46 | 周五, 工作日, 深夜 | 平台: qq_official, 私聊
[2026-11-13 01:04:46.226] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [主链] 已约束被动回复的 send_message_to_user 仅用于媒体投递: session=default:FriendMessage:13E646FD25BD142269F2B12BBA419986
[2026-11-13 01:04:47.244] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [LLM工具动作] [ReactionExpression] {"trace_id":"b40da89e0173","stage":"authorization","decision":"scoped","reason":"media_tools_scoped","scope":"private"}
[2026-11-13 01:04:47.251] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [被动状态管线] 已注入被动状态提示词到 default:FriendMessage:13E646FD25BD142269F2B12BBA419986: mode=light state_mode=delta reason=changed placement=system_prompt+extra_user_content_parts chars=2077 状态=心理能量=65/100|情绪底色=恍惚;当前日程=(暂无);已核实当前活动=(暂无);当前计划时段=(暂无)
[2026-11-13 01:04:47.253] [astrbot_plugin_fish_heart] [INFO] [astrbot_plugin_fish_heart.main:5729]: [鱼的心][影响] {"ts": "2026-11-13 01:04:47", "channel": "private", "source": "owner", "speaker": "13E646FD25BD142269F2", "sender_id": "13E646FD25BD142269F2B12BBA419986", "is_owner": true, "bound_owner": true, "text": "在吗", "weight": 1.0, "events": ["reply_fast", "user_msg"], "events_cn": ["及时回", "说话"], "event_log": [{"event": "reply_fast", "event_cn": "及时回", "applied": {"attachment": 0.7451806605815534, "stress": -2.139375, "intimacy": 0.5052536245099617, "expectation": -40.0, "joy": 1.3819006103107654}}, {"event": "user_msg", "event_cn": "说话", "applied": {"social": 0.7873894866716711, "joy": 0.33165614647458597, "curiosity": 0.2108108418222585, "fatigue": 0.5219035079072292, "intimacy": 0.024757427600988535}}], "skipped": [], "thought_settled": {"kind": "hug", "mood": "被抱了一下,整个人都松了"}, "woken": false, "negative": false, "before": {"blame": 0.0, "expectation": 40.0, "mode": "calm", "mood": "黏人", "desire": 0.0, "desire_awakened": false, "drives": {"attachment": 81.37, "curiosity": 57.84, "reflection": 50.16, "duty": 50.03, "social": 56.26, "fatigue": 26.03, "intimacy": 74.74, "stress": 2.14, "joy": 65.45}}, "after": {"blame": 0.0, "expectation": 0.0, "mode": "calm", "mood": "黏人", "desire": 0.0, "desire_awakened": false, "drives": {"attachment": 84.12, "curiosity": 58.05, "reflection": 50.16, "duty": 50.03, "social": 57.04, "fatigue": 26.55, "intimacy": 77.77, "stress": 0.0, "joy": 70.67}}, "applied_total": {"blame": 0.0, "expectation": -40.0, "desire": 0.0, "drives": {"attachment": 2.75, "curiosity": 0.21, "social": 0.78, "fatigue": 0.52, "intimacy": 3.03, "stress": -2.14, "joy": 5.22}}, "turn_mode_before": "calm", "turn_mode_after": "calm"}
[2026-11-13 01:04:50.615] [astrbot_plugin_memory_companion] [INFO] [core.service:10027]: [MemoryCompanion] 已注入当前轮记忆: session=default:FriendMessage:13E646FD25BD142269F2B12BBA419986 snapshot=context:c99b9cefd826:15c2d3cfb7b8f7e8 count=0 chars=1205
[2026-11-13 01:04:50.630] [astrbot_plugin_memory_companion] [INFO] [core.service:7018]: [MemoryCompanion] recall guidance offered: turn=246468e8eaba8797b829cd624608d9b314cd6306e169e0e3c80841fcf6b9062b request_tools_at_memory_hook=memory_companion_events,memory_companion_navigate,memory_companion_query,memory_companion_recall,memory_companion_sources
[2026-11-13 01:04:50.649] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [回复持久化] [SendTracking] send wrapper installed: event=140409369924368
[2026-11-13 01:04:50.649] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [回复持久化] [SendTracking] send_streaming wrapper installed: event=140409369924368
[2026-11-13 01:04:52.363] [meme_manager] [INFO] [mixins.event_handlers:2101]: [meme_manager] 去重后的最终表情列表: []
[2026-11-13 01:04:53.151] [Core] [INFO] [respond.stage:237]: Prepare to send - /13E646FD25BD142269F2B12BBA419986: 在的。 你还没睡呀? 我刚迷迷糊糊醒了一下,正愣着呢。
[2026-11-13 01:05:05.028] [astrbot_plugin_private_companion] [INFO] [astrbot_plugin_private_companion.logging_util:139]: [回复持久化] 已将实际发送回复交给 AstrBot 核心保存: default:FriendMessage:13E646FD25BD142269F2B12BBA419986

辅助信息

重启机器人后的日志.zip 重启机器人前的日志.zip

检查清单

  • [x] 我已在 Issue 列表中搜索过相关问题仍无法解决,或该问题从未被报告过。
  • [x] 我已尝试过禁用所有插件,排除了可能是因插件导致的问题。
  • [x] 我报告的问题与 AstrBot 本体相关,而非在报告某一插件的问题。
  • [x] 我已阅读并同意本项目的贡献者行为准则。
  • [x] (可选)我愿意提交 PR 以帮助修复该问题。

DFGHJ43 avatar Nov 14 '26 09:11 DFGHJ43

结论:本体 bug(QQ 官方适配器的重连路径) 根因:botpy 在 _bot_login 里只取一次 /gateway/bot 返回的地址,之后所有重连复用同一个 URL;同时 AstrBot 以 bot_log=False 初始化客户端,关闭码不可见,重连失败后只能每 5 秒打一行日志 —— astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py:304 @ 23db1ad08(静态定位,未实机复现)

建议:

  1. 先把 botpy 日志打开,或至少在 ManagedBotWebSocket.on_closed 里把 close_status_code / close_msg 记到 AstrBot 日志。下一次复发时拿到的关闭码,是判断「URL 失效」还是「session 并发限制」的唯一依据。
  2. 重连前重新请求 /gateway/bot 获取新地址,不要复用启动时缓存的 _session["url"]。目前 botpy/gateway.py 的 ws_connect 直接读缓存值。
  3. 给重连加退避。session_interval 固定为 round(5 / max_concurrency) = 5 秒,而 /gateway/bot 返回的 session_start_limit 是 24 小时内 total: 1000,5 秒一次大约 1.4 小时就能耗尽配额,配额打满后新连接会被拒到次日重置。

建议标签:area:platform


本评论由 AI 辅助生成,已对照 QQ 官方 WebSocket 文档与本地代码核实。

silvaling avatar Nov 14 '26 09:11 silvaling

在这里想请教一下各位 maintainer/member:

一、问题根因

经过分析,本问题的根因是由断网够久导致旧 session 失效,网络恢复时 botpy/gateway.py 的逻辑有问题,导致哪怕网络恢复后也不能正确恢复 websocket 连接,导致了 Bot 和用户单方向失联。

所以,这是外部 SDK 的问题,Astrbot 能做的有限。

二、问题触发过程

用户需要网络中断一定时间后才能触发这个问题,在本 issue 中,运行 Astrbot 的机器在 00:21 被校园网策略关闭了外部网络连接,过了 40 分钟才恢复,因此刚好触发了这个问题。

根据 .venv\Lib\site-packages\botpy\gateway.py 里面的定义,当网络中断又恢复时 gateway 会发送操作码 6 Resume Send,但此时断网时间较长,服务端判定 session ID 是旧的,过期了,服务端会因此返回操作码 9,但是相关代码对操作码 9 的应对流程有误,没有做可以恢复连接的操作。因此进入僵尸态。

操作码 9 本身是正确通知,问题在 botpy 收到后没有进入恢复流程。

三、为什么 Bot 还能发送消息?

AstrBot 发送不经过 WebSocket,这就是为什么网络恢复后,bot 能向用户发信息,但用户不能向 bot 发信息。

四、关于后续处理方向

对于这种第三方 SDK 导致的连接问题我们要如何处理呢?

我希望先讨论一下修复方向,避免引入不必要的改动。

DFGHJ43 avatar Nov 19 '26 15:11 DFGHJ43

感觉可以使用内网穿透+webHook方式连接官方bot,以避免ws断联的问题

skyli665 avatar Nov 19 '26 15:11 skyli665

感觉可以使用内网穿透+webHook方式连接官方bot,以避免ws断联的问题

这个第三方SDK “botpy” 最近一次合并PR已经是2024年9月14日了……所以等botpy解决这个问题似乎不太可能,而第三方的QQ机器人又大概率会导致封号,所以现在这个议题很尴尬,8知道怎么办了

DFGHJ43 avatar Nov 22 '26 15:11 DFGHJ43