Bug 25522 - System.Runtime.Caching - Crash when add / remove items
Summary: System.Runtime.Caching - Crash when add / remove items
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Alexander Kyte
URL:
Depends on:
Blocks:
 
Reported: 2014-12-19 05:16 UTC by Jose Navero
Modified: 2015-12-16 17:09 UTC (History)
6 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jose Navero 2014-12-19 05:16:15 UTC
When you add and remove items into cache many times, it will crash.
The exception and test code is attached

Test code:
--------------------------------
using System;
using System.Runtime.Caching;

namespace TestCache
{
    class MainClass
    {
		static MemoryCache cache;

                public static void Main (string[] args)
		{
			try 
			{
				cache = new MemoryCache ("___NewCache", null);
				//Test to Crash Memory cache
				var policy = new CacheItemPolicy ();
				policy.SlidingExpiration = new TimeSpan (0, 10, 0);

				System.Threading.Tasks.Parallel.For (1, 1000, t => {
					for (double i = -1; i <= 1000; i++) 
					{
						cache.Remove (i.ToString ());
						cache.Add (new CacheItem (i.ToString (), i), policy);
					}
				});
			} catch (Exception ex) {
				Console.WriteLine (ex.ToString ());
			}
			Console.WriteLine ("End");
		}
    }
}


Exception:
----------------------------
  at (wrapper stelemref) object:virt_stelemref_sealed_class (intptr,object)
  at System.Runtime.Caching.MemoryCacheEntryPriorityQueue.Enqueue (System.Runtime.Caching.MemoryCacheEntry item) [0x00000] in <filename unknown>:0 
  at System.Runtime.Caching.MemoryCacheContainer.UpdateExpirable (System.Runtime.Caching.MemoryCacheEntry entry) [0x00000] in <filename unknown>:0 
  at System.Runtime.Caching.MemoryCacheContainer.AddToCache (System.String key, System.Runtime.Caching.MemoryCacheEntry entry, Boolean update) [0x00000] in <filename unknown>:0 
  at System.Runtime.Caching.MemoryCacheContainer.AddOrGetExisting (System.String key, System.Object value, System.Runtime.Caching.CacheItemPolicy policy) [0x00000] in <filename unknown>:0 
  at System.Runtime.Caching.MemoryCache.DoAddOrGetExisting (System.String key, System.Object value, System.Runtime.Caching.CacheItemPolicy policy, System.String regionName) [0x00000] in <filename unknown>:0 
  at System.Runtime.Caching.MemoryCache.AddOrGetExisting (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) [0x00000] in <filename unknown>:0 
  at System.Runtime.Caching.ObjectCache.Add (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) [0x00000] in <filename unknown>:0 
  at TestCache.MainClass+<Main>c__AnonStorey0.<>m__0 (Int32 t) [0x0004d] in /home/myuser/TestCache/TestCache/Program.cs:23 
  at System.Threading.Tasks.Parallel+<For>c__AnonStorey0.<>m__0 (Int32 index, System.Threading.Tasks.ParallelLoopState state) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.Parallel+<For>c__AnonStorey1.<>m__0 (Int32 i, System.Threading.Tasks.ParallelLoopState s, System.Object l) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.Parallel+<For>c__AnonStorey2`1[System.Object].<>m__0 () [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.TaskActionInvoker+ActionInvoke.Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.Task.ThreadStart () [0x00000] in <filename unknown>:0
Comment 1 Zoltan Varga 2015-01-02 18:56:57 UTC
I can't reproduce this with mono master on osx.
Comment 2 Jose Navero 2015-01-02 19:05:32 UTC
Hi,

I use Linux Mint 17 (Quiana).

if you need more info about all my system, you can tell me.

Thank you
Comment 3 Zoltan Varga 2015-01-02 19:08:33 UTC
What mono version is this ?
Comment 4 Jose Navero 2015-01-02 19:38:39 UTC
Hi, 

I Use:

~/TestCache/TestCache/bin/Debug $ mono -V
Mono JIT compiler version 3.99.0 (master/5314425 Thu Nov 27 19:45:12 CET 2014)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            sgen



Thank you
Comment 5 Jose Navero 2015-01-02 19:55:01 UTC
Hi again, 


To reproduce it, these are my steps:

- Open the code with MonoDevelop (I use MonoDevelop 5.1)
- When I run the proyect, the code will crash and it throw an exception.


Thank you
Comment 6 Alexander Kyte 2015-04-20 17:40:36 UTC
$ mono cc.exe
System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object
  at System.Collections.Generic.SortedSet`1[T].RotateRightLeft (System.Collections.Generic.Node node) <0x10893ed60 + 0x00020> in <filename unknown>:0
  at System.Collections.Generic.SortedSet`1[T].InsertionBalance (System.Collections.Generic.Node current, System.Collections.Generic.Node& parent, System.Collections.Generic.Node grandParent, System.Collections.Generic.Node greatGrandParent) <0x10893da00 + 0x00125> in <filename unknown>:0
  at System.Collections.Generic.SortedSet`1[T].AddIfNotPresent (System.Collections.Generic.T item) <0x10893c890 + 0x00355> in <filename unknown>:0
  at System.Collections.Generic.SortedSet`1[T].Add (System.Collections.Generic.T item) <0x10893c510 + 0x00020> in <filename unknown>:0
  at System.Runtime.Caching.CacheEntryCollection.Add (System.Runtime.Caching.MemoryCacheEntry entry) <0x10893c4d0 + 0x0002b> in <filename unknown>:0
  at System.Runtime.Caching.CacheUsage.Add (System.Runtime.Caching.MemoryCacheEntry entry) <0x10893ced0 + 0x00137> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCacheStore.AddToCache (System.Runtime.Caching.MemoryCacheEntry entry) <0x10893c010 + 0x001c0> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCacheStore.AddOrGetExisting (System.Runtime.Caching.MemoryCacheKey key, System.Runtime.Caching.MemoryCacheEntry entry) <0x10893b820 + 0x00200> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCache.AddOrGetExistingInternal (System.String key, System.Object value, System.Runtime.Caching.CacheItemPolicy policy) <0x108939a70 + 0x002d3> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCache.AddOrGetExisting (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) <0x1089397a0 + 0x00051> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCache.Add (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) <0x1089396e0 + 0x00021> in <filename unknown>:0
  at TestCache.MainClass+<Main>c__AnonStorey0.<>m__0 (Int32 t) <0x108938380 + 0x0016d> in <filename unknown>:0
  at System.Threading.Tasks.Parallel+<ForWorker>c__AnonStorey3`1[TLocal].<>m__1 () <0x107af35e0 + 0x0030a> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Threading.Tasks.Task.ThrowIfExceptional (Boolean includeTaskCanceledExceptions) <0x1078cdc50 + 0x00037> in <filename unknown>:0
  at System.Threading.Tasks.Task.Wait (Int32 millisecondsTimeout, CancellationToken cancellationToken) <0x1078cef50 + 0x00097> in <filename unknown>:0
  at System.Threading.Tasks.Task.Wait () <0x1078cee20 + 0x00028> in <filename unknown>:0
  at System.Threading.Tasks.Parallel.ForWorker[TLocal] (Int32 fromInclusive, Int32 toExclusive, System.Threading.Tasks.ParallelOptions parallelOptions, System.Action`1 body, System.Action`2 bodyWithState, System.Func`4 bodyWithLocal, System.Func`1 localInit, System.Action`1 localFinally) <0x1078c4340 + 0x00630> in <filename unknown>:0
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object
  at System.Collections.Generic.SortedSet`1[T].RotateRightLeft (System.Collections.Generic.Node node) <0x10893ed60 + 0x00020> in <filename unknown>:0
  at System.Collections.Generic.SortedSet`1[T].InsertionBalance (System.Collections.Generic.Node current, System.Collections.Generic.Node& parent, System.Collections.Generic.Node grandParent, System.Collections.Generic.Node greatGrandParent) <0x10893da00 + 0x00125> in <filename unknown>:0
  at System.Collections.Generic.SortedSet`1[T].AddIfNotPresent (System.Collections.Generic.T item) <0x10893c890 + 0x00355> in <filename unknown>:0
  at System.Collections.Generic.SortedSet`1[T].Add (System.Collections.Generic.T item) <0x10893c510 + 0x00020> in <filename unknown>:0
  at System.Runtime.Caching.CacheEntryCollection.Add (System.Runtime.Caching.MemoryCacheEntry entry) <0x10893c4d0 + 0x0002b> in <filename unknown>:0
  at System.Runtime.Caching.CacheUsage.Add (System.Runtime.Caching.MemoryCacheEntry entry) <0x10893ced0 + 0x00137> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCacheStore.AddToCache (System.Runtime.Caching.MemoryCacheEntry entry) <0x10893c010 + 0x001c0> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCacheStore.AddOrGetExisting (System.Runtime.Caching.MemoryCacheKey key, System.Runtime.Caching.MemoryCacheEntry entry) <0x10893b820 + 0x00200> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCache.AddOrGetExistingInternal (System.String key, System.Object value, System.Runtime.Caching.CacheItemPolicy policy) <0x108939a70 + 0x002d3> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCache.AddOrGetExisting (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) <0x1089397a0 + 0x00051> in <filename unknown>:0
  at System.Runtime.Caching.MemoryCache.Add (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) <0x1089396e0 + 0x00021> in <filename unknown>:0
  at TestCache.MainClass+<Main>c__AnonStorey0.<>m__0 (Int32 t) <0x108938380 + 0x0016d> in <filename unknown>:0
  at System.Threading.Tasks.Parallel+<ForWorker>c__AnonStorey3`1[TLocal].<>m__1 () <0x107af35e0 + 0x0030a> in <filename unknown>:0 <---

End


I can confirm on mono/master on osx.
Comment 7 Alexander Kyte 2015-04-20 18:37:22 UTC
The problem was our use of the non-threadsafe SortedSet in CacheEntryCollection.cs. I should have a fix in a PR soon.
Comment 8 Alexander Kyte 2015-04-20 19:08:51 UTC
https://github.com/mono/mono/pull/1725
Comment 9 Alexander Köplinger [MSFT] 2015-12-16 17:09:00 UTC
I added another fix in https://github.com/mono/mono/pull/2324 so the problems shouldn't occur anymore.