Skip to content

fix: make checkDirty resilient to graph mutations during update - #110

Merged
johnsoncodehk merged 1 commit into
masterfrom
fix/issue-109-checkdirty-resilience
May 10, 2026
Merged

johnsoncodehk merged 1 commit into
masterfrom
fix/issue-109-checkdirty-resilience

Conversation

@johnsoncodehk

Copy link
Copy Markdown
Collaborator

Summary

Fix checkDirty crashing when update() runs user code that disposes effects or scopes, triggering cascading unlink/unwatched that mutates the dependency graph mid-traversal.

Closes #109

Problem

checkDirty is 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 via sub.subs!. If update() ran a computed getter that called dispose(), the cascading cleanup could set sub.subs = undefined, crashing on the ! assertion.

Three crash variants exist depending on which ! assertion fails:

  • dep.subs! after update(dep)
  • sub.subs! during unwind
  • Stale stack entries after graph mutation

Fix

system.ts — make checkDirty self-contained:

  • Always push to stack before recursing (remove single-subscriber shortcut that relied on sub.subs!)
  • Always recover from stack during unwind
  • Snapshot dep.subs / sub.subs before update() for shallowPropagate — the cascade may unlink subscribers during update, but the stale chain is safe to walk (disposed nodes have flags=None, skipped by the Pending check)
  • return dirty && !!sub.flags — after full unwind, sub is always the root subscriber; if it was disposed during traversal (flags=None=0), return false

index.ts — run() else branch:

  • } else if (e.flags) { — don't set a disposed effect (flags=0) back to Watching

Test plan

  • issue_109.spec.ts — original crash: dispose effect during computed update
  • issue_109_variant.spec.ts — dep.subs undefined after update
  • issue_109_scope.spec.ts — dispose effectScope during computed update
  • issue_109_stale.spec.ts — disposed effect should not re-run
  • issue_109_leak.spec.ts — disposed effect not re-notified on later updates
  • issue_109_edge.spec.ts — self-dispose semantics, fn-body completion, graph cleanup
  • issue_109_shallow.spec.ts — shallowPropagate walks stale link after sibling disposal
  • All 53 tests pass

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).
@johnsoncodehk
johnsoncodehk merged commit ad52894 into master May 10, 2026
2 checks passed
@johnsoncodehk
johnsoncodehk deleted the fix/issue-109-checkdirty-resilience branch May 10, 2026 00:16
@johnsoncodehk johnsoncodehk mentioned this pull request May 12, 2026
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.
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

Successfully merging this pull request may close these issues.

Bug: Computed update can error if the update caused it to be unwatched

1 participant