Repository navigation
fix: make checkDirty resilient to graph mutations during update - #110
Merged
Merged
Conversation
checkDirty used sub.subs! to recover traversal state during unwind, which crashes when update() runs user code that disposes effects/scopes and triggers cascading unlink/unwatched. Always push to stack before recursing, snapshot subs before update for shallowPropagate, and return false when the root subscriber was disposed during traversal (flags=None=0).
2 tasks done
johnsoncodehk
added a commit
that referenced
this pull request
May 14, 2026
When an inner effect re-runs on its own (its own dep changed), notify() walks up the parent chain to add ancestors to the queue and clears their Watching flag to prevent duplicate notification. The parent then runs but isn't dirty/pending, so run() takes the "not dirty" branch. Before #109/#110, that branch unconditionally restored Watching. The fix in #109/#110 added `if (e.flags)` to avoid restoring Watching on disposed effects (whose flags were zeroed during checkDirty's traversal). Unfortunately, parents added to the queue via the chain also have flags === 0 (notify just cleared Watching, no other flag set), so they fell into the same branch and lost Watching permanently — the outer effect stopped responding to its own dep after any inner re-run. Disposed effects also have deps === undefined (purgeDeps clears them). Use that as the more precise discriminator: restore Watching when the effect still has deps, skip when fully disposed. Fixes #115. Added a regression test mirroring the report.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
checkDirtycrashing whenupdate()runs user code that disposes effects or scopes, triggering cascadingunlink/unwatchedthat mutates the dependency graph mid-traversal.Closes #109
Problem
checkDirtyis an iterative graph traversal that uses the graph itself for state recovery. When recursing into a single-subscriber dependency, it skipped the stack push and recovered the link during unwind viasub.subs!. Ifupdate()ran a computed getter that calleddispose(), the cascading cleanup could setsub.subs = undefined, crashing on the!assertion.Three crash variants exist depending on which
!assertion fails:dep.subs!afterupdate(dep)sub.subs!during unwindFix
system.ts— makecheckDirtyself-contained:sub.subs!)dep.subs/sub.subsbeforeupdate()forshallowPropagate— the cascade may unlink subscribers during update, but the stale chain is safe to walk (disposed nodes haveflags=None, skipped by thePendingcheck)return dirty && !!sub.flags— after full unwind,subis always the root subscriber; if it was disposed during traversal (flags=None=0), return falseindex.ts—run()else branch:} else if (e.flags) {— don't set a disposed effect (flags=0) back toWatchingTest plan
issue_109.spec.ts— original crash: dispose effect during computed updateissue_109_variant.spec.ts—dep.subsundefined after updateissue_109_scope.spec.ts— dispose effectScope during computed updateissue_109_stale.spec.ts— disposed effect should not re-runissue_109_leak.spec.ts— disposed effect not re-notified on later updatesissue_109_edge.spec.ts— self-dispose semantics, fn-body completion, graph cleanupissue_109_shallow.spec.ts— shallowPropagate walks stale link after sibling disposal