Closed
Description
I have recently upgraded to .net 6 (6.0.13). Application is run under docker containers in kubernetes. For some reason some of the pods randomly fails with System.InvalidOperationException: Collection was modified; enumeration operation may not execute
I'm using multitenancy package https://github.com/saaskit/saaskit which might but not necessarily have to be related to the issue.
In startup this is how it's configured (Configure method):
app.UseMultitenancy<Tenant>();
app
.UseSwagger(c => { c.RouteTemplate = "docs/{documentName}/swagger.json"; })
.UseSwaggerUI(c =>
{
// swagger setup deleted
});
app.UsePerTenant<Tenant>((tenant, builder) =>
{
if (!tenant.Tenant.IsEmpty())
{
var pathPrefix = string.IsNullOrEmpty(basePath) || !basePath.EndsWith('/') ? "/" : string.Empty;
builder.UsePathBase(pathPrefix + tenant.Tenant.Id);
}
// some other stuff
builder.UseIdentityServer();
builder.UseRouting();
builder.UseAuthorization();
builder.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapControllerRoute("signin", "signin", new {controller = "Signin", action = nameof(SigninController.Index)});
// some other routes
});
}
Call stack:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToArray()
at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.HandleChange()
at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.OnDataSourcesChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(IApplicationBuilder builder, Action`1 configure)
at App.IdentityServer.Startup.<>c__DisplayClass8_0.<Configure>b__6(TenantPipelineBuilderContext`1 tenant, IApplicationBuilder builder) in src/App.IdentityServer/Startup.cs:line 601
at SaasKit.Multitenancy.Internal.TenantPipelineMiddleware`1.BuildTenantPipeline(TenantContext`1 tenantContext)
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
--- End of stack trace from previous location ---
at System.Lazy`1.CreateValue()
at SaasKit.Multitenancy.Internal.TenantPipelineMiddleware`1.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at SaasKit.Multitenancy.Internal.TenantResolutionMiddleware`1.Invoke(HttpContext context, ITenantResolver`1 tenantResolver)
at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
It is failing there in UseEndpoints:
var routeOptions = builder.ApplicationServices.GetRequiredService<IOptions<RouteOptions>>();
foreach (var dataSource in endpointRouteBuilder.DataSources)
{
if (!routeOptions.Value.EndpointDataSources.Contains(dataSource))
{
routeOptions.Value.EndpointDataSources.Add(dataSource);
}
}
By looking at the code, UseEndpoints is executed after first request is called, not on startup which might cause this issue