This is a migrated thread and some comments may be shown as answers.

[Solved] Concurrency issue with GridBoundColumn and static dictionary

1 Answer 24 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Edward
Top achievements
Rank 1
Edward asked on 09 Feb 2012, 10:09 PM
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:

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 4
expressionCacheLock.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();
    }
}

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 10 Feb 2012, 07:48 AM
Hello,

 We have fixed this issue recently. Please open a support ticket so we can send you the hotfix build.

Regards,
Atanas Korchev
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Grid
Asked by
Edward
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or