Skip to content

Reset BindingContext.WellKnownVariables when a context is returned to the pool #707

In review

Description

@stencil-harness

Follow-up hardening from #705, which is fixed by #704. This is separate from that fix and isn't needed for it.

Current behaviour

BindingContext.WellKnownVariables (source/Handlebars/BindingContext.cs) caches EntryIndex values that point into ContextDataObject, one per data variable (@root, @index, @key, @first, @last, …). The pool's return policy (BindingContextPolicy.Return in source/Handlebars/Pools/BindingContext.Pool.cs) clears BlockParamsObject and ContextDataObject, but it leaves this array as it is. A pooled context therefore keeps indexes written by whatever render used it last.

A stale index is ignored only while its version differs from the dictionary's current version. Once the byte version wraps, a stale index can carry the current version again. Readers trust that index:

  • BindingContext.TryGetContextVariable, through ContextDataObject.TryGetValue(in EntryIndex)
  • DataValues' indexer, which only checks IsNotEmpty and then the version

As a result, a lookup like @index in a frame that never set it can resolve to whatever entry the current render stored in that slot, under a different key.

With #704 the slots are wiped on wrap, so a stale index can only reach an empty slot or data from the current render. Nothing leaks across renders, but within a single render the value can belong to the wrong key.

A related detail: FixedSizeDictionary.AdjustIndexes restamps every source index with the destination's version, including empty ones. A parent's unset slot therefore becomes a "current" index to slot 0 in the child.

Proposed change

  • In BindingContextPolicy.Return, clear the array: Array.Clear(item.WellKnownVariables, 0, item.WellKnownVariables.Length). A default EntryIndex has version 0, which never matches a live dictionary version (always ≥ 1).
  • Optionally, make AdjustIndexes keep empty indexes empty: source[i].IsNotEmpty ? new EntryIndex<TKey>(source[i].Index, destination._version) : default.

The cost is clearing an 8-element struct array per context return. There is no public API change.

Testing

Add a test in BindingContextPoolTests that checks data variables never resolve to another key's value, even after the version wraps. For example, render {{#with this as |y|}}[{{@index}}]{{/with}} repeatedly after templates that populate @index, and expect [] every time. This is hard to make fully deterministic through the shared pool, so a focused unit test on AdjustIndexes and the return policy may be the better primary check.

Activity

  1. stencil-harness commented on Nov 26, 2026

    @stencil-harness
    ContributorAuthor

    I'm the agent responsible for managing intake of issues, acting under @rexm's steering. I understand this asks to clear BindingContext.WellKnownVariables when a context goes back to the pool, and optionally to keep empty indexes empty in FixedSizeDictionary.AdjustIndexes. I'll check whether I can handle this on my own.

  2. stencil-harness commented on Nov 26, 2026

    @stencil-harness
    ContributorAuthor

    I've opened #708 with both changes proposed here. It's staged for review and not merged or released.

    Handlebars-Net/Handlebars.Net (branch robot/issue-707, left on the branch):

    • FixedSizeDictionary.AdjustIndexes now only carries over indexes that are non-empty and current in the source dictionary. All others become default.
    • BindingContextPolicy.Return clears WellKnownVariables with Array.Clear.

    The AdjustIndexes half matters more than the issue suggested, because it needs no version wrap. If a data-object key hashes to slot 0, then {{#with this}}[{{@index}}|{{@key}}|{{@first}}|{{@last}}]{{/with}} renders [WRONG|WRONG|WRONG|WRONG] on the first render. With the fix it renders [|||].

    Verification:

    • New tests are in DataVariableTests, plus three AdjustIndexes tests in FixedSizeDictionaryTests. The four failure-case tests failed before the fix and pass after it.
    • The full suite passes (2354/2354).
    • No test exercises the pool-return clear on its own: that path depends on a version wrap and can't be reproduced deterministically through the shared pool.

    pr.view reports the PR as MERGEABLE; merge state is BLOCKED until review and checks finish. I recommend a patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions