Repository navigation
fix: wipe FixedSizeDictionary's slots when its version wraps - #704
Open
nadrummond wants to merge 1 commit into
Open
nadrummond wants to merge 1 commit into
nadrummond 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. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
FixedSizeDictionaryclears cheaply:Reset()andOptionalClear()bump abyteversion and leaveold 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 throughOptionalClear()on every return (BindingContext.Pool.cs:61-62), so ablock param bound by one render comes back in a later, unrelated render, from another
IHandlebarsinstance, and shadows that render's own model. The issue has a repro.
Reset()andClear()now move to the next version throughNextVersion(), which wipes_entriesand
_indexeswhen the version wraps, before reusing version 1, so no entry can outlive a full cycleof 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.
EntryIndexvalues kept outside thedictionary (
BindingContext.WellKnownVariables, indexes intoContextDataObject) can still carry aversion 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 overResetand
OptionalClear: add a key, reset 255 times, add another key, and the first key must stay absent.Deterministic.
BindingContextPoolTests.BlockParamBoundInOneRenderIsNotSeenByLaterRenders, in a non-parallelcollection: 20 renders bind
x, then a second template renders up to 100,000 times and must neverprint it. It reads
xin two nested frames, so the pool's rotation can't hand the context holdingthe 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 thepool 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 reproprints the first render's value on renders 509, 1019, 1529, … before the fix, and nothing in 100,000
renders after it.