From b59c7336d04c1deb28903fcb7cd67723f44aca0d Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sun, 18 Dec 2022 22:58:52 +0800 Subject: [PATCH] [console] Make JvmPluginLoader a child job of PluginManager --- .../src/MiraiConsoleImplementation.kt | 3 ++- .../src/internal/plugin/JvmPluginInternal.kt | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt b/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt index 8bbcac440..1832159bf 100644 --- a/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt +++ b/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt @@ -29,6 +29,7 @@ import net.mamoe.mirai.console.internal.command.CommandManagerImpl import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScopeImpl import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl import net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl +import net.mamoe.mirai.console.internal.plugin.impl import net.mamoe.mirai.console.internal.pluginManagerImpl import net.mamoe.mirai.console.logging.LoggerController import net.mamoe.mirai.console.plugin.Plugin @@ -350,7 +351,7 @@ public interface MiraiConsoleImplementation : CoroutineScope { * @since 2.10.0-RC */ public fun createDefaultJvmPluginLoader(coroutineContext: CoroutineContext): JvmPluginLoader = - BuiltInJvmPluginLoaderImpl(coroutineContext) + BuiltInJvmPluginLoaderImpl(coroutineContext + MiraiConsole.pluginManager.impl.coroutineContext.job) /** * @since 2.10.0-RC diff --git a/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt b/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt index cf5088c59..22bebf61e 100644 --- a/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt +++ b/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt @@ -194,6 +194,22 @@ internal abstract class JvmPluginInternal( CoroutineName("Plugin $dataHolderName") } + private val pluginParentJob: Job = run { + val job = parentCoroutineContext[Job] ?: JvmPluginLoader.coroutineContext[Job]!! + + val pluginManagerJob = MiraiConsole.pluginManager.impl.coroutineContext.job + + val allJobs = generateSequence(sequenceOf(pluginManagerJob)) { parentSeqs -> + parentSeqs.flatMap { it.children } + }.flatten() + + check(allJobs.contains(job)) { + "The parent job of plugin `$id' not a child of PluginManager" + } + + job + } + @JvmField internal val coroutineContextInitializer = { CoroutineExceptionHandler { context, throwable -> @@ -203,16 +219,7 @@ internal abstract class JvmPluginInternal( ) } .plus(parentCoroutineContext) - .plus(CoroutineName("Plugin ${(this as AbstractJvmPlugin).dataHolderName}")) - .plus( - SupervisorJob(parentCoroutineContext[Job] ?: JvmPluginLoader.coroutineContext[Job]!!) - ) - .also { - if (!MiraiConsole.isActive) return@also - JvmPluginLoader.coroutineContext[Job]!!.invokeOnCompletion { - this.cancel() - } - } + .plus(SupervisorJob(pluginParentJob)) .plus(_intrinsicCoroutineContext) }