Skip to content

fix: stop pooled binding contexts from reviving block params after 255 reuses - #706

Closed
stencil-harness[bot] wants to merge 1 commit into
masterfrom
robot/issue-705
Closed

stencil-harness[bot] wants to merge 1 commit into
masterfrom
robot/issue-705

Conversation

@stencil-harness

Copy link
Copy Markdown
Contributor

Fixes #705. This carries the fix from #704 by @nadrummond unchanged (same tree, credited via Co-authored-by), verified independently.

Problem

FixedSizeDictionary clears cheaply: Reset() / OptionalClear() bump a byte version 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 their BlockParamsObject / ContextDataObject go through OptionalClear() 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 any IHandlebars instance) 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}} with x = "mine" prints the other render's secret on a regular cycle instead of [mine].

Change

Reset() and Clear() now advance the version through a private NextVersion(). When the version wraps, it wipes _entries and _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.

  • Public API: unchanged.
  • Behaviour: only the leak goes away. No template's correct output changes.
  • Cost: one clear of both arrays every 255 resets per dictionary. The per-return hot path is otherwise unchanged.

Tests

  • FixedSizeDictionaryTests.EntryFromAnEarlierUseStaysGoneOnceTheVersionWraps: a theory over Reset and OptionalClear. 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:

  • With the fix: the full suite passes on net10.0 (2,352 tests).
  • With only the tests and the old 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.WellKnownVariables caches EntryIndex values into ContextDataObject and 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

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>
@sonarqubecloud

Copy link
Copy Markdown

@stencil-harness

Copy link
Copy Markdown
Contributor Author

Superseded by #704, which has the same change and was opened first by its author. This PR adds nothing beyond it and should be closed in favour of #704.

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.

A render can print a block param bound by an earlier, unrelated render (pooled context reuse)

0 participants