This question is locked. New answers and comments are not allowed.
I am using the Telerik MVC grid in a web application and have found a concurrency issue in GridBoundColumn. When multiple users load a page with a grid on it at the exact same time, an exception like the following is generated:
It looks to me that this is caused by unsafe access to the static expressionCache dictionary in the GridBoundColumn class. Writing to a dictionary that may be read from by other threads at the same time is a known unsafe operation in .NET; see the thread safety portion of MSDN's dictionary documentation. Instead, either .NET Framework 4 should be targeted so that the built-in ConcurrentDictionary can be used, or if you need to stay on 3.5 an existing locking mechanism such as ReaderWriterLockSlim should be used to prevent anyone from reading from the dictionary during a write operation.
I believe this was introduced in service pack 2011.3.1306.
Some sample code that should fix this follows.
System.Web.HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. ---> System.Web.HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. ---> System.Web.HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Telerik.Web.Mvc.UI.GridBoundColumn`2..ctor(Grid`1 grid, Expression`1 expression)
at Telerik.Web.Mvc.UI.Fluent.GridColumnFactory`1.Bound[TValue](Expression`1 expression)
It looks to me that this is caused by unsafe access to the static expressionCache dictionary in the GridBoundColumn class. Writing to a dictionary that may be read from by other threads at the same time is a known unsafe operation in .NET; see the thread safety portion of MSDN's dictionary documentation. Instead, either .NET Framework 4 should be targeted so that the built-in ConcurrentDictionary can be used, or if you need to stay on 3.5 an existing locking mechanism such as ReaderWriterLockSlim should be used to prevent anyone from reading from the dictionary during a write operation.
I believe this was introduced in service pack 2011.3.1306.
Some sample code that should fix this follows.
private static ReaderWriterLockSlim expressionCacheLock = new ReaderWriterLockSlim();//Fix Telerik bug with concurrency caused by concurrent access to an unprotected static dictionary//Note: This would all be better done with a ConcurrentDictionary, bit it appears that Telerik is not yet using .NET 4expressionCacheLock.EnterReadLock();bool hasEntry;try{ hasEntry = expressionCache.TryGetValue(expression.ToString(), out value);}finally{ expressionCacheLock.ExitReadLock();}if (!hasEntry){ expressionCacheLock.EnterWriteLock(); try { expressionCache[expression.ToString()] = value = expression.Compile(); } finally { expressionCacheLock.ExitWriteLock(); }}