Duplicate Objects detected for Instance X of Type Y - structuremap

I'm getting the following exception from structure map - "Duplicate Objects detected for Instance d54e25dc-d19c-4d70-99d1-56bd2502d203 of Type ..."
The stack trace is:
at StructureMap.InstanceCache.Set(Type pluginType, Instance Instance, Object result) in c:\code\structuremap\Source\StructureMap\InstanceCache.cs:line 60
at StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 159
at StructureMap.BuildSession.<>c_DisplayClass3.<.ctor>b_1() in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 34
at StructureMap.BuildSession.CreateInstance(Type pluginType) in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 192
at StructureMap.BuildSession.GetInstance[T]() in c:\code\structuremap\Source\StructureMap\BuildSession.cs:line 78
This exception happens only from time to time and is very difficult to reproduce.
What I've found out so far:
Calling ObjectFactory.Container.GetInstance(type) will create a new BuildSession with a new non-static cache.
The call will reach BuildSession.CreateInstance(Type pluginType, Instance instance) and no instance is in the cache yet. (https://github.com/structuremap/structuremap/blob/master/Source/StructureMap/BuildSession.cs#L149-166)
Now we'll go on to _builder.Resolve(pluginType, instance, this);
Somewhere during Resolve() the object will get into the cache and later in CreateInstance we're saving it into the cache once again, receiving the exception.
That object is configured with Singleton lifecycle. The application is WCF service hosted in IIS.
Any help with finding the cause is appreciated.

Related

Stress Testing - EntityFramework issue - DBContext Creation

I am doing stress testing for web api (webapi2) with 20 users on boarding in 0 seconds. I am getting the following errors.
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.InvalidOperationException: Invalid operation. The connection is closed.
Another error
System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: The connection was not closed. The connection's current state is connecting.
My code to get the DBContext, each time a new DBContext is getting created:
public static ForcesChecker_Context GetDataContext()
{
return new ForcesChecker_Context();
}
For one web api request this code is getting executed multiple times and multiple instances of this object is getting created. When I call 20 users at a time, it generates 20* ~10 = ~200 objects are created.
My connection string:
Min Pool Size=1;Max Pool Size=200;
It seems there is a race condition.
What settings would help to allow more users to access my system concurrently?
I fixed it. The reason was, Connection Leak. There are other places in the application wherein the DBContext object wasn't disposed properly. Particularly in UnitOfWork class, DBContext object is used, but not disposed inside the Dispose() method. That led to the connection leak. Subsequently, that led to the race condition when new threads (http requests) try to use a connection from the connection pool. Here is the solution code.
public class UnitOfWork: IDisposable, IUnitOfWork
{
ForcesChecker_Context forcesContext; //EntityFramework DBContext
...
public void Dispose()
{
forcesContext.Dispose(); //Leak prevented by this new line.
}
...
}
Thumb rule is, always remember to use Transient Life Time for the DBContext. That is a new instance every time, and dispose them immediately after use.

Xamarin iOS c__AnonStorey1

I retrieve this crash log about my app.
System.NullReferenceExceptionObject reference not set to an instance of an object
Raw TouristApp.PuntiEventiView.c__AnonStorey1.<>m__0()
Foundation.NSActionDispatcher.Apply()
UIKit.UIApplication.UIApplicationMain(int, string[], intptr, intptr)(wrapper managed-to-native)
UIKit.UIApplication.Main(string[] args, IntPtr principal, IntPtr delegate)
UIKit.UIApplication.Main(string[] args, string principalClassName, string delegateClassName)
TouristApp.Application.Main(string[] args)
I have no clue about the error, in my class (PuntiEventiView) there is no call to c__AnonStorey1.m (it seems a Mono file).
Any idea?
Debug your app step by step. You're using an anonymous delegate or lambda somewhere which causes a NULL ref exception.
The c__AnonStorey1 is a compiler generated class which captures your local variables.
(Using my crystal ball here to guess, because you are not showing any code)

Why private static variable becomes null at some point and what can I do to resolve?

A picture is worth a thousand words:
On first page load, result is not null but at some point, after some time, when Gmail action is called from Javascript, it becomes null (after one of these 10 minute interval calls). It is declared as private static, initialized in Index action and should be alive (not null) all the time.
I managed to catch it by leaving the app running it in a Debug mode for a few hours.
Thank you.
Why dont you just save the cancellation token and recreate the "result" instance on Gmail() function call ?
private CancellationToken token = token; (on index call)
public ActionResult Gmail() {
result = new Authresult(token);
...
}
To diagnose the problem first it is work to double check whether you are accessing the variable in the same AppDomain where it was initialized - to check this you could just add some logging. It could be possible that this is a different AppDomain, because some event triggered IIS AppDomain pool recycling.
If it is the case, then you have 2 options:
either store the state using another mechanism or
have lazy initialization on demand with a null check, so the value can be initialized each time it's needed

Attempting to JIT compile method exception in MonoTouch.CoreGraphics.CGContext.DrawPDFPage

Here is the stack
System.ExecutionEngineException: Attempting to JIT compile method '(wrapper managed-to-native) MonoTouch.CoreGraphics.CGContext:CGContextDrawPDFPage (intptr,intptr)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.
at MonoTouch.CoreGraphics.CGContext.DrawPDFPage (MonoTouch.CoreGraphics.CGPDFPage page) [0x00000] in :0
at Neva.PdfViewer.PageContentView.Draw (MonoTouch.CoreGraphics.CGContext context) [0x00000] in :0
at Neva.PdfViewer.PageContentTile.DrawInContext (MonoTouch.CoreGraphics.CGContext ctx) [0x00000] in :0 [7.1.1]
While we were not able to recreate this problem in QA or unit testing, this exception randomly happens on AppStore distributed installations.
Looking at DrawPDFPage in CGContext
public void DrawPDFPage (CGPDFPage page)
{
CGContext.CGContextDrawPDFPage (this.handle, page.handle);
}
where CGContextDrawPDFPage is a P/Invoke function
[DllImport ("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")]
private static extern void CGContextDrawPDFPage (IntPtr c, IntPtr page);
doesn't give us any hint. The link above http://docs.xamarin.com/ios/about/limitations is not really helpful.
My question is, what could be causing such an exception? What are steps to debug and fix it?
This exception (System.ExecutionEngineException: Attempting to JIT compile method ...) should be 100% reproducible.
The fact that it isn't, points at something else (and probably worse): memory corruption of some sort.
However without some way to (at least randomly) reproduce it yourself, it's close to impossible to track down.
My initial suggestion would be to try to figure out if there's anything at all you can figure out in order to be able to create a test case yourself:
Does it only occur for a certain set of devices (only iPad 2 for instance)?
Does it only occur for a certain set of customers (only customers in Iceland for instance)?
Is the exception exactly the same every time, or does the P/Invoke / stack trace vary?
Is it a low-memory condition? Did the app get memory warnings prior to this occurring?
Are there any required steps in your app (i.e. if the user does X+Y it may crash, but if he does Y+X, then it never crashes)?

Umbraco Lucene.Net.Index.MergePolicy.MergeException What is causing this?

I am using Umbraco and for some reason out of the blue I get the Visual Studio JIT Debugger popup asking me if I want to debug and exception with w3wp.exe, I attached to it and found the following exception, I can't find any cause for this as I haven't added anything new.
Lucene.Net.Index.MergePolicy.MergeException was unhandled
Message=Exception of type 'Lucene.Net.Index.MergePolicy+MergeException' was thrown.
Source=Lucene.Net
StackTrace:
at Lucene.Net.Index.ConcurrentMergeScheduler.HandleMergeException(Exception exc)
at Lucene.Net.Index.ConcurrentMergeScheduler.MergeThread.Run()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: Lucene.Net.Index.CorruptIndexException
Message=doc counts differ for segment _3qw: fieldsReader shows 1025 but segmentInfo shows 1
Source=Lucene.Net
StackTrace:
at Lucene.Net.Index.IndexWriter.HandleMergeException(Exception t, OneMerge merge)
at Lucene.Net.Index.IndexWriter.Merge(OneMerge merge)
at Lucene.Net.Index.ConcurrentMergeScheduler.DoMerge(OneMerge merge)
at Lucene.Net.Index.ConcurrentMergeScheduler.MergeThread.Run()
InnerException:
Does anyone have any idea what is causing this or how to fix it?
Thanks
I found the answer from this link:
http://our.umbraco.org/forum/ourumb-dev-forum/bugs/16066-Missing-LuceneNet-assembly
on Dunfee posted this reply 6 months ago
Just went through a migration from one location to another. I removed the contents of App_Data/TEMP/ExamineIndexes and after a minute or so of watching the browser spinner (while the Lucene cache was being rebuilt) the site came up.

Resources