Exception code 0xe0434352 that crashes w3wp.exe | dotnet run

-1

I find out that an error on my server was crashing the server and not being handled by global exception handler. I am not totally sure that what I will describe is the same problem that I am having (using 3rd party libs, plus custom logic in the middle... is difficult to analyze the full code), although I suspect that is the same.

  • Can anyone explain me why the global exception handler cannot handle exceptions raised after start writing the response?
  • And if is something we can do about this?

I am using ASP.Net core 3.0

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
        app.UseExceptionHandler(options => options.Run(HandleException));

        app.Map("/map1", HandleMapTest1);
        app.Map("/map2", HandleMapTest2);
    }

    private Task HandleException(HttpContext context) {
        try
        {
            var exceptionFeature = context.Features.Get<IExceptionHandlerPathFeature>();
            Console.WriteLine("Unhandled exception");
        }
        catch { /*ignore*/ }
        return Task.CompletedTask;
    }

    private static void HandleMapTest1(IApplicationBuilder app) {
        app.Run(async context =>
        {
            throw new System.Exception("It will be caught by HandleException");
            await context.Response.WriteAsync("Map Test 1");
        });
    }

    private static void HandleMapTest2(IApplicationBuilder app) {
        app.Run(async context =>
        {
            var x = new MyClass();
            x.DoDSomething();
        });
    }
}

class MyListener
{
    public void Check(Action<int> action) {
        action(1);
    }
}

class MyClass
{
    public MyListener DoDSomething() {
        var x = new MyListener();
        x.Check(EnterFieldNode);
        return x;

        async void EnterFieldNode(int num) => await Validate();
    }

    private async Task Validate() {
        // do something that explodes
        throw new Exception();
    }
}

I have found out this article, that uses the UnhandledException event from the AppDomain to catch the exception, although is not possible (at least in a straightforward way) to avoid the server crash.


UPDATE 1: Example updated. Finally was able to reproduce. The code is the most similar possible to what is happening with a 3rd party library that I am using. It crashes the w3p process and I got a error in event viewer with Exception code 0xe0434352.

c#
asp.net-core
asked on Stack Overflow Nov 7, 2019 by user1845593 • edited Nov 8, 2019 by user1845593

1 Answer

0

As @KirkLarkin mentioned above in the comments, the async void is the problem. Here's some links with more details:

Async Guidance

TaskScheduler.UnobservedTaskException Event

answered on Stack Overflow Nov 8, 2019 by user1845593

User contributions licensed under CC BY-SA 3.0