[console] Fix plugin disabling order control

This commit is contained in:
Karlatemp 2022-12-18 23:01:28 +08:00
parent b59c7336d0
commit 2f759c199a
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59

View File

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