Launching an async coroutine with the dispatcher hangs the server permanently.
opened this issue · comments
Deleted user commented
If I do
println(Thread.currentThread().name)
runBlocking {
withContext(dispatcher(plugin, async)) { // hangs here
println(Thread.currentThread().name)
}
}
println(Thread.currentThread().name)
then the server hangs forever once the async dispatcher is started. Do you have any idea why this might happen?
Ole Kristian Sandum commented
I believe this is because you use runBlocking
.
Deleted user commented
What coroutine scope do I use? GlobalScope is not recommended since they are never destroyed.
Ole Kristian Sandum commented
Hmm, I guess you could create one scoped to your plugin's lifetime. I might include something that makes this easier in the future, but for now, how about this:
class Main : JavaPlugin(), CoroutineScope {
private lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = dispatcher() + job
override fun onEnable() {
job = Job()
...
}
override fun onDisable() {
job.cancel()
...
}
}
Deleted user commented
That's super helpful, I'll give it a try. Thank you!
Ole Kristian Sandum commented
Great. I'll close this issue then.