Close JvmPlugin when server is closing

This commit is contained in:
Him188 2020-08-27 21:04:51 +08:00
parent b5ab460bf8
commit c479856380
2 changed files with 13 additions and 6 deletions

View File

@ -9,12 +9,16 @@
package net.mamoe.mirai.console.internal.plugin 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.MiraiConsole
import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.data.PluginDataStorage
import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge
import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip
import net.mamoe.mirai.console.internal.data.createInstanceOrNull 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.AbstractFilePluginLoader
import net.mamoe.mirai.console.plugin.PluginLoadException import net.mamoe.mirai.console.plugin.PluginLoadException
import net.mamoe.mirai.console.plugin.jvm.* import net.mamoe.mirai.console.plugin.jvm.*
@ -40,11 +44,9 @@ internal object JarPluginLoaderImpl :
get() = MiraiConsoleImplementationBridge.dataStorageForJarPluginLoader get() = MiraiConsoleImplementationBridge.dataStorageForJarPluginLoader
override val coroutineContext: CoroutineContext = override val coroutineContext: CoroutineContext =
MiraiConsole.coroutineContext + MiraiConsole.childScopeContext(CoroutineExceptionHandler { _, throwable ->
SupervisorJob(MiraiConsole.coroutineContext[Job]) + logger.error("Unhandled Jar plugin exception: ${throwable.message}", throwable)
CoroutineExceptionHandler { _, throwable -> })
logger.error("Unhandled Jar plugin exception: ${throwable.message}", throwable)
}
private val classLoader: PluginsLoader = PluginsLoader(this.javaClass.classLoader) private val classLoader: PluginsLoader = PluginsLoader(this.javaClass.classLoader)

View File

@ -127,6 +127,11 @@ internal abstract class JvmPluginInternal(
CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }
.plus(parentCoroutineContext) .plus(parentCoroutineContext)
.plus(SupervisorJob(parentCoroutineContext[Job])) .plus(SupervisorJob(parentCoroutineContext[Job]))
.also {
JarPluginLoaderImpl.coroutineContext[Job]!!.invokeOnCompletion {
this.cancel()
}
}
.plus(_intrinsicCoroutineContext) .plus(_intrinsicCoroutineContext)
} }