Skip to content

IndexOutOfRangeException at FixedSizeDictionary`3.AddOrReplace #498

Closed
dotnet/arcade
#16884

Description

@phil000

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)

Activity

  1. phil000 commented on Feb 3, 2022

    @phil000
    Author

    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.

  2. oformaniuk commented on Feb 3, 2022

    @oformaniuk
    Member

    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.

  3. phil000 commented on Feb 3, 2022

    @phil000
    Author

    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.

  4. oformaniuk commented on Feb 3, 2022

    @oformaniuk
    Member

    @phil000, additional information will definitely help 👍
    Looking forward for more info.

  5. phil000 commented on Feb 7, 2022

    @phil000
    Author

    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.

  6. phil000 commented on Feb 9, 2022

    @phil000
    Author

    This issue hasn't occurred again. Happy to close it as we cannot reproduce it.

  7. oformaniuk commented on Feb 10, 2022

    @oformaniuk
    Member

    @phil000, thanks for keeping me updated.

  8. jeffclary commented on Feb 17, 2022

    @jeffclary

    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.

  9. ChadNedzlek commented on Feb 18, 2022

    @ChadNedzlek

    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.

  10. ChadNedzlek commented on Feb 18, 2022

    @ChadNedzlek

    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)?

  11. oformaniuk commented on Feb 19, 2022

    @oformaniuk
    Member

    @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.

  12. ChadNedzlek commented on Feb 22, 2022

    @ChadNedzlek

    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.

  13. ChadNedzlek commented on Feb 22, 2022

    @ChadNedzlek

    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.

  14. ChadNedzlek commented on Feb 23, 2022

    @ChadNedzlek

    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.

  15. 20 remaining items

  16. oformaniuk commented on Mar 14, 2022

    @oformaniuk
    Member

    @ChadNedzlek , any news from your side?

  17. ChadNedzlek commented on Mar 14, 2022

    @ChadNedzlek

    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.

  18. oformaniuk commented on Mar 19, 2022

    @oformaniuk
    Member

    @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?

  19. pavisalavisa commented on Mar 20, 2022

    @pavisalavisa

    FYI I have observed another instance of this issue since deploying the beta package.

    There's probably more to this problem.

  20. ChadNedzlek commented on Mar 21, 2022

    @ChadNedzlek

    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:
    image

    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.

  21. added a commit that references this issue on Mar 26, 2022
  22. oformaniuk commented on Mar 26, 2022

    @oformaniuk
    Member

    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.

  23. ChadNedzlek commented on Mar 28, 2022

    @ChadNedzlek

    That's great news. I will be happy to try out the new code during our next deployment.

  24. ChadNedzlek commented on Apr 1, 2022

    @ChadNedzlek

    Is this included in the 2.1.1 version on Nuget? If so, I'll pull it and try it out.

  25. oformaniuk commented on Apr 2, 2022

    @oformaniuk
    Member

    Is this included in the 2.1.1 version on Nuget? If so, I'll pull it and try it out.

    Yes, it is part of 2.1.1

  26. oformaniuk commented on Apr 12, 2022

    @oformaniuk
    Member

    @ChadNedzlek , are there any updates from your side?

  27. ChadNedzlek commented on Apr 12, 2022

    @ChadNedzlek

    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.

  28. ChadNedzlek commented on Apr 27, 2022

    @ChadNedzlek

    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.

  29. oformaniuk commented on Apr 28, 2022

    @oformaniuk
    Member

    @ChadNedzlek , awesome, thanks for keeping us posted!

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions