[console] Make JvmPluginLoader a child job of PluginManager

This commit is contained in:
Karlatemp 2022-12-18 22:58:52 +08:00
parent 553ea9abbc
commit b59c7336d0
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59
2 changed files with 19 additions and 11 deletions

View File

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

View File

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