Tracker.autorun created inside useEffect can wrongly inherit a parent computation

I was encountering some strange bugs where my autoruns weren’t re-running. Eventually I noticed that when my autorun is created, Tracker.currentComputation is set:

This caused my autorun to wrongly inherit the computation as parent, and to be stopped when the parent was stopped.

I traced this to the forceUpdate call inside Tracker:

It seems that Tracker calls this as part of processing the parent computation, but doesn’t anticipate that new computations could be created within useEffects, etc.

One solution might be to unset Tracker.currentComputation before calling forceUpdate, but I suspect Tracker relies on this, as per this comment:

// For any reactive change, forceUpdate and let the next render rebuild the computation.
forceUpdate();

Another solution might be to add a flag like ignoreParent to Tracker for the duration of the forceUpdate call. Perhaps someone here has a better idea?

At the very least, we could add a note to the docs to warn people to wrap autoruns created within useEffects with Tracker.nonReactive?

1 Like