okkero / Skedule

Use the BukkitScheduler with coroutines - for plugin developers using Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Launching an async coroutine with the dispatcher hangs the server permanently.

opened this issue · comments

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?

I believe this is because you use runBlocking.

What coroutine scope do I use? GlobalScope is not recommended since they are never destroyed.

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()
        ...
    }

}

That's super helpful, I'll give it a try. Thank you!

Great. I'll close this issue then.