question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Threading issues even with createThreadSafeStore

See original GitHub issue

Hi there! Recently began using this library, and have noticed some hard-to-reproduce errors popping up while developing. The errors occur inconsistently and mention threading issues:

You may not call store.subscribe() while the reducer is executing. If you would like to be notified after the store has been updated, subscribe from a component and invoke store.getState() in the callback to access the latest state. See https://www.reduxkotlin.org/api/store#subscribelistener-storesubscriber for more details. You may be seeing this due accessing the store from multiplethreads. Try createThreadSafeStore() https://reduxkotlin.org/introduction/threading

and

You may not call store.getState() while the reducer is executing. The reducer has already received the state as an argument. Pass it down from the top reducer instead of reading it from the store. You may be accessing getState while dispatching from another thread. Try createThreadSafeStore(). https://reduxkotlin.org/introduction/threading

However we do use createThreadSafeStore, and these errors are being thrown from code outside our reducers (in thunks and views), so I am surprised to see these errors.

We are using:

org.reduxkotlin:redux-kotlin-threadsafe:0.5.5
org.reduxkotlin:reselect:0.5.5
org.reduxkotlin:redux-kotlin-thunk:0.5.5

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
jennkaocommented, Apr 21, 2021

Hey there! I’m investigating this issue with @zackls and after some debugging, we think that the cause for this error may be related to the createThreadSafeStore implementation and wanted to run that by you guys. For context, we were also using the redux-kotlin-thunk middleware, and our store implementation looked like:

val store = createThreadSafeStore(
  rootReducer,
  initialState,
  applyMiddleware(
    createThunkMiddleware(),
  )
)

With this implementation, it seems like middlewares are applied to enhance the store’s dispatch function before the store’s functions get synchronized. For us, this meant that our thunks were accessing a non-synchronized dispatch function, whereas outside of thunks, invocations to dispatch were synchronized. Since our application makes use of multiple threads and some invocations to dispatch were not synchronized, we were seeing the reported error indicating that we weren’t using createThreadSafeStore.

We solved this in our application by creating a new store enhancer to handle synchronization, and ordering it so that store creation / synchronization happens before middlewares are applied. Something like:

fun <State> synchronizeStore(): StoreEnhancer<State> {
  return { storeCreator ->
    { reducer, initialState, en: Any? ->
      val store = storeCreator(reducer, initialState, en)
      val synchronizedStore = SynchronizedStore(store)
      synchronizedStore
    }
  }
}

val store = createStore(
  rootReducer,
  initialState,
    compose(
      applyMiddleware(
        createThunkMiddleware(),
      ),
      synchronizeStore(),
    ),
)

Happy to open a PR or discuss further if that all checks out!

2reactions
patjackson52commented, Aug 2, 2021

Any movement on this, or the PR? It’s easy enough to drop the function in, but it’d be nice not to have to.

PR looks good. I requested a small rename change. Hope to have it merged this week.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux on Multithreaded Platforms - PatrickJackson.dev
Given Javascript is single threaded its just not an issue. Working in JVM or Native suddenly presents a new set of challenges.
Read more >
Redux on Multi-threaded Platforms - ReduxKotlin
Redux in multi-threaded environments brings additional concerns that are not present in redux for Javascript. Javascript is single threaded, so Redux.js did not ......
Read more >
"Thread safety" in Redux? - Stack Overflow
It's not real threads, just for the lack of better wording. Actions are asynchronous and state keys are being accessed by reference. –...
Read more >
Redux - Kotlinlang
Hey y all wave ran into some build issues e… ... a thread-safe store • recommended way to create the store is now...
Read more >
How To Resolve Multi-Threading Problems - Digikey
How to Resolve Multi-Threading Problems in Concurrent C Applications ... OS lets many other threads run before thread zero even finishes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found