Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreach mysteriously hangs on first item of a ResourceSet

Occasionally our site slows down and the RAM usage goes up massively high. Then the app pool stops and I have to restart it. Then it's ok for a few days before the RAM suddenly spikes again and the app pool soon stops. The CPU isn't high.

Before the app pool stops I've noticed that one of our pages always hangs. The line it hangs on is a foreach on a ResourceSet :

var englishLocations = Lang.Countries.ResourceManager.GetResourceSet(new CultureInfo("en-GB"),true,true);
foreach(DictionaryEntry entry2 in englishLocations) // THIS LINE HANGS

We have the same code deployed on a different box and this doesn't happen. The main differences between the two boxes are :

Bad box

  • Window Server 2008 R2 Standard SP 1
  • IIS 7.5.7600.16385
  • .NET 4.5
  • 24GB RAM

Good box

  • Window Server 2008 Server SP 2
  • IIS 7.0.6000.16386 SP 2
  • .NET 4.0
  • 24GB RAM

I've tried adding uploadReadAheadSize="0" to the web.config as described here :

http://rionscode.wordpress.com/2013/03/11/resolving-controller-blocking-within-net-4-5-and-asp-net-mvc/

Which didn't work.

Why would foreach hang? It's hanging on the very first item, actually on the foreach.

Thanks.

like image 791
Typo Johnson Avatar asked Jul 31 '13 08:07

Typo Johnson


1 Answers

I know it is an old post, but nevertheless... There is the potential of a deadlock when iterating over a ResourceSet and at the same time retrieving some other object through from the same Resources.

The problem is that when using a ResourceSet the iterator takes out locks on the internal resource cache of the ResourceReader http://referencesource.microsoft.com/#mscorlib/system/resources/resourcereader.cs,1389 and then in the method AllocateStringNameForIndex takes out a lock on the reader itself: http://referencesource.microsoft.com/#mscorlib/system/resources/resourcereader.cs,447

lock (_reader._resCache) {
     key = _reader.AllocateStringForNameIndex(_currentName, out _dataPosition); // locks the reader

Getting an object takes out the same locks int the opposite order:

http://referencesource.microsoft.com/#mscorlib/system/resources/runtimeresourceset.cs,300 and http://referencesource.microsoft.com/#mscorlib/system/resources/runtimeresourceset.cs,335

lock(Reader) {
    ....
    lock(_resCache) {
       _resCache[key] = resLocation;
    }
}

This can lead to a deadlock. We had this exact issue recently..

like image 135
John Sloper Avatar answered Oct 10 '22 18:10

John Sloper