New Thread Race Issue

New Thread Race Issue

Old forum URL: forums.lhotka.net/forums/t/4628.aspx


JTWebMan posted on Monday, April 07, 2008

We had add the thread issue fixes to the 2.1.4 version as we are still running on it and then found another thread race issue. I made this change to fix the issue on the CSLA.Security.AuthorizationRulesManager object in the GetRolesForProperty(string) method:

Dim currentRoles As RolesForProperty = Nothing
If Not RulesList.ContainsKey(propertyName) Then
    SyncLock RulesList
        If Not RulesList.ContainsKey(propertyName) Then
            currentRoles = New RolesForProperty
            RulesList.Add(propertyName, currentRoles)
        End If
    End SyncLock
Else
    currentRoles = RulesList.Item(propertyName)
End If
Return currentRoles

I attached the patch file to the post though I think my tabs are off from yours.

This was the stack trace of the error:

System.ArgumentException: An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at Csla.Security.AuthorizationRulesManager.GetRolesForProperty(String propertyName) at Csla.Security.AuthorizationRules.HasReadAllowedRoles(String propertyName) at Csla.ReadOnlyBase`1.CanReadProperty(String propertyName) at Csla.ReadOnlyBase`1.CanReadProperty(String propertyName, Boolean throwOnFalse) at ASH2.Data.HRMS.HRMSRoleProvider.GetRolesForUser(String username) in C:\Projects\Namespaces\ASH2.Data.HRMS\HRMSRoleProvider.vb:line 43 at System.Web.Security.RolePrincipal.IsInRole(String role) at System.Web.Configuration.AuthorizationRule.IsTheUserInAnyRole(StringCollection roles, IPrincipal principal) at System.Web.Configuration.AuthorizationRule.IsUserAllowed(IPrincipal user, String verb) at System.Web.Configuration.AuthorizationRuleCollection.IsUserAllowed(IPrincipal user, String verb) at System.Web.Security.UrlAuthorizationModule.OnEnter(Object source, EventArgs eventArgs) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

JTWebMan replied on Tuesday, April 08, 2008

There was an issue with that change. The correct fix should be this:

Dim currentRoles As RolesForProperty = Nothing
If Not RulesList.ContainsKey(propertyName) Then
    SyncLock RulesList
        If Not RulesList.ContainsKey(propertyName) Then
            currentRoles = New RolesForProperty
            RulesList.Add(propertyName, currentRoles)
        Else
            currentRoles = RulesList.Item(propertyName)
        End If
    End SyncLock
Else
    currentRoles = RulesList.Item(propertyName)
End If
Return currentRoles

JTWebMan replied on Sunday, April 13, 2008

We figured out this was a threading issue on our side. CSLA object are not thread safe so we loaded a immutable object into cache instead.

Copyright (c) Marimer LLC