OpenAPI v3 and Swagger concurrent update exception

Hi,

We are using ServiceStack 8.8.0 and OpenAPI v3 and the Swagger UI configured in IHostingStartup as recommended at OpenAPI v3 and Swagger UI - ServiceStack

Occasionally we get the exception below at startup.

System.InvalidOperationException: 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.
   at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at ServiceStack.AspNetCore.OpenApi.OpenApiMetadata.CreateSchema(Type schemaType, String route, String verb, HashSet`1 allTypes) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.AspNetCore.OpenApi/OpenApiMetadata.cs:line 767
   at ServiceStack.AspNetCore.OpenApi.ServiceStackDocumentFilter.Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.AspNetCore.OpenApi/ServiceStackDocumentFilter.cs:line 32
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerAsync(String documentName, String host, String basePath)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

Any ideas?

I’ve changed the Schemas to use a ConcurrentDictionary in this commit.

This change is available in the latest v8.8.1 that’s now available in the pre release packages.

Hello, I’m occasionally seeing a similar problem with authentication:

HandleException EventId=5088535C Exception=System.InvalidOperationException: 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.
at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at ServiceStack.RequestExtensions.CompletedAuthentication(IRequest req) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/RequestExtensions.cs:line 374 at ServiceStack.Auth.AuthProvider.OnAuthenticatedAsync(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary2 authInfo, CancellationToken token) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/Auth/AuthProvider.cs:line 284

or

HandleException EventId=6FEEFC93 Exception=System.InvalidOperationException: 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.
at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
at ServiceStack.ServiceExtensions.GetSessionInternalAsync(IRequest httpReq, Boolean reload, Boolean async, CancellationToken token) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/ServiceExtensions.cs:line 258
at ServiceStack.Auth.AuthenticateService.AuthenticateAsync(Authenticate request, CancellationToken token) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack/Auth/AuthenticateService.cs:line 481

which seem to use IRequest’s

/// <summary>
/// Attach any data to this request that all filters and services can access.
/// </summary>
Dictionary<string, object> Items { get; }

We’re using ServiceStack 6.0.0.0, but as far as I can tell from GitHub, current source is still the same (no ConcurrentDictionary).

Any comments would be much appreciated.

That’s a the IRequest Dictionary, it shouldn’t be accessed by more than 1 thread concurrently and ServiceStack doesn’t spawn any threads in a Request, I’m assuming you must be trying to access the same IRequest context instance in a background thread?