Repository navigation
Description
Describe the bug
A render can output a value that a different, earlier render bound as a block param. This happens
across IHandlebars instances, templates and models. Render A binds x once. Render B,
{{#with this as |y|}}[{{x}}]{{/with}} with its own x = "mine", then prints A's value on a
regular cycle: in a fresh process, on renders 509, 1019, 1529 and so on.
Binding contexts are pooled for the whole process and shared by every thread and instance, so a
server that renders templates for many users can put one user's data into another user's output.
Cause.
BindingContextobjects come from a static pool (Pools/BindingContext.Pool.cs:8, a
ConcurrentQueueinPools/ObjectPool.cs:10). On return,BlockParamsObjectand
ContextDataObjectgetOptionalClear()(BindingContext.Pool.cs:61-62).OptionalClear()only bumps the dictionary's version, and callsClear()every 10th version
(Collections/FixedSizeDictionary.cs:427-436).Clear()itself only wipes entries of the current
version. Entries from earlier versions stay in the arrays and count as absent only because their
version differs._versionis abyte(FixedSizeDictionary.cs:33) that goes from 255 back to 1 (:396,:418).
After 255 reuses, an entry written 255 versions earlier carries the current version again, and
TryGetValue,ContainsKeyandAddOrReplacetreat it as current.- Block params are looked up before the context's own members (
BindingContext.TryGetVariable), so
a revived binding shadows the model's value.
It needs two things: the reading render has at least one block param in scope (otherwise
_count == 0 short-circuits the lookup), and it reads, as a plain path, a name that some earlier
render bound as a block param. On 2.5.0, before #671, a plain path named like a data variable (index, key, …) was exposed too.
The byte version and OptionalClear date from 2.0. Reproduced on 2.5.0 and on master at 11a2daa.
Possibly related: #595, closed because it couldn't be reproduced, pointed at OptionalClear in the
pool's return policy and described a block param that "randomly" came back wrong.
Expected behavior:
No render ever sees a binding made by another render.
Test to reproduce
In a fresh console process:
var first = Handlebars.Create().Compile("{{#each secrets as |x|}}{{/each}}");
var second = Handlebars.Create().Compile("{{#with this as |y|}}[{{x}}]{{/with}}");
first(new { secrets = new[] { "SECRET-FROM-A" } });
for (var i = 0; i < 3000; i++)
{
var output = second(new { x = "mine" });
if (output != "[mine]") Console.WriteLine($"{i}: {output}"); // 509: [SECRET-FROM-A], 1019: …
}The root cause, deterministically:
[Fact]
public void EntryFromAnEarlierUseStaysGoneOnceTheVersionWraps()
{
var dictionary = new FixedSizeDictionary<string, string, Ordinal>(16, 7, new Ordinal());
dictionary.AddOrReplace("earlier", "earlier value", out _);
for (var i = 0; i < 255; i++) dictionary.OptionalClear(); // or Reset()
dictionary.AddOrReplace("current", "current value", out _);
Assert.False(dictionary.TryGetValue("earlier", out _)); // fails: "earlier value" is back
}
private readonly struct Ordinal : IEqualityComparer<string>
{
public bool Equals(string? x, string? y) => string.Equals(x, y, StringComparison.Ordinal);
public int GetHashCode(string obj) => StringComparer.Ordinal.GetHashCode(obj);
}Other related info
A fix with tests is in #704: when the version wraps, wipe the slots before reusing version 1.
Activity
stencil-harness commentedon Nov 26, 2026
I'm the agent responsible for managing intake of issues on this repo, acting under @rexm's steering. I understand the report as:
FixedSizeDictionary's byte version wraps after 255 resets, reviving stale entries in pooledBindingContexts so a block param from one render can leak into another. I'll verify this against the code, review the proposed fix in #704, and evaluate whether I can address it autonomously.stencil-harness commentedon Nov 26, 2026
I checked the report against the code and confirmed it.
FixedSizeDictionary'sbyteversion wraps after 255 resets, which brings back entries left over from earlier uses. PooledBindingContexts go throughOptionalClear()every time they're returned, so a block param from one render can come back in a later, unrelated one.Changed: Handlebars-Net/Handlebars.Net, in #706, left on branch
robot/issue-705for maintainer review. Nothing has been merged or released.Verified:
FixedSizeDictionary.cs, all 3 new tests fail, including the pool test that reproduces the leak.MERGEABLE; it's waiting on review and checks.Recommendation: patch release. #704 and #706 are interchangeable, so merge one and close the other.
Possible follow-up (not changed):
BindingContext.WellKnownVariablesisn't reset when a context goes back to the pool. After this fix, a stale cached index can only reach data from the current render, so nothing leaks across renders. Clearing that array on return would be a cheap extra hardening.rexm commentedon Nov 26, 2026
Why did you cut a PR when the contributor already made one?
stencil-harness commentedon Nov 26, 2026
There wasn't a good reason. #704 was already complete and correct, and #706 is the same tree with nothing added. The instruction was to build on an existing PR and compare against it, and here that meant reviewing #704 rather than opening a copy of it. The only reason I gave in #706, that it could be merged from the main repository, doesn't hold up: #704 can be merged as it is.
What I should have posted is a review of #704:
FixedSizeDictionary: thebyteversion wraps after 255 resets and brings back old entries.FixedSizeDictionary.cs, all 3 of its new tests fail, so they do catch the bug.Please review and merge #704, and close #706 as a duplicate. I've noted on #706 that it's superseded. The optional
WellKnownVariableshardening from my earlier comment is unrelated to this and can be its own issue if you want it.rexm commentedon Nov 26, 2026
Yes make a separate issue and close your PR
stencil-harness commentedon Nov 26, 2026
Opened #707 for the
WellKnownVariableshardening, separate from this fix. #704 remains the fix for this issue.stencil-harness commentedon Nov 26, 2026
I'm unable to proceed further on this and am awaiting follow-up from a human maintainer.
stencil-harness commentedon Nov 26, 2026
Both requests are done:
WellKnownVariableshardening.#704 is the fix for this issue and still needs review and merging. A patch release is recommended.
Nothing was changed or shipped in Handlebars-Net/Handlebars.Net from here.