aspnetcore: RegexMatchTimeoutException when rendering ViewComponent

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Today a razor view on our .net core MVC site started inexplicably failing with a System.Text.RegularExpressions.RegexMatchTimeoutException exception. This is the second time this has happened - the first time it was fixed by restarting the site.

This problem started occurring with no changes, and I cannot reproduce it in my development environment. It doesn’t appear to be related to the HTML that the ViewComponent renders.

I think perhaps the only slightly strange thing I’m doing is passing through a complex model to be bound on the ViewComponent’s InvokeAsync method:

public class YouTubeVideosViewComponent : ViewComponent
{
	private readonly IYouTubeVideosService _youTubeVideosService;

	public YouTubeVideosViewComponent(IYouTubeVideosService youTubeVideosService)
	{
		_youTubeVideosService = youTubeVideosService;
	}

	public async Task<IViewComponentResult> InvokeAsync(YouTubeVideosViewModel viewModel)
	{
		await _youTubeVideosService.PopulateAsync(viewModel);
		return View(viewModel);
	}
}

YouTubeVideosViewModel contains a couple of properties that are used to modify the result:

@await Component.InvokeAsync("YouTubeVideos", new YouTubeVideosViewModel
    {
        Type = YouTubeVideosType.Home,
        MaxToShow = 2
    })

Expected Behavior

The ViewComponent renders it’s content without throwing an exception.

Steps To Reproduce

No response

Exceptions (if any)

System.Text.RegularExpressions.RegexMatchTimeoutException: The Regex engine has timed out while trying to match a pattern to an input string. This can occur for many reasons, including very large inputs or excessive backtracking caused by nested quantifiers, back-references and other factors.
   at System.Text.RegularExpressions.RegexInterpreter.Scan(ReadOnlySpan`1 text)
   at System.Text.RegularExpressions.Regex.RunAllMatchesWithCallback[TState](String inputString, ReadOnlySpan`1 inputSpan, Int32 startat, TState&amp; state, MatchCallback`1 callback, RegexRunnerMode mode, Boolean reuseMatchObject)
   at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat)
   at System.Text.RegularExpressions.Regex.Replace(String input, String replacement, Int32 count, Int32 startat)
   at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
   at Microsoft.AspNetCore.Razor.Language.HtmlConventions.ToHtmlCase(String name)
   at Microsoft.AspNetCore.Mvc.Razor.Extensions.ViewComponentTagHelperDescriptorFactory.&lt;&gt;c__DisplayClass10_0.&lt;AddRequiredAttributes&gt;b__0(RequiredAttributeDescriptorBuilder attributeBuilder)
   at Microsoft.AspNetCore.Razor.Language.DefaultTagMatchingRuleDescriptorBuilder.Attribute(Action`1 configure)
   at Microsoft.AspNetCore.Mvc.Razor.Extensions.ViewComponentTagHelperDescriptorFactory.AddRequiredAttributes(ImmutableArray`1 methodParameters, TagMatchingRuleDescriptorBuilder builder)
   at Microsoft.AspNetCore.Mvc.Razor.Extensions.ViewComponentTagHelperDescriptorFactory.&lt;&gt;c__DisplayClass7_0.&lt;CreateDescriptor&gt;b__0(TagMatchingRuleDescriptorBuilder ruleBuilder)
   at Microsoft.AspNetCore.Razor.Language.DefaultTagHelperDescriptorBuilder.TagMatchingRule(Action`1 configure)
   at Microsoft.AspNetCore.Mvc.Razor.Extensions.ViewComponentTagHelperDescriptorFactory.CreateDescriptor(INamedTypeSymbol type)
   at Microsoft.AspNetCore.Mvc.Razor.Extensions.ViewComponentTagHelperDescriptorProvider.Execute(TagHelperDescriptorProviderContext context)
   at Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
   at Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.ExecuteCore(RazorCodeDocument codeDocument)
   at Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument)
   at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document)
   at Microsoft.AspNetCore.Razor.Language.DefaultRazorProjectEngine.ProcessCore(RazorCodeDocument codeDocument)
   at Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Process(RazorProjectItem projectItem)
   at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(String relativePath)
   at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(String normalizedPath)
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
   at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)
   at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
   at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)
   at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
   at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
   at Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult.ExecuteAsync(ViewComponentContext context)
   at Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentInvoker.InvokeAsync(ViewComponentContext context)
   at Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentHelper.InvokeCoreAsync(ViewComponentDescriptor descriptor, Object arguments)
   at AspNetCore.Views_Home_Index.ExecuteAsync() in C:\home\site\wwwroot\Views\Home\Index.cshtml:line 381
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
   at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeResultAsync&gt;g__Logged|22_0(ResourceInvoker invoker, IActionResult result)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResultFilterAsync&gt;g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeResultFilters&gt;g__Awaited|28_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResourceFilter&gt;g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeFilterPipelineAsync&gt;g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeAsync&gt;g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeAsync&gt;g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.&lt;Invoke&gt;g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at StackExchange.Profiling.MiniProfilerMiddleware.Invoke(HttpContext context) in C:\projects\dotnet\src\MiniProfiler.AspNetCore\MiniProfilerMiddleware.cs:line 114
   at SimplyDatingNetwork.Presentation.Web.Models.Middleware.CurrentCultureCurrencySymbolOverrideMiddleware.InvokeAsync(HttpContext httpContext) in C:\DevOps\agent\_work\1\s\Presentation.Web\Models\Middleware\CurrentCultureCurrencySymbolOverrideMiddleware.cs:line 39
   at EasyI18n.Core.Middleware.I18NMiddleware.InvokeAsync(HttpContext context, IWebHostEnvironment webHostEnvironment)
   at EasyI18n.Core.Middleware.I18NMiddleware.InvokeAsync(HttpContext context, IWebHostEnvironment webHostEnvironment)
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
   at SimplyDatingNetwork.Presentation.Web.Models.Middleware.EnableRequestBufferingMiddleware.InvokeAsync(HttpContext httpContext) in C:\DevOps\agent\_work\1\s\Presentation.Web\Models\Middleware\EnableRequestBufferingMiddleware.cs:line 19
   at SimplyDatingNetwork.Presentation.Web.Models.Middleware.AuthenticatedSessionValidatorMiddleware.InvokeAsync(HttpContext httpContext, IUnitOfWork unitOfWork, ISiteUrlGenerator siteUrlGenerator) in C:\DevOps\agent\_work\1\s\Presentation.Web\Models\Middleware\AuthenticatedSessionValidatorMiddleware.cs:line 72
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.Invoke(HttpContext httpContext)
   at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context)
   at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context)
   at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context)
   at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context)
   at NWebsec.AspNetCore.Middleware.Middleware.CspMiddleware.Invoke(HttpContext context)
   at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.&lt;Invoke&gt;g__Awaited|8_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task)

.NET Version

7.0.403

Anything else?

  • No changes to the code or infrastructure. Error just started occurring randomly in production.
  • Cannot reproduce in development environment, despite the code generated by the ViewComponent being identical
  • An almost identical bit of code for a different ViewComponent works fine, just above this

Important note: This time I was able to fix this in Production by “editing” (ie. clicking the save button with no changes) the view component’s Default.cshtml file in Kudu. After saving, the exception stopped being thrown. My assumption is that this cleared an internal cache of some description.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 2
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Have just seen this on an Umbraco Cloud site running v12. Issue resolved following a site restart.

Image

Going to have a support ticket raised with Umbraco, so will feed back following their response.