OrchardCMS / OrchardCore

Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
https://orchardcore.net
BSD 3-Clause "New" or "Revised" License
7.42k stars 2.39k forks source link

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. #11033

Closed rjpowers10 closed 5 months ago

rjpowers10 commented 2 years ago

Describe the bug

I'm using ISession.Query to query for a content item. I'm also calling the extension method FirstOrDefaultAsync that takes an IContentManager to ensure that the IContentManager.LoadAsync method is called.

https://github.com/OrchardCMS/OrchardCore/blob/5da5858fe04ac864430fddfdc91fa9c4806a51cc/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentQueryOfTIndexExtensions.cs#L18

I encountered this error and was wondering if someone on the OC team might be able to make sense of it. It's not consistently repeatable for me; in fact it's only happened one time. Sorry I don't have anything more specific to say. Is there anything else I can provide to help?

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.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at OrchardCore.ContentManagement.DefaultContentManagerSession.Store(ContentItem item)
   at OrchardCore.ContentManagement.DefaultContentManager.LoadAsync(ContentItem contentItem)
   at OrchardCore.ContentManagement.ContentQueryOfTIndexExtensions.FirstOrDefaultAsync[TIndex](IQuery`2 query, IContentManager contentManager)

My calling code basically looks like this.

return await _session.Query<ContentItem, SomeIndex>(i => i.SomeValue == someValue)
    .FirstOrDefaultAsync(_contentManager);
Skrypt commented 2 years ago

Are you doing any update on the data in a ContentHandler that could make this happen? Basically, it says that the record you are asking for has previously changed in the same session you are using. You then need to use a different scoped session to retrieve this record knowing that it will retrieve the current record stored in the database and not necessarily the one that you just updated. Else, you need to find a way to pass the updated data to your method if that's what you want to use/display.

rjpowers10 commented 2 years ago

Are you doing any update on the data in a ContentHandler that could make this happen?

Hmm, no, I don't think any updates should be happening here. We do have a few content handlers but in this context we should only be displaying the content items.

I know this isn't a lot of information to go on so I'll try digging into it a little more.

sebastienros commented 2 years ago

Usually it means some code forgot the await keyword. Can you check all the ways you are using a session and if you might have this issue?

szilardcsere89 commented 2 years ago

Getting the same exception when using a service from another tenant's scope: _shellHost.GetScopeAsync(tenant)

sebastienros commented 2 years ago

Can someone share a repro?

rjpowers10 commented 2 years ago

Can someone share a repro?

It happened again but I'm still not sure why. I'll try to come up with a minimal repro.

SaeidAshian commented 2 years ago

happens to me on the test running on Azure devops, Funny is test are running perfectly on the dev pc. I am using Sqlite inmemory for tests.

ODA669 commented 8 months ago

i have the same error the only thing i did, has been publied my project in the server iis to production and in the log show me that Category: OrchardCore.ContentManagement.Display.ContentItemDisplayCoordinator

TermPartContentDriver thrown from BuildDisplayAsync by InvalidOperationException

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 System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at OrchardCore.ContentManagement.ContentExtensions.Get(ContentElement contentElement, Type contentElementType, String name)
   at OrchardCore.ContentManagement.ContentExtensions.Get[TElement](ContentElement contentElement, String name)
   at OrchardCore.Taxonomies.Drivers.TermPartContentDriver.DisplayAsync(ContentItem model, BuildDisplayContext context)
   at OrchardCore.ContentManagement.Display.ContentItemDisplayCoordinator.BuildDisplayAsync(ContentItem contentItem, BuildDisplayContext context)
sebastienros commented 8 months ago

Initial issue was about OrchardCore.ContentManagement.DefaultContentManagerSession.Store(ContentItem item). Latest comment is about OrchardCore.ContentManagement.ContentExtensions.Get(ContentElement contentElement, Type contentElementType, String name) -> TryGetValue which are two different cases.

ODA669 commented 7 months ago

``hi how i can fix this probleme OrchardCore.ContentManagement.ContentExtensions.Get(ContentElement contentElement, Type contentElementType, String name) mi site works fine but in server show mer this error

OrchardCore.ContentManagement.DefaultContentManagerSession.Store(ContentItem item). Latest comment is about OrchardCore.ContentManagement.ContentExtensions.Get(ContentElement contentElement, Type contentElementType, String name) -> TryGetValue which are two different cases.

Piedone commented 5 months ago

We need a specific repro for this to be able to investigate.

rjpowers10 commented 5 months ago

@Piedone I haven't encountered this issue for some time. In fact I had completely forgotten about it. I'm fine with closing it out. If I do see it again I'll try to get repro steps.

Piedone commented 5 months ago

OK, thank you!

Piedone commented 5 months ago

Sub-1000, yiss! image