You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IndexOutOfRangeException at FixedSizeDictionary`3.AddOrReplace #498
Getting this exception periodically in Handlebars, Version=2.1.0.0 trying to render content from a compiled delagate (HandlebarsTemplate<object, object>)
Not sure what else I can provide because it doesn't seem related to the content being rendered. Debugging though it always works.
We were previously using 2.0.9 on .NET 5. We upgraded our project to .NET 6.0 and Handlebars 2.1.0 and the problem apparently started.
System.IndexOutOfRangeException:
at HandlebarsDotNet.Collections.FixedSizeDictionary`3.AddOrReplace (Handlebars, Version=2.1.0.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661)
at HandlebarsDotNet.BindingContext.Initialize (Handlebars, Version=2.1.0.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661)
at HandlebarsDotNet.BindingContext+BindingContextPool.CreateContext (Handlebars, Version=2.1.0.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661)
at HandlebarsDotNet.BindingContext.Create (Handlebars, Version=2.1.0.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661)
at HandlebarsDotNet.HandlebarsEnvironment+<>c__DisplayClass15_0.<Compile>b__0 (Handlebars, Version=2.1.0.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661)
at HandlebarsDotNet.HandlebarsEnvironment+<>c__DisplayClass16_0.<Compile>b__0 (Handlebars, Version=2.1.0.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661)
at OnSend.MessagingService.Core.Templating.HandlebarsMessageBuilder.BuildMessage (OnSend.MessagingService.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\a\1\s\OnSend.MessagingService.Core\Templating\HandlebarsMessageBuilder.cs:68)
Hello @phil000
Thanks for the issue report.
There were no changes related to the code path in 2.1.0.
Although, it's unclear how .NET upgrade could have caused it.
Hi.
For your information, we have been monitoring our test environment on .NET 6.0 + 2.0.9 for several days and have not seen the 'IndexOutOfRangeException' occur again.
We have deployed this configuration into production as of now.
Next step is to see if we can get the 'IndexOutOfRangeException' to occur again in the test environment on 2.1.0 and provide a debug stack trace.
I am seeing a very similar issue in version 2.0.9 and netstandard2.1. My stack trace is a bit different because I am using Handlebars.Net.Extension.Json version 1.0.0.
The behavior is the same, in that I see only periodic errors that I can't explain. Still investigating on my end.
We are seeing this in our service as well, several dozen times a day. Though our callstack isn't quite the same:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at HandlebarsDotNet.Collections.FixedSizeDictionary`3.AddOrReplace(TKey& key, TValue& value, EntryIndex`1& index)
at HandlebarsDotNet.Iterators.ListIterator`2.Iterate(EncodedTextWriter& writer, BindingContext context, ChainSegment[] blockParamsVariables, Object input, TemplateDelegate template, TemplateDelegate ifEmpty)
at HandlebarsDotNet.Iterators.ListIterator`2.HandlebarsDotNet.Iterators.IIterator.Iterate(EncodedTextWriter& writer, BindingContext context, ChainSegment[] blockParamsVariables, Object input, TemplateDelegate template, TemplateDelegate ifEmpty)
at HandlebarsDotNet.Compiler.Iterator.Iterate(BindingContext context, EncodedTextWriter writer, ChainSegment[] blockParamsVariables, Object target, TemplateDelegate template, TemplateDelegate ifEmpty)
at lambda_method(Closure , EncodedTextWriter& , BindingContext )
at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass15_0.<Compile>b__0(TextWriter writer, Object context, Object data)
at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass16_0.<Compile>b__0(Object context, Object data)
It definitely seems like something is weird about the FixedSizeDictionary. It's also not consistent... we run it ~3000 times a day, and it only fails 30 ish times... But that still means 1% of our users get essentially random failures, which is a bit too high for comfort... We are about to attempt to just wrap all the handlebars code in a giant "try{Render}catch{Render... again}" loop, but that's really unfortunate for our service to have to do.
My guess is there is some sort of multithreading issue... and some handlebars data structure we are sharing isn't actually thread safe (that's the only way I'd be able to explain the infrequency, and a failure to look up a thing in an immutable input).
Is the "HandlebarsTemplate" delegate returned from IHandlebars.Compile not reusable? Or perhaps not threadsafe (so we could have a pool of them shared, but perhaps they have to be rented)?
@ChadNedzlek , @jeffclary
It is thread safe (by design). FixedSizeDictionary is part of BindingContext which is pooled and should not be shared between different threads at any given moment in time.
However, I suspect that there's some race-condition involved.
I've already tried to tackle the problem but I still cannot reproduce it in a way I can get some useful data about what is happening.
If you see the error constantly and if you'd be able to provide some dump or try to create a consistent repro - it will help.
I'll try my best, unfortunately, its server code, so it just causes 500's in our server code, and taking a dump isn't really feasible (especially not one I could share, since it would contain customer data). If it's not shared between threads, it would be weird for it to be a race condition (since what two things would be racing)?
It's definitely causing us serious problems in our server... I'm not sure what we are going to do here.
I'm going to try making a small repo that basically just does millions of simultaneous renders, and see if I can trigger it... I'm not sure what callstack that's coming from though, so it's going to take me a while to produce anything. Like I'm not even sure what it's iterating.
Hrm... I think it might be a double return of something from a pool. In general, disposing something is supposed to be safe to do multiple times... but a lot of the Disposes in Handlebars appear to return things to the pool unchecked, including "contexts"... so if we disposed something we shouldn't have twice, then two copies of the same object are in the pool, and will just randomly crash later. (documentation here about 6 paragraphs down)
It would probably be good if the returning disposes were multi-call safe. I'm going to check our code base for any potential double disposes and see if I can reproduce the issue.
The incidence seems to have gone down, but not gone, which is... confusing. We only have 1 hit since deploying the beta version 5 day ago to this particular set of machines, whereas we previously had around a dozen a day. But I'm confused that there would be any at all. It somehow seems to have changed the behavior, but not eradicated it, which from the change I saw... doesn't make any sense... it seems like it should either have done nothing or solved the problem.
@ChadNedzlek , thanks for the update.
Could it be due do some rolling update and the error actually popped from the old version?
Anyway, it's been additional 4 days, how is the picture with the errors now?
Yeah, unfortunately it seems to still be prevalent, We deployed the fix March 10, and for a couple days it looked fine, but then a whole bunch of the problem came up, looks like the problem is somehow somewhere else:
I still haven't managed to get it to reproduce in a non-production environment to get a dump of it. I can't figure out what the difference is other than scale.
Thanks to a failure in Github Actions I was able get some more details about the problem:
Error Message:
System.IndexOutOfRangeException : Index was outside the bounds of the array.
Stack Trace:
at HandlebarsDotNet.Collections.FixedSizeDictionary`3.AddOrReplace(TKey& key, TValue& value, EntryIndex`1& index) in D:\a\Handlebars.Net\Handlebars.Net\source\Handlebars\Collections\FixedSizeDictionary.cs:line 307
at HandlebarsDotNet.ValueProviders.BlockParamsValues.CreateProperty(Int32& variableIndex, EntryIndex`1& index) in D:\a\Handlebars.Net\Handlebars.Net\source\Handlebars\ValueProviders\BlockParamsValues.cs:line 30
at HandlebarsDotNet.Iterators.DictionaryIterator`3.Iterate(EncodedTextWriter& writer, BindingContext context, ChainSegment[] blockParamsVariables, Object input, TemplateDelegate template, TemplateDelegate ifEmpty) in D:\a\Handlebars.Net\Handlebars.Net\source\Handlebars\Iterators\DictionaryIterator'2.cs:line 26
at HandlebarsDotNet.Iterators.DictionaryIterator`3.HandlebarsDotNet.Iterators.IIterator.Iterate(EncodedTextWriter& writer, BindingContext context, ChainSegment[] blockParamsVariables, Object input, TemplateDelegate template, TemplateDelegate ifEmpty)
at HandlebarsDotNet.Compiler.Iterator.Iterate(BindingContext context, EncodedTextWriter writer, ChainSegment[] blockParamsVariables, Object target, TemplateDelegate template, TemplateDelegate ifEmpty) in D:\a\Handlebars.Net\Handlebars.Net\source\Handlebars\Compiler\Translation\Expression\IteratorBinder.cs:line 135
at lambda_method(Closure , EncodedTextWriter& , BindingContext )
at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass15_0.<Compile>b__0(TextWriter writer, Object context, Object data) in D:\a\Handlebars.Net\Handlebars.Net\source\Handlebars\HandlebarsEnvironment.cs:line 171
at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass16_0.<Compile>b__0(Object context, Object data) in D:\a\Handlebars.Net\Handlebars.Net\source\Handlebars\HandlebarsEnvironment.cs:line 185
at HandlebarsDotNet.Test.BasicIntegrationTests.DictionaryEnumeratorWithBlockParams(IHandlebars handlebars) in D:\a\Handlebars.Net\Handlebars.Net\source\Handlebars.Test\BasicIntegrationTests.cs:line 814
According to stacktrace the problem happened due to a bug in collisions handling code. It could occur regardless of pooling or load and mostly depend on hashcode value used and the state of internal storage.
@ChadNedzlek , I'm going to release a new version of the package today and I'd really appreciate if you can deploy it to your environment to verify that problem went away.
Activity
phil000 commentedon Feb 3, 2022
FYI,
Just checking our logs, we've never seen this issue before upgrading to .NET 6.0 and 2.1.0 a few days ago.
I've temporarily downgraded back to 2.0.9 to see if the issue is relieved by that. We were previously on 2.0.9.
oformaniuk commentedon Feb 3, 2022
Hello @phil000
Thanks for the issue report.
There were no changes related to the code path in 2.1.0.
Although, it's unclear how .NET upgrade could have caused it.
phil000 commentedon Feb 3, 2022
Thank you for that.
I've reverted to .NET 6.0 + 2.0.9 and deployed to our test environment already.
I'll be able to report in a few days if we see the issue keep occurring, or if it stops.
Either way that will provide a path forward.
There's a lot of calls to 'AddOrReplace' in Initialize so a debug build and maybe a process dump at the time of the exception may help.
oformaniuk commentedon Feb 3, 2022
@phil000, additional information will definitely help 👍
Looking forward for more info.
phil000 commentedon Feb 7, 2022
Hi.
For your information, we have been monitoring our test environment on .NET 6.0 + 2.0.9 for several days and have not seen the 'IndexOutOfRangeException' occur again.
We have deployed this configuration into production as of now.
Next step is to see if we can get the 'IndexOutOfRangeException' to occur again in the test environment on 2.1.0 and provide a debug stack trace.
phil000 commentedon Feb 9, 2022
This issue hasn't occurred again. Happy to close it as we cannot reproduce it.
oformaniuk commentedon Feb 10, 2022
@phil000, thanks for keeping me updated.
jeffclary commentedon Feb 17, 2022
I am seeing a very similar issue in version 2.0.9 and netstandard2.1. My stack trace is a bit different because I am using Handlebars.Net.Extension.Json version 1.0.0.
The behavior is the same, in that I see only periodic errors that I can't explain. Still investigating on my end.
ChadNedzlek commentedon Feb 18, 2022
We are seeing this in our service as well, several dozen times a day. Though our callstack isn't quite the same:
It definitely seems like something is weird about the FixedSizeDictionary. It's also not consistent... we run it ~3000 times a day, and it only fails 30 ish times... But that still means 1% of our users get essentially random failures, which is a bit too high for comfort... We are about to attempt to just wrap all the handlebars code in a giant "try{Render}catch{Render... again}" loop, but that's really unfortunate for our service to have to do.
ChadNedzlek commentedon Feb 18, 2022
My guess is there is some sort of multithreading issue... and some handlebars data structure we are sharing isn't actually thread safe (that's the only way I'd be able to explain the infrequency, and a failure to look up a thing in an immutable input).
Is the "HandlebarsTemplate" delegate returned from IHandlebars.Compile not reusable? Or perhaps not threadsafe (so we could have a pool of them shared, but perhaps they have to be rented)?
oformaniuk commentedon Feb 19, 2022
@ChadNedzlek , @jeffclary
It is thread safe (by design). FixedSizeDictionary is part of BindingContext which is pooled and should not be shared between different threads at any given moment in time.
However, I suspect that there's some race-condition involved.
I've already tried to tackle the problem but I still cannot reproduce it in a way I can get some useful data about what is happening.
If you see the error constantly and if you'd be able to provide some dump or try to create a consistent repro - it will help.
ChadNedzlek commentedon Feb 22, 2022
I'll try my best, unfortunately, its server code, so it just causes 500's in our server code, and taking a dump isn't really feasible (especially not one I could share, since it would contain customer data). If it's not shared between threads, it would be weird for it to be a race condition (since what two things would be racing)?
It's definitely causing us serious problems in our server... I'm not sure what we are going to do here.
ChadNedzlek commentedon Feb 22, 2022
I'm going to try making a small repo that basically just does millions of simultaneous renders, and see if I can trigger it... I'm not sure what callstack that's coming from though, so it's going to take me a while to produce anything. Like I'm not even sure what it's iterating.
ChadNedzlek commentedon Feb 23, 2022
Hrm... I think it might be a double return of something from a pool. In general, disposing something is supposed to be safe to do multiple times... but a lot of the Disposes in Handlebars appear to return things to the pool unchecked, including "contexts"... so if we disposed something we shouldn't have twice, then two copies of the same object are in the pool, and will just randomly crash later. (documentation here about 6 paragraphs down)
It would probably be good if the returning disposes were multi-call safe. I'm going to check our code base for any potential double disposes and see if I can reproduce the issue.
20 remaining items
oformaniuk commentedon Mar 14, 2022
@ChadNedzlek , any news from your side?
ChadNedzlek commentedon Mar 14, 2022
The incidence seems to have gone down, but not gone, which is... confusing. We only have 1 hit since deploying the beta version 5 day ago to this particular set of machines, whereas we previously had around a dozen a day. But I'm confused that there would be any at all. It somehow seems to have changed the behavior, but not eradicated it, which from the change I saw... doesn't make any sense... it seems like it should either have done nothing or solved the problem.
oformaniuk commentedon Mar 19, 2022
@ChadNedzlek , thanks for the update.
Could it be due do some rolling update and the error actually popped from the old version?
Anyway, it's been additional 4 days, how is the picture with the errors now?
pavisalavisa commentedon Mar 20, 2022
FYI I have observed another instance of this issue since deploying the beta package.
There's probably more to this problem.
ChadNedzlek commentedon Mar 21, 2022
Yeah, unfortunately it seems to still be prevalent, We deployed the fix March 10, and for a couple days it looked fine, but then a whole bunch of the problem came up, looks like the problem is somehow somewhere else:

I still haven't managed to get it to reproduce in a non-production environment to get a dump of it. I can't figure out what the difference is other than scale.
Merge pull request #512 from /issues/498
oformaniuk commentedon Mar 26, 2022
Thanks to a failure in Github Actions I was able get some more details about the problem:
According to stacktrace the problem happened due to a bug in collisions handling code. It could occur regardless of pooling or load and mostly depend on
hashcodevalue used and the state of internal storage.@ChadNedzlek , I'm going to release a new version of the package today and I'd really appreciate if you can deploy it to your environment to verify that problem went away.
ChadNedzlek commentedon Mar 28, 2022
That's great news. I will be happy to try out the new code during our next deployment.
ChadNedzlek commentedon Apr 1, 2022
Is this included in the 2.1.1 version on Nuget? If so, I'll pull it and try it out.
oformaniuk commentedon Apr 2, 2022
Yes, it is part of 2.1.1
oformaniuk commentedon Apr 12, 2022
@ChadNedzlek , are there any updates from your side?
ChadNedzlek commentedon Apr 12, 2022
It's been in the staging environment for a week without issue. It's rolling out a bit broader in the next couple days. I'll let you know what I see.
ChadNedzlek commentedon Apr 27, 2022
After running in production for a week, I don't think I've seen any more evidence of this. Looks like it's all good.
oformaniuk commentedon Apr 28, 2022
@ChadNedzlek , awesome, thanks for keeping us posted!