aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

ActionDescriptor - Operations that change non-concurrent collections must have exclusive access

Open johnavery opened this issue 7 years ago • 4 comments

I've run into this issue when invoking a custom middleware component used for extracting the user identity from a REST API call. This is happening in our ASP.NET Core 2.1.6 API. This is a similar issue to IdentityServer/IdentityServer4 #2453. The culprit seems to be Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor.Properties. It is being initialized as a Dictionary<object, object>, but it seems you may want to consider a ConcurrentDictionary instead. I've included the exception details. Please let me know what additional information would be helpful.

Exception: System.InvalidOperationException

ExceptionMessage: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.

Stack Trace: at System.Collections.Generic.Dictionary2.FindEntry(TKey key) at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& value) at Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptorExtensions.GetProperty[T](ActionDescriptor actionDescriptor) at Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptorExtensions.IsImplicitlyMappedTo(ActionDescriptor action, ApiVersion version) at Microsoft.AspNetCore.Mvc.Versioning.ApiVersionActionSelector.ActionIsSatisfiedBy(ActionDescriptor action, ApiVersionModel model, ApiVersion version, ICollection1 implicitMatches) at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Collections.Generic.List1.InsertRange(Int32 index, IEnumerable1 collection) at Microsoft.AspNetCore.Mvc.Versioning.ApiVersionActionSelector.SelectBestActions(ActionSelectionContext context) at Microsoft.AspNetCore.Mvc.Versioning.ApiVersionActionSelector.SelectBestCandidate(RouteContext context, IReadOnlyList1 candidates) at Microsoft.AspNetCore.Mvc.Internal.MvcAttributeRouteHandler.RouteAsync(RouteContext context) at Microsoft.AspNetCore.Routing.Tree.TreeRouter.RouteAsync(RouteContext context) at Microsoft.AspNetCore.Routing.RouteCollection.RouteAsync(RouteContext context) at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at FMGlobal.Lucid.Api.IndexManagement.Middleware.ConfigureUserIdentityInfoMiddleware.Invoke(HttpContext context, IEnvironmentLogic environmentLogic, UserIdentityInfo userIdentityInfo, ILogger1 logger) in C:\agent13_work\39\s\src\Api.IndexManagement\Middleware\ConfigureUserIdentityInfoMiddleware.cs:line 48 at Lucid.Api.IndexManagement.Middleware.ApiKeyValidatorMiddleware.Invoke(HttpContext context) in C:\agent13_work\39\s\src\Api.IndexManagement\Middleware\ApiKeyValidatorMiddleware.cs:line 60 at Lucid.Api.IndexManagement.Startup.<>c.<b__15_0>d.MoveNext() in C:\agent13_work\39\s\src\Api.IndexManagement\Startup.cs:line 516 --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)

johnavery avatar Dec 14 '18 17:12 johnavery

One more thing: I worked around this issue by recycling the app pool. But I imagine this could happen again at any time.

johnavery avatar Dec 14 '18 17:12 johnavery

This is a bug in the API Versioning library. Action Descriptors provide a way to be modified during startup and should be treated as immutable once that completes. We've worked with the author in the past to provide another solution, so this might already be fixed.

/cc @commonsensesoftware

rynowak avatar Dec 14 '18 19:12 rynowak

Correct. This was reported and fixed as one of a few possible symptoms related to microsoft/aspnet-api-versioning#306. The fix was published in 2.3.0 back in June. Updating the package version should fix the problem for good. Thanks.

commonsensesoftware avatar Dec 14 '18 20:12 commonsensesoftware

Great, I'm going to close this since we can't change how the API works, and the real cause is fixed in api-versioning. Thanks @commonsensesoftware !

rynowak avatar Dec 14 '18 20:12 rynowak