MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.33k stars 458 forks source link

Likely concurrency issue in CacheManager.Microsoft.Extensions.Caching.Memory #267

Closed jkernsva closed 4 years ago

jkernsva commented 5 years ago

Our application is occasionally getting into a state in which its MemoryCache will throw exceptions when used. Once in this state, all further requests will fail and we must restart the process to get it into a healthy state again.

We speculate that this is related to concurrent writes to one of its internal metadata structures. Here is a partial stack trace from one of the occurences:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.HashSet`1.AddIfNotPresent(T value)
   at CacheManager.MicrosoftCachingMemory.MemoryCacheExtensions.RegisterChild(MemoryCache cache, Object parentKey, Object childKey)
   at CacheManager.MicrosoftCachingMemory.MemoryCacheHandle`1.AddInternalPrepared(CacheItem`1 item)
   at CacheManager.Core.BaseCacheManager`1.AddToHandles(CacheItem`1 item, Int32 foundIndex)
   at CacheManager.Core.BaseCacheManager`1.GetCacheItemInternal(String key, String region)
   at CacheManager.Core.BaseCacheManager`1.GetOrAddInternal(String key, String region, Func`3 valueFactory)

I have submitted a pull request, #266, that I believe will address this problem. We have been testing this build internally and we have not seen the issue reoccur.

MichaCo commented 5 years ago

Hi @jkernsva Thanks for letting me know and thanks for the PR. I've seen that one already. I'll comment on that if I find anything. But looks good overall.

rawrspace commented 5 years ago

I am also experiencing the same issues and the fixes @jkernsva provided in #266 appear to have resolved my problem. It would be great to see this incorporated and released as an updated NuGet package.

tynorton commented 5 years ago

Any idea when this will be merged / released? We are experiencing it too.

tynorton commented 4 years ago

Bump.

jkernsva commented 4 years ago

This has been resolved in #266