From 2f759c199a18b9fd9897bac3d46a8b434d33acc1 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sun, 18 Dec 2022 23:01:28 +0800 Subject: [PATCH] [console] Fix plugin disabling order control --- .../src/internal/plugin/PluginManagerImpl.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mirai-console/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt b/mirai-console/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt index 89e835731..c36338e24 100644 --- a/mirai-console/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt +++ b/mirai-console/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt @@ -12,7 +12,7 @@ package net.mamoe.mirai.console.internal.plugin import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job +import kotlinx.coroutines.job import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.extensions.PluginLoaderProvider import net.mamoe.mirai.console.internal.data.mkdir @@ -74,7 +74,13 @@ internal class PluginManagerImpl( plugin.safeLoader.getPluginDescription(plugin) init { - MiraiConsole.coroutineContext[Job]!!.invokeOnCompletion { + // Kotlin coroutine job cancelling ordering: + // - sub job 0 invokeOnCompletion called + // - sub job 1 invokeOnCompletion called + // - sub job N invokeOnCompletion called + // - parent invokeOnCompletion called + // So we need register a child job to control plugins' disabling order + this.childScopeContext("PluginManager shutdown monitor").job.invokeOnCompletion { plugins.asReversed().forEach { plugin -> if (plugin.isEnabled) { disablePlugin(plugin)