Skip to content

Unsubscribing when using in a multi threaded way throws Exceptions #165

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

Open
christopherperry opened this issue Oct 16, 2024 · 1 comment

Comments

@christopherperry
Copy link

Hey team, I'm using a thread safe store via createTypedThreadSafeStore(). My code is roughly:

storeSubscription = store.subscribe {
    coroutineScope.launch { ui.update(store.state) }
}

actionFlow.onEach { action ->
  store.dispatch(action)
}.flowOn(Dispatchers.Default)
 .launchIn(coroutineScope)

Then in the shutdown part of the lifecycle I call on the main thread:

storeSubscription?.invoke()

and it sometimes throws the error

You may not unsubscribe from a store listener while the reducer
|is executing. See
|https://www.reduxkotlin.org/api/store#subscribelistener-storesubscriber
|for more details.

I noticed in your samples you don't unsubscribe. How do I use your library in a multi-threaded way and safely unsubscribe?

@inpt333
Copy link

inpt333 commented Jan 15, 2025

I have the same issue, although I am using createThreadSafeStoreEnhancer to avoid multithreading issues, it doesn't seem to be enough for unsubscribe. The issue seems to come from here:

https://github.com/reduxkotlin/redux-kotlin/blob/master/redux-kotlin/src/commonMain/kotlin/org/reduxkotlin/CreateStore.kt#L135

I am wondering:

  • Is it even needed to check if the store is dispatching to unsubscribe?
  • If so, what about making the unsubscribe also synchronized?

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

2 participants