IndexOutOfRangeException thrown when web server is processing multiple sessionfactory creation requests

Description

We ran into this issue on our webserver wherein we received two parallel requests to our application and the Nhibernate sessionfactory wasn't yet created and the following error was thrown:
IndexOutOfRangeException: Index was outside the bounds of the array.
and the stack trace is as follows:
Stack trace: at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
at NHibernate.Impl.SessionFactoryObjectFactory.AddInstance(String uid, String name, ISessionFactory instance, IDictionary`2 properties) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionFactoryObjectFactory.cs:line 51
at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionFactoryImpl.cs:line 312
at NHibernate.Cfg.Configuration.BuildSessionFactory() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 1246

The weird thing is that the following exception probably hung up the app pool and eventually slowed down the server. Also, all future requests to that server crashed with the same stack trace. We had to recycle the app pool to get everything back up and running.

We have added code to make sure that the same does not happen i.e the same server does not try generating more than one sessionfactory at any given moment, but I am hoping the Nhibernate community does handle this case and make sure that the application pool does not go to an invalid state and end up slowing down the server,

Environment

None

Activity

Show:

Alex Zaytsev 
September 8, 2014 at 11:31 AM

Close issues resolved in 3.3.3

Oskar Berggren 
February 9, 2013 at 11:58 AM

Fixed in 109724f540d3c1439263a9f7fd045615619917e3.

Oskar Berggren 
February 9, 2013 at 11:37 AM

I'm having trouble recreating the issue, but looking at the code it appears that SessionFactoryObjectFactory has two static dictionaries which are not protected against concurrent modification.

Since the exception originates from inside the non-threadsafe dictionary due to concurrent access, we can assume that it's likely to have corrupted the internal state of the dictionary and anything could happen in future use of the same instance.

Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created January 27, 2012 at 9:38 PM
Updated September 8, 2014 at 11:31 AM
Resolved February 9, 2013 at 11:58 AM
Who's Looking?