none
Unity and Random “Index was outside the bounds of the array” exception RRS feed

  • Question

  • We are running web site with around 15.000 realtime user (google analytics) (Around 1000 request/sec (perf counters)). 

    We have two web server behind load balancer. 

    Sometimes every day sometimes 1 time in a week one of our web servers stop execute requests and start to response with error and every request is logging following exception: "System.IndexOutOfRangeException - Index was outside the bounds of the array." 

    Our environment : IIS 8.5, .Net 4.5.0, Mvc 5.1.0, Unity 3.5 (same state with 3.0), WebActivatorEx 2.0 In IIS, Worker Process 1 and other settings are with defaults. 

    We could not catch any specific scenario made this error. After App pool recycle everything start with no problem. And before every request respond with error, there is not any error related with it. 

    There is one question asked in the past with related old Unity version: https://unity.codeplex.com/discussions/328841http://unity.codeplex.com/workitem/11791 Could not see anything I can do about it. 

    Here exception details:

    System.IndexOutOfRangeException
    Index was outside the bounds of the array.
    
    System.IndexOutOfRangeException: Index was outside the bounds of the array.
       at System.Collections.Generic.List`1.Enumerator.MoveNext()
       at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Microsoft.Practices.Unity.NamedTypesRegistry.RegisterType(Type t, String name)
       at Microsoft.Practices.Unity.UnityDefaultBehaviorExtension.OnRegisterInstance(Object sender, RegisterInstanceEventArgs e)
       at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
       at Microsoft.Practices.Unity.UnityContainer.RegisterInstance(Type t, String name, Object instance, LifetimeManager lifetime)
       at Microsoft.Practices.Unity.UnityContainerExtensions.RegisterInstance[TInterface](IUnityContainer container, TInterface instance, LifetimeManager lifetimeManager)
       at DemoSite.News.Portal.UI.App_Start.UnityConfig.<>c__DisplayClass1.<RegisterTypes>b__0()
       at DemoSite.News.Portal.Core.Controller.BaseController.Initialize(RequestContext requestContext)
       at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
       at System.Web.Mvc.Async.AsyncResultWrapper.Begin[TState](AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

    Maybe it is related with high traffic. Is there anyone encounter a problem like that and any solution ? 


    jandark

    Friday, July 18, 2014 1:39 PM

Answers

  • That is not unusual.  What timeout are you using?  Occassionally delays can be longer in windows. With that much data you may want to see if there is a relationship between memory usage and the error.  You could have a memory leak.  I would also check the Control Panel : Administrative Tools : Event Viewer to see if there is additional details at the time of the error.

    jdweng

    Saturday, July 19, 2014 12:16 PM

All replies

  • Did you read the following comments ate the bottom of the webpage?

    Seems I found the root of the issue. It is related to construct which creates and sets UnityServiceLocator in the application. 
    
     ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container)); 
    
     Registration is called in UnityServiceLocator constructor. So when multiple threads were trying to access service locator, this issue raised. 
    
     Similar issues are explained well in the article:http://blogs.msdn.com/b/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx

    I think what is happening is you are trying to move the next item ion the enumeration before all the data has been received or the object is still being constructed when you perform the movenext. If the enumerator didn't exist you would get a null reference. So the enumerator object was set to a serialized/deserialized buffer, but not all the data was received when you tried to perform the movenext causing the exception. you need to make sure all the data has been received before gong to the next enumeration. You should also check with http that you get a status of "200 done" before using any of the received data.

    jdweng

    Friday, July 18, 2014 9:34 PM
  • Yes I read it and I also read "Fixed in the latest release of Unity (2.1.505.2). Get it via NuGet."

    I am using Unity 3.5 so there should not be a problem like that.

    You assume that I am changing container with registering new class/instance etc. 

    But everything is registered before App start and after that container never change.

    And it gives this exception suddenly while web site receiving lots of request for  long time properly.

     


    jandark

    Friday, July 18, 2014 9:48 PM
  • Connecting to webpages often gives random errors.  That is why I said to make sure that you get the status "200 done" before you attempt to use the data from the server.

    jdweng

    Saturday, July 19, 2014 12:14 AM
  • I am not sure which data you are talking about but we are seeing for example 30 million 200 done and suddenly this exception occurs.

    jandark

    Saturday, July 19, 2014 10:41 AM
  • That is not unusual.  What timeout are you using?  Occassionally delays can be longer in windows. With that much data you may want to see if there is a relationship between memory usage and the error.  You could have a memory leak.  I would also check the Control Panel : Administrative Tools : Event Viewer to see if there is additional details at the time of the error.

    jdweng

    Saturday, July 19, 2014 12:16 PM
  • Hi,

    Here is problem and solution.

    edit: forget to add the link : http://stackoverflow.com/questions/24822232/unity-and-random-index-was-outside-the-bounds-of-the-array-exception


    jandark


    • Edited by mehmetim Wednesday, July 30, 2014 6:25 PM edit
    Tuesday, July 22, 2014 1:56 PM