diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt index 3c35e46dd..be1b4dd65 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt @@ -9,12 +9,16 @@ package net.mamoe.mirai.console.internal.plugin -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.ensureActive import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip import net.mamoe.mirai.console.internal.data.createInstanceOrNull +import net.mamoe.mirai.console.internal.util.childScopeContext import net.mamoe.mirai.console.plugin.AbstractFilePluginLoader import net.mamoe.mirai.console.plugin.PluginLoadException import net.mamoe.mirai.console.plugin.jvm.* @@ -40,11 +44,9 @@ internal object JarPluginLoaderImpl : get() = MiraiConsoleImplementationBridge.dataStorageForJarPluginLoader override val coroutineContext: CoroutineContext = - MiraiConsole.coroutineContext + - SupervisorJob(MiraiConsole.coroutineContext[Job]) + - CoroutineExceptionHandler { _, throwable -> - logger.error("Unhandled Jar plugin exception: ${throwable.message}", throwable) - } + MiraiConsole.childScopeContext(CoroutineExceptionHandler { _, throwable -> + logger.error("Unhandled Jar plugin exception: ${throwable.message}", throwable) + }) private val classLoader: PluginsLoader = PluginsLoader(this.javaClass.classLoader) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt index 3f89bb0a8..a1a3e90c1 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt @@ -127,6 +127,11 @@ internal abstract class JvmPluginInternal( CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } .plus(parentCoroutineContext) .plus(SupervisorJob(parentCoroutineContext[Job])) + .also { + JarPluginLoaderImpl.coroutineContext[Job]!!.invokeOnCompletion { + this.cancel() + } + } .plus(_intrinsicCoroutineContext) }