How and why do TypeInitializationExceptions crash dotnet on linux, and how can I prevent this defensively?

111 Views Asked by At

I've been using dotnet on linux for a bit now and I've noticed that my dotnet process crashes hard and produces a core dump whenever a TypeInitializationException occurs.

Of course the solution is to fix the underlying type initialization issue, but I am wondering why this sort of exception is not one from which dotnet can recover, and 2) whether there is anything I can do to prevent a dotnet crash if such an exception occurs? Is there something worse about unhandled TypeInitializationException as opposed to other unhandled exceptions?

example

For instance, one of the API calls to my application eventually hits the following lines of code.

While load-testing this API call in particular I started seeing my dotnet crash.

using (var gThumbNail = System.Drawing.Graphics.FromImage(scaledMap)) {
    gThumbNail.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.FromName(backgroundcolor)), 0, 0, width, height);

    int masterWidth = large.Width;
    int masterHeight = large.Height;
    int heightOffset = 0;
    int widthOffset = 0;
    int oldHeight = height;
    int oldWidth = width;

    // Scale or crop desired size thumb from master image
    if (cropthumb == false) {
        var pluckRect = new System.Drawing.Rectangle(large.Width / 2 - width / 2, large.Height / 2 - height / 2, width, height);
        var cropRect = new System.Drawing.Rectangle(0, 0, width, height);
        gThumbNail.DrawImage(large, cropRect, pluckRect, System.Drawing.GraphicsUnit.Pixel);
    } else {
        gThumbNail.DrawImage(large, widthOffset / 2, heightOffset / 2, width, height);
    }
}

You can reproduce this by instantiating a new System.Drawing.Graphics without all this particular code - on a Linux machine without GDI installed. Sometimes to reproduce the crash I have to hit it with some load (5 or 10 concurrent users).

When I inspected the core dump, I found:

Nested exception -------------------------------------------------------------
Exception object: 00007fcab86500b0
Exception type:   System.TypeInitializationException
Message:          The type initializer for 'Gdip' threw an exception.
InnerException:   System.DllNotFoundException, Use !PrintException 00007FCAB864AAC8 to see more.
StackTrace (generated):
    SP               IP               Function
    00007FC994FF5A70 00007FCFCD621866 System.Drawing.Common.dll!Unknown+0x56
    00007FC994FF5AF0 00007FCFCD621735 System.Drawing.Common.dll!Unknown+0x95
    00007FC994FF5B50 00007FCFCD61EADA Acme.App.dll!Unknown+0x9ca
    00007FC994FF5F30 00007FCFC5D88ACF System.Private.CoreLib.dll!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+0x1f
    00007FC996FF9F40 00007FCFC5DCBEB7 System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)+0x57
    00007FC996FF9F60 00007FCFCD5B8CD3 Acme.App.dll!Unknown+0x343
    00007FC9F3FFBAF0 00007FCFCD621735 System.Drawing.Common.dll!Unknown+0x95
    00007FC9F3FFBB50 00007FCFCD61EADA Acme.App.dll!Unknown+0x9ca
    00007FC996FFA040 00007FCFC5D88ACF System.Private.CoreLib.dll!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+0x1f
    00007FC9F3FFBF30 00007FCFC5D88ACF System.Private.CoreLib.dll!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+0x1f
0

There are 0 best solutions below