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

1.6.0-native-mt broken with shareIn #3136

Closed
PaulWoitaschek opened this issue Jan 12, 2022 · 14 comments
Closed

1.6.0-native-mt broken with shareIn #3136

PaulWoitaschek opened this issue Jan 12, 2022 · 14 comments
Assignees

Comments

@PaulWoitaschek
Copy link
Contributor

PaulWoitaschek commented Jan 12, 2022

The following example:

  @Test
  fun sharingTest() {
    runBlocking {
      flowOf(Unit).shareIn(GlobalScope, SharingStarted.WhileSubscribed())
        .collect { println(it) }
    }
  }

Throws an exception on native (tried on iosSimulatorArm64) using Kotlin 1.6.10 and coroutines 1.6.0. This was working fine with kotlin 1.5.2.

kotlin.IllegalStateException: Cannot start an undispatched coroutine in another thread DefaultDispatcher from current MainThread
kotlin.IllegalStateException: Cannot start an undispatched coroutine in another thread DefaultDispatcher from current MainThread
	at kotlin.Throwable#<init>(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt:24)
	at kotlin.Exception#<init>(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:23)
	at kotlin.RuntimeException#<init>(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:34)
	at kotlin.IllegalStateException#<init>(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:70)
	at kotlinx.coroutines.startCoroutine#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:136)
	at kotlinx.coroutines#startAbstractCoroutine(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:118)
	at kotlinx.coroutines.AbstractCoroutine#start(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/AbstractCoroutine.kt:130)
	at kotlinx.coroutines#launch__at__kotlinx.coroutines.CoroutineScope(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/Builders.common.kt:56)
	at kotlinx.coroutines.flow.launchSharing#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/flow/operators/Share.kt:209)
	at kotlinx.coroutines.flow#shareIn__at__kotlinx.coroutines.flow.Flow<0:0>(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/flow/operators/Share.kt:147)
	at kotlinx.coroutines.flow#shareIn$default__at__kotlinx.coroutines.flow.Flow<0:0>(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/flow/operators/Share.kt:135)
	at com.yazio.shared.common.property.FrozenPropertyTest.$sharingTest$lambda-1COROUTINE$0.invokeSuspend#internal(/Users/ph1b/repos/yazio/shared/common/src/commonTest/kotlin/com.yazio.shared.common.property/FrozenPropertyTest.kt:29)
	at kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/coroutines/ContinuationImpl.kt:30)
	at kotlinx.coroutines.DispatchedTask#run(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Debug.kt:17)
	at kotlinx.coroutines.EventLoopImplBase#processNextEvent(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/EventLoop.common.kt:281)
	at kotlinx.coroutines#runEventLoop(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:87)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:74)
	at kotlinx.coroutines#runBlocking(/opt/buildAgent/work/6326934d18cfe24e/kotlin/libraries/stdlib/src/kotlin/contracts/ContractBuilder.kt:56)
	at kotlinx.coroutines#runBlocking$default(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:35)
	at com.yazio.shared.common.property.FrozenPropertyTest#sharingTest(/Users/ph1b/repos/yazio/shared/common/src/commonTest/kotlin/com.yazio.shared.common.property/FrozenPropertyTest.kt:28)
	at com.yazio.shared.common.property.$FrozenPropertyTest$test$0.$sharingTest$FUNCTION_REFERENCE$4.invoke#internal(/Users/ph1b/repos/yazio/shared/common/src/commonTest/kotlin/com.yazio.shared.common.property/FrozenPropertyTest.kt:27)
	at com.yazio.shared.common.property.$FrozenPropertyTest$test$0.$sharingTest$FUNCTION_REFERENCE$4.$<bridge-UNNN>invoke(/Users/ph1b/repos/yazio/shared/common/src/commonTest/kotlin/com.yazio.shared.common.property/FrozenPropertyTest.kt:27)
	at kotlin.native.internal.test.BaseClassSuite.TestCase#run(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/TestSuite.kt:85)
	at kotlin.native.internal.test.TestRunner.run#internal(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/TestRunner.kt:245)
	at kotlin.native.internal.test.TestRunner.runIteration#internal(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/TestRunner.kt:271)
	at kotlin.native.internal.test.TestRunner#run(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/TestRunner.kt:286)
	at kotlin.native.internal.test#testLauncherEntryPoint(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/Launcher.kt:30)
	at kotlin.native.internal.test#main(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/Launcher.kt:34)
	at <global>.Konan_start(/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/test/Launcher.kt:33)

@qwwdfsad qwwdfsad added the bug label Jan 12, 2022
@qwwdfsad qwwdfsad self-assigned this Jan 17, 2022
qwwdfsad added a commit that referenced this issue Jan 17, 2022
@petarmarijanovicfive
Copy link

Same here

@PaulWoitaschek
Copy link
Contributor Author

Can we get a hotfix for this? This forces us to stay on 1.5

@qwwdfsad
Copy link
Collaborator

We're aiming to release 1.6.1 along with 1.6.1-native-mt next wekk

@PaulWoitaschek
Copy link
Contributor Author

@qwwdfsad
Next week has passed, do you have any news on this?

@qwwdfsad
Copy link
Collaborator

Sorry for the delay, we had to postpone the release for now. Please stay tuned for the updates

@petarmarijanovicfive
Copy link

@qwwdfsad maybe an ETA for this? It pops up from time to time and we have to create some partial hotfixes because we are waiting for the fix in the new version

qwwdfsad added a commit that referenced this issue Apr 5, 2022
qwwdfsad added a commit that referenced this issue Apr 5, 2022
qwwdfsad pushed a commit that referenced this issue Apr 5, 2022
* Provides newSingleThreadedContext.
* Provides Dispatchers.Main on iOS, Dispatchers.Default everywhere.
* Coroutine references (Job), all kinds of channels and StateFlow are shareable across workers.
* Each individual coroutine is confined to a single worker.
* Update Dispatchers docs to account for native-mt changes.
* Multithreaded support in select expression.
* Fix ObjC autorelease object leaks with native-mt dispatchers (#2477)

Additional fixes:
* Fixed broadcast builder with different thread
* Fixed adding a child to a frozen parent job

Fixes #462
Fixes #470
Fixes #765
Fixes #1645
Fixes #1751
Fixes #1828
Fixes #1831
Fixes #1764
Fixes #2064
Fixes #2025
Fixes #2226
Fixes #2138
Fixes #2263
Fixes #2322
Fixes #2283
Fixes #2688
Fixes #2398
Fixes #3136
@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Apr 6, 2022

1.6.1-native-mt is out

@qwwdfsad qwwdfsad closed this as completed Apr 6, 2022
hichamboushaba added a commit to hichamboushaba/WCStoreApp that referenced this issue Jun 16, 2022
Otherwise the sharing fails with the error: "Cannot start an undispatched coroutine in another thread DefaultDispatcher from current MainThread"
This is supposedly fixed Kotlin/kotlinx.coroutines#3136, but for some reason, the fix doesn't work when the new memory model is enabled
qwwdfsad pushed a commit that referenced this issue Jun 22, 2022
* Provides newSingleThreadedContext.
* Provides Dispatchers.Main on iOS, Dispatchers.Default everywhere.
* Coroutine references (Job), all kinds of channels and StateFlow are shareable across workers.
* Each individual coroutine is confined to a single worker.
* Update Dispatchers docs to account for native-mt changes.
* Multithreaded support in select expression.
* Fix ObjC autorelease object leaks with native-mt dispatchers (#2477)

Additional fixes:
* Fixed broadcast builder with different thread
* Fixed adding a child to a frozen parent job

Fixes #462
Fixes #470
Fixes #765
Fixes #1645
Fixes #1751
Fixes #1828
Fixes #1831
Fixes #1764
Fixes #2064
Fixes #2025
Fixes #2226
Fixes #2138
Fixes #2263
Fixes #2322
Fixes #2283
Fixes #2688
Fixes #2398
Fixes #3136
@chippmann
Copy link

chippmann commented Sep 14, 2022

@qwwdfsad As of 1.6.3-native-mt this issue still exists if one enables the new memory manager:

    targets
        .filterIsInstance<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget>()
        .forEach { kotlinNativeTarget ->
            with(kotlinNativeTarget) {
                binaries.all {
                        // enabling experimental memory model because ktor 2.x does not work otherwise on native atm: https://youtrack.jetbrains.com/issue/KTOR-4260/Ktor-client-Curl-engine-crash
                        binaryOptions["memoryModel"] = "experimental"
                        // because we need the new memory model for native targets because of the issue outlined above, we also want to enable the experimental concurrent mark and sweep gc to reduce gc pauses as much as possible
                        // see: https://kotlinlang.org/docs/whatsnew1620.html#concurrent-implementation-for-the-sweep-phase-in-new-memory-manager
                        freeCompilerArgs += "-Xgc=cms"

                        // better native stacktraces with libbacktrace: https://kotlinlang.org/docs/whatsnew1620.html#better-stack-traces-with-libbacktrace
                        // disadvantage: slightly slower link times; but the better output definitely makes up for it!
                        binaryOptions["sourceInfoType"] = "libbacktrace"
                }
            }
        }

Edit: I just realized that with the new memory manager, coroutines are multithreaded even without the native-mt version.
Sorry for the useless ping. But could you quickly confirm/deny my finding?

@LouisCAD
Copy link
Contributor

Yup, and the new memory manager is becoming the default in 1.7.20 (there's an ongoing RC)

@petarmarijanovicfive
Copy link

@chippmann And yes, you don't need to use the native-mt version anymore 😁

@sigma-stochanskyi
Copy link

Working with 1.6.3-native-mt version on KMM. Facing same issue on iOS. Does someone else have this issue or is there any fix for that?

@LouisCAD
Copy link
Contributor

LouisCAD commented Nov 4, 2022

Stop using the native-mt, not needed anymore with Kotlin 1.7.20+

@remcomokveld
Copy link

Would probably be good to update the documentation here then. Also it is now referring to a 1.6.4-native-mt which doesn't exist

@LouisCAD
Copy link
Contributor

I bet you could get a doc updating PR accepted 🙂

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

No branches or pull requests

8 participants