Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coroutine issues with Dispatchers.Main #6

Closed
AlbRoehm opened this issue Mar 30, 2024 · 13 comments
Closed

Coroutine issues with Dispatchers.Main #6

AlbRoehm opened this issue Mar 30, 2024 · 13 comments

Comments

@AlbRoehm
Copy link

AlbRoehm commented Mar 30, 2024

After upgrading to compose-kmp 1.6.0 i'm seeing following issues even when the mentioned packages are present.

Exception in thread "OkHttp Dispatcher" java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'
	at kotlinx.coroutines.internal.MainDispatchersKt.throwMissingMainDispatcherException(MainDispatchers.kt:77)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:108)

I hoped that #5 fixed this issues but its still there. Replacing Dispatchers.Main with Dispatchers.Default in FirebaseAuth.kt, AsyncTask.kt and Handler.ktseems to fix the issue for me. I'm not super deep into coroutines and dispatcher, please ignore if this does not make sense to you.

@nbransby
Copy link
Member

nbransby commented Apr 1, 2024

After upgrading to compose-kmp 1.6.0 i'm seeing following issues even when the mentioned packages are present.

What mentioned packages are you referring to? I'm not super deep into compose-kmp, what library is used to add the Main dispatcher? For a swing-based env you would use kotlinx-coroutines-swing, like the tests do:
https://github.com/GitLiveApp/firebase-java-sdk/blob/master/build.gradle.kts#L153

@AlbRoehm
Copy link
Author

AlbRoehm commented Apr 1, 2024

Sorry, maybe my description was misleading. For the android source set kotlinx-corountines-android is present in my project and for jvm-desktop kotlinx-coroutines-swing is present. For compose 1.5.11 this worked fine but it seems that Dispatchers.Main is not working correctly anymore when compose 1.6.0 is used. With my proposed changes I got it working again by changing the dispatcher and publishing to mavenlocal

@nbransby
Copy link
Member

nbransby commented Apr 2, 2024

What version of coroutines does compose 1.6.0 ship with?

@AlbRoehm
Copy link
Author

AlbRoehm commented Apr 2, 2024

It looks like the compose-runtime is bringing corountines:1.8.0

+--- org.jetbrains.compose.runtime:runtime:1.6.1
|    +--- org.jetbrains.compose.collection-internal:collection:1.6.1
|    |    +--- org.jetbrains.compose.annotation-internal:annotation:1.6.1
|    |    |    \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.23 (*)
|    |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.9.23 (*)
|    |    \--- org.jetbrains.kotlinx:atomicfu:0.23.2
|    |         \--- org.jetbrains.kotlin:kotlin-stdlib:{prefer 1.9.21} -> 1.9.23 (*)
|    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.9.23
|    |    \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.23 (*)
|    +--- org.jetbrains.kotlinx:atomicfu:0.23.2 (*)
|    \--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0
|         +--- org.jetbrains.kotlin:kotlin-stdlib:1.9.21 -> 1.9.23 (*)
|         \--- org.jetbrains.kotlinx:atomicfu:0.23.1 -> 0.23.2 (*)

@AlbRoehm
Copy link
Author

AlbRoehm commented Apr 8, 2024

Maybe this was only because of usage of different coroutine packages?
With latests update of kotlin-sdk 1.12.0 this is not an issues anymore. Fine to close from my perspective.

@AlbRoehm AlbRoehm closed this as completed Apr 8, 2024
@AlbRoehm
Copy link
Author

AlbRoehm commented Apr 8, 2024

Turns out for Windows the issues is still there also with the new version of kotlin-sdk. Changing to Dispatchers.Default fixes it also for Windows, at least how far i can tell.

@nbransby
Copy link
Member

nbransby commented Apr 9, 2024

Not sure what's going on there but changing it to Dispatchers.Default is more of a workaround/hack, need to find out what the root cause is

@nbransby nbransby reopened this Apr 9, 2024
@alexzhirkevich
Copy link

alexzhirkevich commented Apr 16, 2024

The core issue is here and it is going to be fixed soon - Kotlin/kotlinx.coroutines#3914. There is a workaround to disable fast service loader mentioned there

@Deaths-Door
Copy link

Deaths-Door commented Jun 1, 2024

I'm encountering the known issue with kotlinx.coroutines that affects applications running on Windows.

The core issue is here and it is going to be fixed soon - Kotlin/kotlinx.coroutines#3914. There is a workaround to disable fast service loader mentioned there

Disabling the fast service loader, as suggested in the workaround, currently doesn't work on Windows. Setting System.setProperty("kotlinx.coroutines.fast.service.loader", "false") doesn't resolve the problem.

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'
	at kotlinx.coroutines.internal.MainDispatchersKt.throwMissingMainDispatcherException(MainDispatchers.kt:77)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:108)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:92)
	at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:315)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:21)
	at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:88)
	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:123)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:52)
	at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:43)
	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
	at com.google.firebase.auth.FirebaseAuth.addAuthStateListener(FirebaseAuth.kt:397)
	at dev.gitlive.firebase.auth.FirebaseAuth$authStateChanged$1.invokeSuspend(auth.kt:35)
	at dev.gitlive.firebase.auth.FirebaseAuth$authStateChanged$1.invoke(auth.kt)
	at dev.gitlive.firebase.auth.FirebaseAuth$authStateChanged$1.invoke(auth.kt)
	at kotlinx.coroutines.flow.ChannelFlowBuilder.collectTo$suspendImpl(Builders.kt:316)
	at kotlinx.coroutines.flow.ChannelFlowBuilder.collectTo(Builders.kt)
	at kotlinx.coroutines.flow.CallbackFlowBuilder.collectTo(Builders.kt:330)
	at kotlinx.coroutines.flow.internal.ChannelFlow$collectToFun$1.invokeSuspend(ChannelFlow.kt:56)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2$1.invoke(FlushCoroutineDispatcher.skiko.kt:62)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2$1.invoke(FlushCoroutineDispatcher.skiko.kt:57)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher.performRun(FlushCoroutineDispatcher.skiko.kt:99)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher.access$performRun(FlushCoroutineDispatcher.skiko.kt:37)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2.invokeSuspend(FlushCoroutineDispatcher.skiko.kt:57)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:792)
	at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:739)
	at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:733)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:761)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:207)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:92)
	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@2abc176, Dispatchers.Main[missing]]
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'
	at kotlinx.coroutines.internal.MainDispatchersKt.throwMissingMainDispatcherException(MainDispatchers.kt:77)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:108)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:92)
	at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:315)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:21)
	at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:88)
	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:123)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:52)
	at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:43)
	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
	at com.google.firebase.auth.FirebaseAuth.addAuthStateListener(FirebaseAuth.kt:397)
	at dev.gitlive.firebase.auth.FirebaseAuth$authStateChanged$1.invokeSuspend(auth.kt:35)
	at dev.gitlive.firebase.auth.FirebaseAuth$authStateChanged$1.invoke(auth.kt)
	at dev.gitlive.firebase.auth.FirebaseAuth$authStateChanged$1.invoke(auth.kt)
	at kotlinx.coroutines.flow.ChannelFlowBuilder.collectTo$suspendImpl(Builders.kt:316)
	at kotlinx.coroutines.flow.ChannelFlowBuilder.collectTo(Builders.kt)
	at kotlinx.coroutines.flow.CallbackFlowBuilder.collectTo(Builders.kt:330)
	at kotlinx.coroutines.flow.internal.ChannelFlow$collectToFun$1.invokeSuspend(ChannelFlow.kt:56)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2$1.invoke(FlushCoroutineDispatcher.skiko.kt:62)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2$1.invoke(FlushCoroutineDispatcher.skiko.kt:57)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher.performRun(FlushCoroutineDispatcher.skiko.kt:99)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher.access$performRun(FlushCoroutineDispatcher.skiko.kt:37)
	at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2.invokeSuspend(FlushCoroutineDispatcher.skiko.kt:57)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:792)
	at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:739)
	at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:733)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:761)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:207)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:92)
	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelled}@2abc176, Dispatchers.Main[missing]]
	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.ui.scene.ComposeContainer$DesktopCoroutineExceptionHandler@7ac80f17, androidx.compose.runtime.BroadcastFrameClock@6dd0f1ff, StandaloneCoroutine{Cancelling}@599cad48, FlushCoroutineDispatcher@7b995065]

So is there some sort of solution or workaround for this issue?

@AlbRoehm
Copy link
Author

AlbRoehm commented Jun 3, 2024

So is there some sort of solution or workaround for this issue?

I used a hack/workaround where i did go into codebase and replaced Dispatchers.Main with Dispatchers.Default and published it to MavenLocal and imported it in my project to be used instead. This made it work on Windows for me, but as mentioned by @nbransby this is kind of a bad hack.

@nbransby
Copy link
Member

nbransby commented Jun 3, 2024

The fix is available: https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.9.0-RC

have you tried upgrading to 1.9.0-RC?

@AlbRoehm
Copy link
Author

AlbRoehm commented Jun 3, 2024

Thanks for the reminder, org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.9.0-RC seem to have fixed the issue for me on Mac and on Windows. Also no need to manually import some other version of the java-sdk.

@Deaths-Door
Copy link

I can confirm that adding the dependency org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.9.0-RC as suggested by @AlbRoehm , resolves the issue. However, it's important to note that this dependency needs to be included specifically in the desktop source set (Tested on Windows). Simply updating the version to 1.9.0-RC in commonMain doesn't seem to be sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants