Repository navigation
fix: stop pooled binding contexts from reviving block params after 255 reuses - #706
Closed
stencil-harness[bot] wants to merge 1 commit into
Closed
stencil-harness[bot] wants to merge 1 commit into
stencil-harness[bot] wants to merge 1 commit into
Conversation
Reset() and OptionalClear() bump a byte version and leave old entries in place, counting them as absent while their version differs. Once the version wraps from 255 back to 1, an entry written 255 versions earlier matches again, so a pooled BindingContext could hand a block param bound by one render to a later, unrelated render. When the version wraps, the entries and indexes are now wiped before version 1 is reused. Taken from #704 by @nadrummond. Co-authored-by: Noor Drummond <noor@ankabut.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Contributor
Author
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.



Fixes #705. This carries the fix from #704 by @nadrummond unchanged (same tree, credited via
Co-authored-by), verified independently.Problem
FixedSizeDictionaryclears cheaply:Reset()/OptionalClear()bump abyteversion and leave old entries in the arrays, counting them as absent only while their version differs.Clear()only wipes entries of the current version. When the version wraps 255 → 1, an entry written 255 versions earlier matches the current version again.BindingContexts are pooled process-wide, and theirBlockParamsObject/ContextDataObjectgo throughOptionalClear()on every return (Pools/BindingContext.Pool.cs). Block params are looked up before the model's members (BindingContext.TryGetVariable), so a block param bound by one render could reappear in a later, unrelated render (from anyIHandlebarsinstance) and shadow that render's own data. Example from the issue: after{{#each secrets as |x|}}{{/each}}renders once,{{#with this as |y|}}[{{x}}]{{/with}}withx = "mine"prints the other render's secret on a regular cycle instead of[mine].Change
Reset()andClear()now advance the version through a privateNextVersion(). When the version wraps, it wipes_entriesand_indexes(Array.Clear) before reusing version 1, so no entry can outlive a full version cycle. Any entry carrying the current version must therefore have been written during the current use.Tests
FixedSizeDictionaryTests.EntryFromAnEarlierUseStaysGoneOnceTheVersionWraps: a theory overResetandOptionalClear. It adds a key, resets 255 times, adds another key, and checks that the first key stays absent. Deterministic.BindingContextPoolTests.BlockParamBoundInOneRenderIsNotSeenByLaterRenders, in a non-parallel collection: an end-to-end check through the shared pool.Results:
FixedSizeDictionary.cs: all 3 new tests fail, and the pool test reports the leaked value.Comparison with #704
The code is identical to #704. This PR exists so the fix can be reviewed and merged from the main repository. Either PR can be merged; closing the other afterwards is enough.
Note (not changed here)
BindingContext.WellKnownVariablescachesEntryIndexvalues intoContextDataObjectand is not reset when a context goes back to the pool. With this fix, a stale cached index can only reach an empty slot or an entry written during the current render, so it can't leak data across renders. Clearing that array in the pool's return policy would be a cheap extra hardening and could be a follow-up.Release recommendation
Patch: a bug and data-isolation fix with no API or intended behaviour change.
🤖 Generated with Claude Code
Closes #705