1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 13:03:35 +08:00

[core] AuthControl: require explicit .start() instead of launching job in constructor

This commit is contained in:
Him188 2023-04-22 12:05:46 +01:00
parent 10e731f260
commit 0c24053198
4 changed files with 11 additions and 6 deletions
mirai-core/src
commonMain/kotlin/network
commonTest/kotlin/network/component

View File

@ -86,7 +86,7 @@ internal class AuthControl(
}
}
init {
fun start() {
userDecisions.expectMore(null)
}

View File

@ -103,6 +103,7 @@ internal sealed interface ProducerState<T, V> {
class ProducerReady<T, V>(
launchProducer: () -> OnDemandProducerScope<T, V>,
) : HasProducer<T, V> {
// Lazily start the producer job since it's on-demand
override val producer: OnDemandProducerScope<T, V> by lazy(launchProducer) // `lazy` is synchronized
fun startProducerIfNotYet() {

View File

@ -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!!

View File

@ -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)