diff --git a/mirai-core/src/commonMain/kotlin/network/auth/AuthControl.kt b/mirai-core/src/commonMain/kotlin/network/auth/AuthControl.kt index cee451327..215d91661 100644 --- a/mirai-core/src/commonMain/kotlin/network/auth/AuthControl.kt +++ b/mirai-core/src/commonMain/kotlin/network/auth/AuthControl.kt @@ -86,7 +86,7 @@ internal class AuthControl( } } - init { + fun start() { userDecisions.expectMore(null) } diff --git a/mirai-core/src/commonMain/kotlin/network/auth/ProducerState.kt b/mirai-core/src/commonMain/kotlin/network/auth/ProducerState.kt index 128ac9ebe..373694b84 100644 --- a/mirai-core/src/commonMain/kotlin/network/auth/ProducerState.kt +++ b/mirai-core/src/commonMain/kotlin/network/auth/ProducerState.kt @@ -103,6 +103,7 @@ internal sealed interface ProducerState { class ProducerReady( launchProducer: () -> OnDemandProducerScope, ) : HasProducer { + // Lazily start the producer job since it's on-demand override val producer: OnDemandProducerScope by lazy(launchProducer) // `lazy` is synchronized fun startProducerIfNotYet() { diff --git a/mirai-core/src/commonMain/kotlin/network/components/SsoProcessor.kt b/mirai-core/src/commonMain/kotlin/network/components/SsoProcessor.kt index 915ff04b3..6466bc723 100644 --- a/mirai-core/src/commonMain/kotlin/network/components/SsoProcessor.kt +++ b/mirai-core/src/commonMain/kotlin/network/components/SsoProcessor.kt @@ -11,7 +11,8 @@ package net.mamoe.mirai.internal.network.components import kotlinx.atomicfu.AtomicRef import kotlinx.atomicfu.atomic -import net.mamoe.mirai.auth.* +import net.mamoe.mirai.auth.BotAuthInfo +import net.mamoe.mirai.auth.BotAuthorization import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.QRCodeLoginData @@ -159,13 +160,13 @@ internal class SsoProcessorImpl( */ override suspend fun login(handler: NetworkHandler) { - fun initAuthControl() { + fun initAndStartAuthControl() { authControl = AuthControl( botAuthInfo, ssoContext.bot.account.authorization, ssoContext.bot.network.logger, ssoContext.bot.coroutineContext, // do not use network context because network may restart whilst auth control should keep alive - ) + ).also { it.start() } } suspend fun loginSuccess() { @@ -195,7 +196,7 @@ internal class SsoProcessorImpl( kotlin.runCatching { FastLoginImpl(handler).doLogin() }.onFailure { e -> - initAuthControl() + initAndStartAuthControl() authControl!!.exceptionCollector.collect(e) throw SelectorRequireReconnectException() @@ -207,7 +208,7 @@ internal class SsoProcessorImpl( } } - if (authControl == null) initAuthControl() + if (authControl == null) initAndStartAuthControl() val authControl0 = authControl!! diff --git a/mirai-core/src/commonTest/kotlin/network/component/BotAuthControlTest.kt b/mirai-core/src/commonTest/kotlin/network/component/BotAuthControlTest.kt index 4ed2708ae..f898d10fe 100644 --- a/mirai-core/src/commonTest/kotlin/network/component/BotAuthControlTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/component/BotAuthControlTest.kt @@ -60,6 +60,7 @@ internal class BotAuthControlTest : AbstractCommonNHTest() { } }, bot.logger, backgroundScope.coroutineContext) + control.start() control.assertRequire(SsoProcessorImpl.AuthMethod.Pwd::class) control.actComplete() control.assertRequire(SsoProcessorImpl.AuthMethod.NotAvailable::class) @@ -78,6 +79,7 @@ internal class BotAuthControlTest : AbstractCommonNHTest() { } }, bot.logger, backgroundScope.coroutineContext) + control.start() control.assertRequire(SsoProcessorImpl.AuthMethod.Pwd::class) control.actMethodFailed(MyLoginFailedException()) @@ -100,6 +102,7 @@ internal class BotAuthControlTest : AbstractCommonNHTest() { } }, bot.logger, backgroundScope.coroutineContext) + control.start() control.assertRequire(SsoProcessorImpl.AuthMethod.Pwd::class) control.actComplete() control.assertRequire(SsoProcessorImpl.AuthMethod.NotAvailable::class)