RazorLight: Exception: Source array was not long enough. Check srcIndex and length, and the array's lower bounds.
I’ve got a really weird error going on here with our Azure App Service deployment related to RazorLight.
I’ve been running 2.0-alpha3 for several days now in Azure App Services without issue. Then all of a sudden this evening (without anything changing on our end) our app starts getting 500 errors from anything that invokes RazorLight (sending emails).
Source array was not long enough. Check srcIndex and length, and the array’s lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)\r\n
at System.Collections.Immutable.ImmutableExtensions.TryCopyTo[T](IEnumerable`1 sequence, T[] array, Int32 arrayIndex)\r\n
at System.Collections.Immutable.ImmutableExtensions.ToArray[T](IEnumerable`1 sequence, Int32 count)\r\n
at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)\r\n
at Microsoft.CodeAnalysis.ImmutableArrayExtensions.AsImmutableOrEmpty[T](IEnumerable`1 items)\r\n
at Microsoft.CodeAnalysis.Compilation.ValidateReferences[T](IEnumerable`1 references)\r\n
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create(String assemblyName, CSharpCompilationOptions options, IEnumerable`1 syntaxTrees, IEnumerable`1 references, CSharpCompilation previousSubmission, Type returnType, Type hostObjectType, Boolean isSubmission)\r\n
at RazorLight.Compilation.RoslynCompilationService.CreateCompilation(String compilationContent, String assemblyName)\r\n
at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(GeneratedRazorTemplate razorTemplate)\r\n
at RazorLight.TemplateFactoryProvider.CreateFactory(GeneratedRazorTemplate razorTemplate)\r\n
at RazorLight.TemplateFactoryProvider.<CreateFactoryAsync>d__4.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at RazorLight.RazorLightEngine.<CompileTemplateAsync>d__10.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at RazorLight.RazorLightEngine.<CompileRenderAsync>d__9.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at Bit.Core.Services.RazorMailService.<SendMasterPasswordHintEmailAsync>d__9.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at Bit.Core.Services.UserService.<SendMasterPasswordHintAsync>d__26.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at Bit.Api.Controllers.AccountsController.<PostPasswordHint>d__8.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)\r\n
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextExceptionFilterAsync>d__23.MoveNext()",
I restart the server and even re-deployed the code, but the error remains every time something uses RazorLight. I don’t really understand what has changed all of a sudden to cause this issue.
Locally I can execute the same RazorLight code without issue. See source here: https://github.com/bitwarden/core/blob/master/src/Core/Services/Implementations/RazorMailService.cs
Any ideas what is going on here?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 36 (15 by maintainers)
Commits related to this issue
- Fix #103 . Deprecate EngineFactory. Add engine builder — committed to toddams/RazorLight by toddams 7 years ago
- Remove RazorLight pkg, waiting for a new release: https://github.com/toddams/RazorLight/issues/103 — committed to massimodipaolo/ws-core by massimodipaolo 6 years ago
Hello everyone: Is another maintainer able to publish a release of @toddams 's changes please?
For anyone experiencing this issue, I was able to fix it after @toddams closed this issue on 8th June by cloning the latest commit of the master branch and compiling the RazorLight plugin myself in Visual Studio. This is because there hasn’t been any release since the issue was fixed containing the fix. I then referenced my compiled RazorLight in my projects and I haven’t had the issue since. I’ve attached a zip file of the RazorLight that I compiled which is working well for me, I will note that the version I’ve attached was compiled back in June and therefore is probably outdated now.
You just need to uninstall any version that you already have installed beforehand and reference the compiled version in your projects (Right click project -> Add -> Reference in Visual Studio 2017) RazorLight.zip
Issue is fixed, preparing for release
We are also seeing this issue, It does seem to be somehow related to Threading. Any idea if there is a fix for this any time soon. We really like this library.
You can reproduce it using this unit test project. razorlight_issue.zip
For me, the problem is caused by a simultaneous compilation of the same resources. But this is probably less obvious when it is in an ASP.Net env. Where the engine is probably registered as a singleton.
As a workaround, you can lock the methods where you compile templates.
@toddams I am fairly confident there are no calls to undefined template keys in my code. https://github.com/bitwarden/core/blob/master/src/Core/Services/Implementations/RazorMailService.cs
There should be 1 compilation at a time, otherwise this exception occurs
Well, if we look at stack trace - it goes deeper than RazorLight and falls into Microsoft.CodeAnalysis, which uses some array extensions, that makes me think that it’s not a RazorLight issue. I’m pretty concerned about this issue, as I use RazorLight personally in my projects and at work (that’s basically why it’s developed in a first place) but I did not face with such an issue so far. Let’s reopen this and wait if somebody else has the same problem
@toddams Even if you did, we are locked at
<PackageReference Include="RazorLight" Version="2.0.0-alpha3" />. Plus, we didn’t even change anything here so no new bits have been picked up. This same code has been running without issue on the server for 2-3 days without issue. Then all of a sudden these 500s that I could not get rid of for several hours last night.And guess what, this morning I wake up and the errors have magically stopped and everything works fine again with RazorLight.