After some investigation a colleague of mine said, "Hold on, isn't this lock object supposed to static?" Sure enough we had a block of code similar the following:
public class SomeClassWe fixed the code by simply making '_lockObject' static as follows:
{
private readonly IDictionary<string, string> _someList;
private readonly object _lockObject = new object();
public SomeClass()
{
_someList = new Dictionary<string, string>();
}
public string Get(string key)
{
string value;
if(!_someList.TryGetValue(key, out value))
{
lock (_lockObject)
{
if (!_someList.TryGetValue(key, out value))
{
value = GetValueFor(key);
_someList.Add(key, value);
}
}
}
return value;
}
}
private static readonly object _lockObject = new object();When we pushed this to live and all was fixed. It was a pretty obvious mistake but the symptoms were not immediately obvious to us as to what to look for so i thought I would put this out there on the tinterwebs for if anyone else encounters the same issue.
No comments:
Post a Comment