Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复因握手阶段的错误导致bot无法接受后续连接的问题 #110

Merged
merged 2 commits into from
Nov 24, 2024

Conversation

kagg886
Copy link
Contributor

@kagg886 kagg886 commented Nov 24, 2024

在提出此拉取请求时,我确认了以下几点(请复选框):

  • 我已阅读并理解贡献文档
  • 我已检查没有与此请求重复的 Pull Requests。
  • 我已经考虑过,并确认这份呈件对其他人很有价值。
  • 我接受此提交可能不会被使用,并根据维护人员的意愿关闭 Pull Requests。

填写PR内容:
出问题的代码片段如下:

val versionInfo = botImpl.getVersionInfo()
if (printInfo) {
    logger.info("协议端版本信息\n${gson.toJson(versionInfo.getAsJsonObject("data"))}")
}
if (botImpl.onebotVersion == 12) {
    throw IllegalStateException("Overflow 暂不支持 Onebot 12")
}
val bot = botImpl.wrap(configuration, workingDir)

return bot.also {
    it.eventDispatcher.broadcastAsync(BotOnlineEvent(bot))
}

getVersionInfowrap内发包出错(如超时)时会导致ActionHandler的协程失活,从而无法接受后续bot连接以及无法推送新消息。

于是让创建producer的scope和ConnectFactory绑定在一起

@JvmOverloads
fun createProducer(
   scope0: CoroutineScope = CoroutineScope(CoroutineName("ConnectFactory"))
): OneBotProducer {
   val scope = if (parent == null) scope0 else scope0 + parent
   //...TODO   
}
        

同时修改ConnectFactory的scope源头:

val defaultJob: Job? by lazy {
    if (!miraiConsole) return@lazy SupervisorJob()
    return@lazy MiraiConsole.job
}

希望可以解决这个问题。

可能断连时第二次无法继续连接的bug也会在这里得到修复

我的bot是直接给ActionHandler暴力写了个SupervisorJob解决的,like this

class WSHandler : WebSocketHandler, IAdapter {
    override val scope = CoroutineScope(Dispatchers.IO) + SupervisorJob()

    override val logger: Logger = LoggerFactory.getLogger(ActionHandler::class.java)
    override val actionHandler: ActionHandler = ActionHandler(scope.coroutineContext[Job], logger)

    init {
        top.mrxiaom.overflow.internal.Overflow.setup()
        log.info("Youmu WebSocket 已接管默认Overflow Bot Handler,WSBufferSize: ${System.getProperty("org.apache.tomcat.websocket.DEFAULT_BUFFER_SIZE")}")
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants