Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An unhandled exception of type 'System.StackOverflowException' occurred in VimService55.XmlSerializers.dll

I am working on an asp.net mvc-5 web application and i am using ap.net version 4.5.

Inside my web application I am executing some power-shell scripts to get some hardware info about some servers and VMs, and get back the results inside my code, as follows:

var shell = PowerShell.Create();
string PsCmd =       
    "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" 
    + vCenterName.Trim() + "';$vCenterAdmin = '" + vCenterUsername.Trim() 
    + "' ;$vCenterPassword = '" + vCenterPassword + "';" 
    + System.Environment.NewLine;

PsCmd += "$VIServer = Connect-VIServer -Server $vCenterServer -User $vCenterAdmin -Password $vCenterPassword;" + System.Environment.NewLine;
PsCmd += "Get-VMHost " + System.Environment.NewLine;

shell.Commands.AddScript(PsCmd);

dynamic results = shell.Invoke(); 

var temp_result = results[0].BaseObject == null ? results[0] : results[0].BaseObject;
var otherIdentityInfo = temp_result.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo;

now currently when i run this inside my Visual Studio 2012 professional , i will get the following exception :-

System.StackOverflowException was unhandled
An unhandled exception of type 'System.StackOverflowException' occurred in VimService55.XmlSerializers.dll

on

var otherIdentityInfo = temp_result.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo;

So can anyone adivce on this? I know that in general a "StackOverflowException" exception is related to the fact that too many data exists inside the stack, but in my case I do not have control over this data as I am scanning VM server information. So can anyone advice on this please?

EDIT

I am not sure what is really raising the error (the debugger OR the code)? because when i try calling this code on the hosted application inside IIS (not inside Visual Studio) I will get null value for the otherIdentityInfo variable, rather than getting an exception. However, when i debug the code inside Visual Studio using Autos i will get the exception, so as @JmaesP mentioned the exception is being raised by the debugger, but not sure how i can debug this value to see why i am getting null??

like image 567
john Gu Avatar asked Nov 25 '16 17:11

john Gu


2 Answers

A stack overflow exception from an XML serializer might indicate an issue with one of you serializable types. If the type declaration by itself is somewhat recursice, the default XML serializer will end up in inifite recursion. Consider this example:

[Serializable()]
public class Foo : IEnumerable<Foo>
{
    public Foo()
    {
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    public IEnumerator<Foo> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    public void Add(Foo item)
    {
        throw new NotImplementedException();
    }
}

The default XML serializer first tries to figure out how to (de-)serialize an instance of Foo, then it tries to figure out how to (de-)serialize an IEnumerable<Foo>, and to do that it tries to figure out how to (de-)serialize a Foo — resulting in infinite recursion. Typically, a solution to this would to use a custom serializer as described here.

However, in your case the serializer is provided by the third-party component. The exception likely occurs during the serialization/deserialization that is happening when objects are passed from the PowerShell session to your process. So what you could to is to change the object that is returned from the PowerShell script. Instead of returning a VMHost object (requiring to serialize the entire object tree of the VMHost object), you could just return the SystemInfo or OtherIdentifyingInfo.

like image 197
Dirk Vollmar Avatar answered Sep 19 '22 14:09

Dirk Vollmar


Large recursions can cause out-of-stack errors. Your problem is likely that your program is attempting to consume an infinite amount /very large amount of stack.

Without seeing your stack trace, it's a bit difficult to provide a definitive answer, but I think sticking to the basics leads me to believe the source of your StackOverflow is the PsCmd += which continuously adding the data into the stack and results in StackOverflowException.

You can increase the stack size by using the below code:

Editbin.exe /Stack:14000000 "$(TargetDir)MyProject.exe"

Have you analyzed your code to find out that how deep your recursion goes on average? Does it always hit a StackOverflow? Try hardcoding a single entity and see the result.

Since 64 bit code can up more stack space than equivalent 32 bit code, large recursions can cause out-of-stack errors to occur earlier.

In that case, making the stack larger is not a good idea either. Instead we should find the deeply recursive algorithm and make it into an iterative one.

For Stack-Trace; You can read up this property: Environment.StackTrace.

If the stacktrace exceded a specific threshold that you preset, you can return the function.

Note: From .Net 2.0 and above, you cannot get a StackOverflowException object using a try-catch block.

Let me know if at all it helps you.

like image 30
Ranadip Dutta Avatar answered Sep 19 '22 14:09

Ranadip Dutta