Repository navigation
🐛 keep onebot11 message loop alive on events before login - #40
Merged
Merged
Conversation
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
OneBot 11 适配器收到
lifecycle.connect/heartbeat后,用asyncio.create_task(_login_add())异步登记账号,_login_add要先等get_login_info的响应,才把 login 写入logins。这段时间里协议端(比如 NapCat)如果已经推来普通事件,
message_handle会走到:这里的
return结束的是整个接收循环,不只是这一条事件。之后连get_login_info的 echo 响应也没人处理,账号不会登记:message_handle结束后websocket_server_handler进入finally,self.logins[account_id]因为账号还没写入而抛KeyError,连接被关闭,等协议端重连后可能再次撞上同样的时序。receiver_task结束,connection_daemon转入下一轮重连。群消息多的账号刚连上时比较容易遇到。
最小复现:用一个假的 WebSocket 先推
lifecycle.connect,在回复get_login_info之前推一条群消息事件,再回复 echo。main 上message_handle立即退出,logins为空。改法
把这处
return改成continue:照旧记录 warning 并丢弃这条登录前的事件,循环继续处理后续消息。forward.py 与 reverse.py 中的同一段代码一起修改。只有
self_id未登记的分支受影响,账号已登记时的事件处理、echo 响应、心跳和 connect 的逻辑都没有变化。lifecycle.enable/lifecycle.disable两处的return没有改。按 OneBot 11 的约定,这两个事件只会出现在 HTTP 通信方式下,WebSocket 连接中收到时目前的处理是记录 warning 并结束接收,我不确定这是否有意为之,所以保持原样。验证
仓库目前没有测试框架,我用未提交到仓库的脚本验证:给
_Connection(reverse)和OneBot11ForwardAdapter(forward)喂入同一组消息——lifecycle.connect→ 群消息事件 →get_login_info的 echo 响应,等待 0.5 秒后检查。message_handle已退出,logins为空logins含该账号,推送了login-addedmessage_handle已退出,logins为空logins含该账号两种情况下那条登录前的事件都只记录 warning 后被丢弃。
ruff check通过,black --check无差异。