ReSwift icon indicating copy to clipboard operation
ReSwift copied to clipboard

Crash when dispatching actions from inside StoreSubscriber.newState()

Open gmorning opened this issue 2 years ago • 2 comments
trafficstars

Screenshot 2022-12-07 at 18 16 47

We have several actions that are chained one after another in this manner:

  1. Action is dispatched
  2. A StoreSubscriber receives newState callback on state change
  3. It dispatches another Action on the same Store
  4. Another StoreSubscriber (or maybe the same one) receives newState callback on state change
  5. It dispatches another Action on the same Store

These chains are not infinitely recursive, their depth is 2-4 actions. They all operate on single (main) thread synchronously. We see a crash when 2nd or 3rd dispatch is called inside ReSwift. It occurs with different action sequences. One case we can reproduce 100% in debug configuration (-Onone), but it's not reproduced in release configuration.

Stacktrace top is __swift_instantiateConcreteTypeFromMangledName

gmorning avatar Dec 07 '22 15:12 gmorning

I recommend against this pattern -- It could be running into call-stack or memory-stack restrictions, due to not allowing the runloop to complete and reset.

If you need to, dispatching the chained actions should resolve the issue, as they'll occur on the next runloop and have the available resources again.

Better is to handle the state changes within the singular, first action. If its all synchronous, then technically there is no need for the follow-up actions. You can add the first action to the reducers appropriately.

mjarvis avatar Dec 07 '22 16:12 mjarvis

@mjarvis This would be an example of (ab)using ReSwift as a general-purpose tool to decouple and implement messaging between components, something we brushed a couple of years ago, right?

DivineDominion avatar Dec 08 '22 17:12 DivineDominion