fix(core): stale computed values after a batch reverts a signal - #947
Merged
Conversation
Rolling the version back during batch snapshot reconciliation let a later write re-mint a version number that a lazy computed had already observed mid-batch, permanently serving a stale cached value. Fast-forward subscriber nodes that saw the pre-batch version instead.
✅ Deploy Preview for preact-signals-demo ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
🦋 Changeset detectedLatest commit: 5fab6c2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
Size Change: +394 B (+0.22%) Total Size: 180 kB 📦 View Changed
ℹ️ View Unchanged
|
marvinhagemeister
approved these changes
Jul 5, 2026
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.
Root cause
reconcileBatchSnapshotsrolls a signal's_versionbackwards when a batch ends with the value equal to its pre-batch state. Version numbers are the only invalidation mechanism, and they must be monotonic: a lazy (unsubscribed) computed that reads the signal during the batch records the intermediate version on its dependency node. After the rollback, the next distinct write re-mints that exact version number for a different value, soneedsToRecomputesees matching versions and the computed serves its stale cache — permanently, on every read, even though the signal holds a new value:Reading a modified signal inside a batch is explicitly supported per
batch's documentation, and this shipped in@preact/signals-core@1.14.0.Fix
Never roll versions back. Instead, when a batch reverts a value, fast-forward the subscriber nodes that last saw the pre-batch version to the current version. Subscribed effects/computeds still skip recomputing on no-op batches (the point of the original optimization), while any node that observed an intermediate version keeps its mismatch and recomputes correctly. Lazy computeds that saw only the pre-batch version now do one value-equal recompute instead of being skipped — trading a micro-optimization for correctness.
Note: this PR was authored by Claude and @JoviDeCroock.