badoo / Reaktive

Kotlin multi-platform implementation of Reactive Extensions
Apache License 2.0
1.18k stars 58 forks source link

Native iOS crash #72

Closed kpgalligan closed 5 years ago

kpgalligan commented 5 years ago

I have a forked version from a few days ago, so almost master. The following crashes, and in an unpleasant way:

class FailTests{
    init {
        ensureNeverFrozen()
    }

    fun loadData(){
        observableOf(listOf("arst"))
                .observeOn(mainScheduler)
                .subscribe {
                    showData(it)
                }
    }

    fun showData(a: Any){

    }
}

The lamba to subscribe captures the object instance of FailTests, but that has had ensureNeverFrozen() called on it, so it should crash. However, rather than report an unhandled exception, we get a low level C++ runtime crash.

/Users/teamcity/buildAgent/work/4d622a065c544371/runtime/src/main/cpp/Weak.cpp:35: runtime assert: Incorrect lock state

From the stack in xcode

#0  0x000000010e87d23e in __pthread_kill ()
#1  0x000000010e8d0c1c in pthread_kill ()
#2  0x000000010e64701d in abort ()
#3  0x000000010a3c52b9 in konan::abort() ()
#4  0x000000010a3c51d3 in RuntimeAssertFailed(char const*, char const*) ()
#5  0x000000010a3c87d8 in WeakReferenceCounterClear ()
#6  0x000000010a3c5d30 in FreeContainer(ContainerHeader*) ()
#7  0x000000010a3d8cdd in LeaveFrame ()
#8  0x000000010a163cf7 in kfun:com.badoo.reaktive.utils.atomicreference.AtomicReference.getAndSet(#GENERIC)#GENERIC at /Users/kgalligan/temp/Reaktive/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomicreference/AtomicReference.kt:26
#9  0x000000010a372ff1 in kfun:com.badoo.reaktive.scheduler.MainScheduler.ExecutorImpl.Operation.invoke#internal at /Users/kgalligan/temp/Reaktive/reaktive/src/iosCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt:67
#10 0x000000010a372e47 in kfun:com.badoo.reaktive.scheduler.MainScheduler.ExecutorImpl.Operation.$<bridge-UNN>invoke()#internal at /Users/kgalligan/temp/Reaktive/reaktive/src/iosCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt:60

The assert is coming from kotlin native's runtime. It looks like something in interop, but that's a complex sequence and a very unexpected error. If you remove ensureNeverFrozen() or avoid freezing (https://github.com/touchlab/DroidconKotlin/blob/kpg/reaktive/sessionize/lib/src/commonMain/kotlin/co/touchlab/sessionize/reaktive/ReaktiveExtensions.kt#L65), you avoid the crash.

kpgalligan commented 5 years ago

Branch in my fork showing crash in iOS sample. The actual crash reports differently there, but it is still misleading as to the cause: https://github.com/kpgalligan/Reaktive/tree/kpg/failcrash

arkivanov commented 5 years ago

@kpgalligan Thanks for reporting! Would you mind to check how it works on https://github.com/badoo/Reaktive/pull/73 ?

kpgalligan commented 5 years ago

Yes, much better!


Uncaught exception: kotlin.native.concurrent.FreezingException: freezing of com.badoo.reaktive.disposable.DisposableWrapper.Holder@1d65f28 has failed, first blocker is co.touchlab.sessionize.ScheduleModel@871c08kotlin.native.concurrent.FreezingException: freezing of com.badoo.reaktive.disposable.DisposableWrapper.Holder@1d65f28 has failed, first blocker is co.touchlab.sessionize.ScheduleModel@871c08
        at 0   iosApp                              0x0000000109447226 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 70
        at 1   iosApp                              0x0000000109447146 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 70
        at 2   iosApp                              0x00000001096dbf66 kfun:kotlin.native.concurrent.FreezingException.<init>(kotlin.Any;kotlin.Any)kotlin.native.concurrent.FreezingException + 230
        at 3   iosApp                              0x00000001096dc12a ThrowFreezingException + 90
        at 4   iosApp                              0x00000001096f2d80 FreezeSubgraph + 368
        at 5   iosApp                              0x0000000109447957 kfun:kotlin.native.concurrent.freeze@#GENERIC.()Generic + 55
        at 6   iosApp                              0x0000000109475058 kfun:com.badoo.reaktive.utils.freeze$reaktive@#GENERIC.()Generic + 72
        at 7   iosApp                              0x0000000109474f65 kfun:com.badoo.reaktive.utils.atomicreference.AtomicReference.freezeIfNeeded#internal + 85
        at 8   iosApp                              0x000000010948043f kfun:com.badoo.reaktive.utils.atomicreference.AtomicReference.getAndSet(#GENERIC)#GENERIC + 95
        at 9   iosApp                              0x0000000109480d8b kfun:com.badoo.reaktive.disposable.DisposableWrapper.setHolder#internal + 91
        at 10  iosApp                              0x00000001094811b2 kfun:com.badoo.reaktive.disposable.DisposableWrapper.set(com.badoo.reaktive.disposable.Disposable?) + 98
        at 11  iosApp                              0x0000000109481007 kfun:com.badoo.reaktive.observable.<no name provided>_7.setDisposable_11#internal + 71
        at 12  iosApp                              0x00000001094815a7 kfun:co.touchlab.sessionize.reaktive.asObservable$lambda-0#internal + 151
        at 13  iosApp                              0x00000001094814c7 kfun:co.touchlab.sessionize.reaktive.$asObservable$lambda-0$FUNCTION_REFERENCE$94.invoke#internal + 71
        at 14  iosApp                              0x000000010948142b kfun:co.touchlab.sessionize.reaktive.$asObservable$lambda-0$FUNCTION_REFERENCE$94.$<bridge-UNNN>invoke(#GENERIC)#internal + 75
        at 15  iosApp                              0x000000010948090a kfun:com.badoo.reaktive.observable.<no name provided>_1.subscribe_2#internal + 266
        at 16  iosApp                              0x0000000109472a5e kfun:com.badoo.reaktive.observable.subscribeSafe$reaktive@com.badoo.reaktive.observable.Observable<#GENERIC>.(com.badoo.reaktive.observable.ObservableObserver<#GENERIC>)Generic + 110
        at 17  iosApp                              0x0000000109472708 kfun:com.badoo.reaktive.observable.observeOn$lambda-2#internal + 296
        at 18  iosApp                              0x000000010947259b kfun:com.badoo.reaktive.observable.$observeOn$lambda-2$FUNCTION_REFERENCE$108.invoke#internal + 75
        at 19  iosApp                              0x00000001094724fb kfun:com.badoo.reaktive.observable.$observeOn$lambda-2$FUNCTION_REFERENCE$108.$<bridge-UNNN>invoke(#GENERIC)#internal + 75
        at 20  iosApp                              0x000000010948090a kfun:com.badoo.reaktive.observable.<no name provided>_1.subscribe_2#internal + 266
        at 21  iosApp                              0x0000000109472a5e kfun:com.badoo.reaktive.observable.subscribeSafe$reaktive@com.badoo.reaktive.observable.Observable<#GENERIC>.(com.badoo.reaktive.observable.ObservableObserver<#GENERIC>)Generic + 110
        at 22  iosApp                              0x0000000109472708 kfun:com.badoo.reaktive.observable.observeOn$lambda-2#internal + 296
        at 23  iosApp                              0x000000010947259b kfun:com.badoo.reaktive.observable.$observeOn$lambda-2$FUNCTION_REFERENCE$108.invoke#internal + 75
        at 24  iosApp                              0x00000001094724fb kfun:com.badoo.reaktive.observable.$observeOn$lambda-2$FUNCTION_REFERENCE$108.$<bridge-UNNN>invoke(#GENERIC)#internal + 75
        at 25  iosApp                              0x000000010948090a kfun:com.badoo.reaktive.observable.<no name provided>_1.subscribe_2#internal + 266`
arkivanov commented 5 years ago

Glad that it helped! I have another question, are you testing on simulator? If so then is there any chance you could test it on a real iOS device? We are exploring the idea of sharing common code with iOS team but their concern is unreadable stack traces (without debug symbols, only offsets in memory). So I would like to see how they actually look.

kpgalligan commented 5 years ago

That’s a complex topic. My current focus is making the iOS dev experience better in general. For example, kotlin Xcode plugin https://github.com/touchlab/xcode-kotlin. There are crashes locally, and crashes in release builds with Crashlytics (etc). We’re actively working on improving the latter. I’m on my phone now but can chat another time. If you’ve got an email address or you’re on the kotlin slack that would be better for some details

arkivanov commented 5 years ago

That’s a complex topic. My current focus is making the iOS dev experience better in general. For example, kotlin Xcode plugin https://github.com/touchlab/xcode-kotlin. There are crashes locally, and crashes in release builds with Crashlytics (etc). We’re actively working on improving the latter. I’m on my phone now but can chat another time. If you’ve got an email address or you’re on the kotlin slack that would be better for some details

Thanks, dropped you a message in slack.