Skip to content

fix: wipe FixedSizeDictionary's slots when its version wraps - #704

Open
nadrummond wants to merge 1 commit into
Handlebars-Net:masterfrom
nadrummond:fix/fixed-size-dictionary-version-wrap
Open

nadrummond wants to merge 1 commit into
Handlebars-Net:masterfrom
nadrummond:fix/fixed-size-dictionary-version-wrap

Conversation

@nadrummond

@nadrummond nadrummond commented Nov 26, 2026 •

Copy link
Copy Markdown

Fixes #705.

FixedSizeDictionary clears cheaply: Reset() and OptionalClear() bump a byte version and leave
old entries in place, counting them as absent while their version differs. When the version wraps from
255 back to 1, an entry written 255 versions earlier matches the current version again. Pooled
BindingContexts go through OptionalClear() on every return (BindingContext.Pool.cs:61-62), so a
block param bound by one render comes back in a later, unrelated render, from another IHandlebars
instance, and shadows that render's own model. The issue has a repro.

Reset() and Clear() now move to the next version through NextVersion(), which wipes _entries
and _indexes when the version wraps, before reusing version 1, so no entry can outlive a full cycle
of versions. The cost is one clear of both arrays per 255 resets (112 slots for a binding context's
dictionaries); the per-return path is otherwise unchanged. EntryIndex values kept outside the
dictionary (BindingContext.WellKnownVariables, indexes into ContextDataObject) can still carry a
version from an earlier cycle; after the wipe such an index points at an empty slot or at an entry
written since the wipe, never at one left from before the wrap.

Tests

  • FixedSizeDictionaryTests.EntryFromAnEarlierUseStaysGoneOnceTheVersionWraps, a theory over Reset
    and OptionalClear: add a key, reset 255 times, add another key, and the first key must stay absent.
    Deterministic.
  • BindingContextPoolTests.BlockParamBoundInOneRenderIsNotSeenByLaterRenders, in a non-parallel
    collection: 20 renders bind x, then a second template renders up to 100,000 times and must never
    print it. It reads x in two nested frames, so the pool's rotation can't hand the context holding
    the old binding only to the root frame, where nothing is read. It depends on the state other tests
    leave the shared pool in, so the dictionary theory is the deterministic check.

On master (11a2daa), with only the tests added, the dictionary theory fails on every run, and the
pool test fails both alone (5 of 5) and in the full suite (15 of 15). With the fix, the full suite
passes on net10.0: 2,352 tests (2,349 on master, plus the three new ones). The issue's console repro
prints the first render's value on renders 509, 1019, 1529, … before the fix, and nothing in 100,000
renders after it.

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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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)

1 participant