Giter Site home page Giter Site logo

Comments (11)

nsk90 avatar nsk90 commented on June 3, 2024 1

not, actually

GlobalScope.launch { // any scope/context, it does not matter

      val machineScope = CoroutineScope(newSingleThreadContext("your context")) // must be single threaded, you should cancel it when you complete working with the machine.
 
      val machine = createStateMachine(machineScope, ....) {...} // this scope/context matters
      //...

      // the library will execute all suspendable functions from machineScope context
      machine.processEvent(...) // will switch to machineScope internally, so you can call it from any thread and context.
      // please not that it is only about suspendable library functions. all other non-suspendable api cannot be used in 
      // multithreaded environment without proper synchronization.
} 

from kstatemachine.

nsk90 avatar nsk90 commented on June 3, 2024

Hi, could you provide a code sample facing this issue?

from kstatemachine.

nsk90 avatar nsk90 commented on June 3, 2024

Is the falling StateMachine called from multiple threads? Pease specify with which scope do you call createStateMachine function.

from kstatemachine.

preetb123 avatar preetb123 commented on June 3, 2024

Is the falling StateMachine called from multiple threads? Pease specify with which scope do you call createStateMachine function.

@nsk90 , thank you very much for replying.
I called createStateMachine from GlobalScope.launch{}.

from kstatemachine.

preetb123 avatar preetb123 commented on June 3, 2024

@nsk90
for now I have made following modification and so far it did not crash. But not sure if it is the right way to do

  private val SINGLE_THREAD = newSingleThreadContext("mvi")
  
  fun sendEvent(event: AppEvent): Unit = intent {
    sendEffect(ModelEffect.ControlEventSent(event))
    val machine = NysnoApplication.getApp().machine
    GlobalScope.launch {
      withContext(SINGLE_THREAD){
        machine?.processEvent(event)
      }
    }
  }

from kstatemachine.

nsk90 avatar nsk90 commented on June 3, 2024

I think the crash is related to GlobalScope usage. As it uses Default coroutine dispatcher internally (correct?). This dispatcher uses thread pool, not a single thread. So processEvent may be executed concurrenly, which is wrong. You should pass single threaded coroutine context into createStateMachine function.
Try it. If this does not help, I will try to reproduce your issue locally to debug.

from kstatemachine.

preetb123 avatar preetb123 commented on June 3, 2024

@nsk90
is this fine?

GlobalScope.launch {
  withContext(singleThread){
    machine = createAndStartStateMachine()
    machine?.start(null)
    printStateMachine()  
  }
}

from kstatemachine.

nsk90 avatar nsk90 commented on June 3, 2024

yeah this looks better, please inline createAndStartStateMachine() body, so is can see pure library methods, to give exact answer.

I think you don't need GlobalScope at all.
Having separate single thread you can create your own CoroutineScope from it, and use the latter as createStateMachine argument.

This is not critical and does not change the way how it should work, both cases are effectively equivalent.

Something like this:

CoroutineScope(newSingleThreadContext("your context"))
// or
CoroutineScope(Dispatchers.Default.limitedParallelism(1))

from kstatemachine.

preetb123 avatar preetb123 commented on June 3, 2024

this is how createAndStartStateMachine looks. Actually it took lot of time to create if I pass start = true while creating and I got machine variable not initialized error in sendEvent function while accessing machine, so had to change it this way.

suspend fun createAndStartStateMachine(): StateMachine {
  return createStateMachine(GlobalScope, "MyAppState", ChildMode.PARALLEL, start = false) {
    logger = StateMachine.Logger {
      Timber.tag("StateMachine").d(it())
    }
    
    state("AppState") {
      addInitialState(AppState.LoginState) {
        ....
      }
      ...
    }
  }
}

from kstatemachine.

preetb123 avatar preetb123 commented on June 3, 2024

CoroutineScope(Dispatchers.Default.limitedParallelism(1))

you mean like this?

val applicationScope = CoroutineScope(Dispatchers.Default.limitedParallelism(1))
applicationScope.launch {
  machine = createAndStartStateMachine()
  machine?.start(null)
  printStateMachine()
}

from kstatemachine.

preetb123 avatar preetb123 commented on June 3, 2024

not, actually

GlobalScope.launch { // any scope/context, it does not matter

      val machineScope = CoroutineScope(newSingleThreadContext("your context")) // must be single threaded, you should cancel it when you complete working with the machine.
 
      val machine = createStateMachine(machineScope, ....) {...} // this scope/context matters
      //...

      // the library will execute all suspendable functions from machineScope context
      machine.processEvent(...) // will switch to machineScope internally, so you can call it from any thread and context.
      // please not that it is only about suspendable library functions. all other non-suspendable api cannot be used in 
      // multithreaded environment without proper synchronization.
} 

thank you very much @nsk90

from kstatemachine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.