Shynixn / MCCoroutine

MCCoroutine is a library, which adds extensive support for Kotlin Coroutines for Minecraft Server environments.
Other
211 stars 19 forks source link

AsyncDispatcher behaves like Dispatchers.Unconfined #87

Closed Spliterash closed 1 year ago

Spliterash commented 2 years ago

When I use asycnDispatcher it behaves like Dispatchers.Unconfined in every way, unless it is the main thread. Because of this, I caught a lot of errors, because I thought that he would always switch thread to the asynchronous bukkit, but it continued to execute in the lettuce thread, for example

Dispatch method from com.github.shynixn.mccoroutine.bukkit.dispatcher.AsyncCoroutineDispatcher

    override fun isDispatchNeeded(context: CoroutineContext): Boolean {
        wakeUpBlockService.ensureWakeup()
        return plugin.server.isPrimaryThread
    }
Shynixn commented 2 years ago

That's true and that's a limitation I have accepted when writing MCCoroutine. I am not sure how to reliably detect if the current thread is one of the threads of the Bukkit async thread pool.

Solution is quite simple: Use Dispatchers.IO from Kotlin Coroutines. The asyncDispatcher can be seen more like convenient feaure. I also recommend to use Dispatchers.IO in the wiki.

Spliterash commented 2 years ago

Then please note it somehow in the documentation, because I didn't know about this limitation and I mistakenly dispatched my tasks to lettuce and mognoreactive

Shynixn commented 1 year ago

I'll leave this issue open to let myself know to improve the wiki regarding that.