From 30e359c282ec4528bee8a3c464c664d83b9da958 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 1 Aug 2020 23:13:49 +0800 Subject: [PATCH] Fix blocking call in shutdown hook --- .../mirai/console/command/BuiltInCommands.kt | 86 +++++++++---------- .../mirai/console/command/TestCommand.kt | 22 ++++- .../mirai/console/setting/SettingTest.kt | 8 +- 3 files changed, 63 insertions(+), 53 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/BuiltInCommands.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/BuiltInCommands.kt index d4eac3ff8..243aefb0b 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/BuiltInCommands.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/BuiltInCommands.kt @@ -9,9 +9,8 @@ package net.mamoe.mirai.console.command +import kotlinx.coroutines.cancel import kotlinx.coroutines.cancelAndJoin -import kotlinx.coroutines.isActive -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import net.mamoe.mirai.Bot @@ -86,12 +85,6 @@ public object BuiltInCommands { ConsoleCommandOwner, "help", description = "Gets help about the console." ), BuiltInCommand { - init { - Runtime.getRuntime().addShutdownHook(thread(false) { - runBlocking { Stop.execute(ConsoleCommandSender.instance) } - }) - } - @Handler public suspend fun CommandSender.handle() { sendMessage("现在有指令: ${allRegisteredCommands.joinToString { it.primaryName }}") @@ -105,57 +98,56 @@ public object BuiltInCommands { ), BuiltInCommand { init { Runtime.getRuntime().addShutdownHook(thread(false) { - if (!MiraiConsole.isActive) { - return@thread - } - runBlocking { Stop.execute(ConsoleCommandSender.instance) } + MiraiConsole.cancel() }) } private val closingLock = Mutex() @Handler - public suspend fun CommandSender.handle(): Unit = closingLock.withLock { - sendMessage("Stopping mirai-console") - kotlin.runCatching { - MiraiConsole.job.cancelAndJoin() - }.fold( - onSuccess = { sendMessage("mirai-console stopped successfully.") }, - onFailure = { - MiraiConsole.mainLogger.error(it) - sendMessage(it.localizedMessage ?: it.message ?: it.toString()) - } - ) + public suspend fun CommandSender.handle(): Unit { + closingLock.withLock { + sendMessage("Stopping mirai-console") + kotlin.runCatching { + MiraiConsole.job.cancelAndJoin() + }.fold( + onSuccess = { sendMessage("mirai-console stopped successfully.") }, + onFailure = { + MiraiConsole.mainLogger.error(it) + sendMessage(it.localizedMessage ?: it.message ?: it.toString()) + } + ) + } exitProcess(0) } - } - public object Login : SimpleCommand( - ConsoleCommandOwner, "login", - description = "Log in a bot account." - ), BuiltInCommand { - @Handler - public suspend fun CommandSender.handle(id: Long, password: String) { + public object Login : SimpleCommand( + ConsoleCommandOwner, "login", + description = "Log in a bot account." + ), BuiltInCommand { + @Handler + public suspend fun CommandSender.handle(id: Long, password: String) { - kotlin.runCatching { - MiraiConsole.addBot(id, password).alsoLogin() - }.fold( - onSuccess = { sendMessage("${it.nick} ($id) Login succeed") }, - onFailure = { throwable -> - sendMessage( - "Login failed: ${throwable.localizedMessage ?: throwable.message ?: throwable.toString()}" + - if (this is MessageEventContextAware<*>) { - this.fromEvent.selectMessagesUnit { - "stacktrace" reply { - throwable.stacktraceString + kotlin.runCatching { + MiraiConsole.addBot(id, password).alsoLogin() + }.fold( + onSuccess = { sendMessage("${it.nick} ($id) Login succeed") }, + onFailure = { throwable -> + sendMessage( + "Login failed: ${throwable.localizedMessage ?: throwable.message ?: throwable.toString()}" + + if (this is MessageEventContextAware<*>) { + this.fromEvent.selectMessagesUnit { + "stacktrace" reply { + throwable.stacktraceString + } } - } - "test" - } else "") + "test" + } else "") - throw throwable - } - ) + throw throwable + } + ) + } } } } diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt index c8ae74243..a54384f38 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt @@ -11,7 +11,23 @@ package net.mamoe.mirai.console.command +import kotlinx.coroutines.cancel +import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.Testing +import net.mamoe.mirai.console.Testing.withTesting +import net.mamoe.mirai.console.command.description.CommandArgParser +import net.mamoe.mirai.console.command.description.CommandParserContext +import net.mamoe.mirai.console.command.internal.InternalCommandManager +import net.mamoe.mirai.console.command.internal.flattenCommandComponents +import net.mamoe.mirai.console.initTestEnvironment +import net.mamoe.mirai.message.data.Image +import net.mamoe.mirai.message.data.SingleMessage +import net.mamoe.mirai.message.data.toMessage +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import kotlin.test.* object TestCompositeCommand : CompositeCommand( ConsoleCommandOwner, @@ -33,7 +49,7 @@ object TestSimpleCommand : RawCommand(owner, "testSimple", "tsS") { internal val sender by lazy { ConsoleCommandSender.instance } internal val owner by lazy { ConsoleCommandOwner } -/* + internal class TestCommand { companion object { @JvmStatic @@ -45,8 +61,7 @@ internal class TestCommand { @AfterAll @JvmStatic fun destroy() { - // Runtime.getRuntime().halt(0) // TODO: 2020/8/1 fix exitProcess - exitProcess(0) + MiraiConsole.cancel() } } @@ -209,4 +224,3 @@ internal class TestCommand { } } } -*/ \ No newline at end of file diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/setting/SettingTest.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/setting/SettingTest.kt index 0dfd1a8ad..20a6e0cd2 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/setting/SettingTest.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/setting/SettingTest.kt @@ -9,7 +9,12 @@ package net.mamoe.mirai.console.setting -/* +import kotlinx.serialization.json.Json +import net.mamoe.mirai.console.utils.ConsoleInternalAPI +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertSame + @OptIn(ConsoleInternalAPI::class) internal class SettingTest { @@ -129,4 +134,3 @@ internal class SettingTest { assertSame(reference(), delegation()) // check shadowing } } -*/ \ No newline at end of file