From 65eff19f320cdae40a4eed636b77f0cac65edd33 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 23 Jan 2021 23:29:09 +0800 Subject: [PATCH] Fix reconnect. Fix #894 --- .../src/commonMain/kotlin/AbstractBot.kt | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/AbstractBot.kt b/mirai-core/src/commonMain/kotlin/AbstractBot.kt index 12b7b8403..60645ffa1 100644 --- a/mirai-core/src/commonMain/kotlin/AbstractBot.kt +++ b/mirai-core/src/commonMain/kotlin/AbstractBot.kt @@ -96,16 +96,14 @@ internal abstract class AbstractBot constructor( priority = MONITOR, concurrency = ConcurrencyKind.LOCKED ) { event -> - if (!event.bot.isActive) { - // bot closed - return@subscribeAlways - } - if (!::_network.isInitialized) { - // bot 还未登录就被 close - return@subscribeAlways - } - if (_isConnecting) { - // bot 还在登入 + if ( + !event.bot.isActive // bot closed + || !::_network.isInitialized // bot 还未登录就被 close + || _isConnecting // bot 还在登入 + ) { + // Close network to avoid endless reconnection while network is ok + // https://github.com/mamoe/mirai/issues/894 + kotlin.runCatching { network.close(event.castOrNull()?.cause) } return@subscribeAlways } /* @@ -167,13 +165,20 @@ internal abstract class AbstractBot constructor( if (tryCount != 0) { delay(configuration.reconnectPeriodMillis) } - network.withConnectionLock { - /** - * [AbstractBot.relogin] only, no [BotNetworkHandler.init] - */ - @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class) - relogin((event as? BotOfflineEvent.Dropped)?.cause) - } + + + // Close network to avoid endless reconnection while network is ok + // https://github.com/mamoe/mirai/issues/894 + kotlin.runCatching { network.close(event.castOrNull()?.cause) } + + login() +// network.withConnectionLock { +// /** +// * [AbstractBot.relogin] only, no [BotNetworkHandler.init] +// */ +// @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class) +// relogin((event as? BotOfflineEvent.Dropped)?.cause) +// } launch { BotReloginEvent(bot, (event as? BotOfflineEvent.CauseAware)?.cause).broadcast() }