KStateMachine / kstatemachine

KStateMachine is a Kotlin DSL library for creating state machines and statecharts.
https://kstatemachine.github.io/kstatemachine/
Boost Software License 1.0
339 stars 19 forks source link

Getting crash if I try to use `machine.startBlocking` from machine created with `createStdLibStateMachine` #93

Closed TE-PreetamBhosle closed 4 months ago

TE-PreetamBhosle commented 4 months ago

Getting below crash if I try to use the state machine with createStdLibStateMachine.

I am not using kstatemachine-coroutines.

And as given here If you can not have dependency on Kotlin Coroutines or just do not want to use it in the documentation, if I use suspendable version of machine.start then machine takes long time to start thereby delaying the app launch.

The app quickly starts if i use startBlocking but fails with below exception.

If I use createStdLibStateMachine, do I have to use suspendable versions of start/stop/processEvents OR is there a way out?  

FATAL EXCEPTION: main
Process: com.kstamachinesample, PID: 26183
java.lang.RuntimeException: Unable to create application com.kstamachinesample.MyApplication: java.lang.IllegalStateException: Seems that you are trying to use Kotlin Coroutines library from KStateMachine callbacks, use kstatemachine-coroutines support library to make that work
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6724)
    at android.app.ActivityThread.access$1300(ActivityThread.java:237)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7664)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalStateException: Seems that you are trying to use Kotlin Coroutines library from KStateMachine callbacks, use kstatemachine-coroutines support library to make that work
    at ru.nsk.kstatemachine.StdLibCoroutineAbstraction.runBlocking(CoroutineAbstraction.kt:34)
    at ru.nsk.kstatemachine.StateMachineKt.startBlocking(StateMachine.kt:130)
    at com.kstamachinesample.statemachine.MyStateMachine$Companion.start(MyStateMachine.kt:729)
    at com.kstamachinesample.MyApplication.initStateMachine(MyApplication.kt:83)
    at com.kstamachinesample.MyApplication.onCreate(MyApplication.kt:75)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6719)
    at android.app.ActivityThread.access$1300(ActivityThread.java:237) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7664) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property result has not been initialized
    at ru.nsk.kstatemachine.StdLibCoroutineAbstraction.runBlocking(CoroutineAbstraction.kt:32)
    at ru.nsk.kstatemachine.StateMachineKt.startBlocking(StateMachine.kt:130) 
    at com.kstamachinesample.statemachine.MyStateMachine$Companion.start(MyStateMachine.kt:729) 
    at com.kstamachinesample.MyApplication.initStateMachine(MyApplication.kt:83) 
    at com.kstamachinesample.MyApplication.onCreate(MyApplication.kt:75) 
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192) 
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6719) 
    at android.app.ActivityThread.access$1300(ActivityThread.java:237) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7664) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
nsk90 commented 4 months ago

Have you solved the issue?

TE-PreetamBhosle commented 4 months ago

Have you solved the issue?

starting it this way gave no error

machineScope.launch {
   machine.start(null)
}

but since i am creating machine using createStdLibStateMachine not sure If i should use start machine inside a coroutine scope.

nsk90 commented 4 months ago

this looks strange. first of all if you have kotlin-coroutines avialable in your project, you should use it with kstatemachine. what variable machineScope is referred to?

you have mentioned that suspendable version take long time to execute. how long it takes? actually there should not be any visible difference in execution time for any machine startup case. can you analyze were the time is wasted?