Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4596492
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:07:21+00:00 2026-05-21T23:07:21+00:00

We’ve hit a snag mixing Tasks with our top-level crash handler and are trying

  • 0

We’ve hit a snag mixing Tasks with our top-level crash handler and are trying to find a workaround. I’m hoping someone has some ideas.

Our tools have a top-level crash handler (from the AppDomain’s UnhandledException event) that we use to file bug reports with minidumps. It works wonderfully. Unfortunately, Tasks throw a wrench in this.

We just started using 4.0 Tasks and discovered that internally in the Task action execution code, there is a try/catch which grabs the exception and stores it for passing down the task chain. Unfortunately, the existence of the catch (Exception) unwinds the stack and when we create the minidump the call site is lost. That means we have none of the local variables at the time of the crash, or they have been collected, etc.

Exception filters seem to be the right tool for this job. We could wrap some Task action code in a filter via an extension method, and on an exception getting thrown call our crash handler code to report the bug with the minidump. However, we don’t want to do this on every exception, because there may be a try-catch in our own code that is ignoring specific exceptions. We only want to do the crash report if the catch in Task was going to handle it.

Is there any way to walk up the chain of try/catch handlers? I think if I could do this, I could walk upwards looking for catches until hitting Task, and then firing the crash handler if true.

(This seems like a long shot but I figured I’d ask anyway.)

Or if anyone has any better ideas, I’d love to hear them!

UPDATE

I created a small sample program that demonstrates the problem. My apologies, I tried to make it as short as possible but it’s still big. :/

In the below example, you can #define USETASK or #define USEWORKITEM (or none) to test out one of the three options.

In the non-async case and USEWORKITEM case, the minidump that is generated is built at the call site, exactly how we need. I can load it in VS and (after some browsing to find the right thread), I see that I have the snapshot taken at the call site. Awesome.

In the USETASK case, the snapshot gets taken from within the finalizer thread, which is cleaning up the Task. This is long after the exception has been thrown and so grabbing a minidump at this point is useless. I can do a Wait() on the task to make the exception get handled sooner, or I can access its Exception directly, or I can create the minidump from within a wrapper around TestCrash itself, but all of those still have the same problem: it’s too late because the stack has been unwound to one catch or another.

Note that I deliberately put a try/catch in TestCrash to demonstrate how we want some exceptions to be processed normally, and others to be caught. The USEWORKITEM and non-async cases work exactly how we need. Tasks almost do it right! If I could somehow use an exception filter that lets me walk up the chain of try/catch (without actually unwinding) until I hit the catch inside of Task, I could do the necessary tests myself to see if I need to run the crash handler or not. Hence my original question.

Here’s the sample.

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        AppDomain.CurrentDomain.UnhandledException += (_, __) =>
            {
                using (var stream = File.Create(@"c:\temp\test.dmp"))
                {
                    var process = Process.GetCurrentProcess();
                    MiniDumpWriteDump(
                        process.Handle,
                        process.Id,
                        stream.SafeFileHandle.DangerousGetHandle(),
                        MiniDumpType.MiniDumpWithFullMemory,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        IntPtr.Zero);
                }
                Process.GetCurrentProcess().Kill();
            };
        TaskScheduler.UnobservedTaskException += (_, __) =>
            Debug.WriteLine("If this is called, the call site has already been lost!");

        // must be in separate func to permit collecting the task
        RunTest();

        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    static void RunTest()
    {

#if USETASK
        var t = new Task(TestCrash);
        t.RunSynchronously();
#elif USEWORKITEM
        var done = false;
        ThreadPool.QueueUserWorkItem(_ => { TestCrash(); done = true; });
        while (!done) { }
#else
        TestCrash();
#endif
    }

    static void TestCrash()
    {
        try
        {
            new WebClient().DownloadData("http://filenoexist");
        }
        catch (WebException)
        {
            Debug.WriteLine("Caught a WebException!");
        }
        throw new InvalidOperationException("test");
    }

    enum MiniDumpType
    {
        //...
        MiniDumpWithFullMemory = 0x00000002,
        //...
    }

    [DllImport("Dbghelp.dll")]
    static extern bool MiniDumpWriteDump(
        IntPtr hProcess,
        int processId,
        IntPtr hFile,
        MiniDumpType dumpType,
        IntPtr exceptionParam,
        IntPtr userStreamParam,
        IntPtr callbackParam);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:07:21+00:00Added an answer on May 21, 2026 at 11:07 pm

    It sounds like if you can wrap your top-level tasks in an exception filter (written in VB.NET?) you would be able to do what you want. Since your filter would run just before the Task‘s own exception filter, it would only get invoked if nothing else within your task handles the exception but before Task gets ahold of it.

    Here’s a working sample. Create a VB library project called ExceptionFilter with this in a VB file:

    Imports System.IO
    Imports System.Diagnostics
    Imports System.Runtime.CompilerServices
    Imports System.Runtime.InteropServices
    
    Public Module ExceptionFilter
        Private Enum MINIDUMP_TYPE
            MiniDumpWithFullMemory = 2
        End Enum
    
        <DllImport("dbghelp.dll")>
        Private Function MiniDumpWriteDump(
                ByVal hProcess As IntPtr,
                ByVal ProcessId As Int32,
                ByVal hFile As IntPtr,
                ByVal DumpType As MINIDUMP_TYPE,
                ByVal ExceptionParam As IntPtr,
                ByVal UserStreamParam As IntPtr,
                ByVal CallackParam As IntPtr) As Boolean
        End Function
    
        Function FailFastFilter() As Boolean
            Dim proc = Process.GetCurrentProcess()
            Using stream As FileStream = File.Create("C:\temp\test.dmp")
                MiniDumpWriteDump(proc.Handle, proc.Id, stream.SafeFileHandle.DangerousGetHandle(),
                                  MINIDUMP_TYPE.MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)
            End Using
            proc.Kill()
            Return False
        End Function
    
        <Extension()>
        Public Function CrashFilter(ByVal task As Action) As Action
            Return Sub()
                       Try
                           task()
                       Catch ex As Exception When _
                           FailFastFilter()
                       End Try
                   End Sub
        End Function
    End Module
    

    Then create a C# project and add a reference to ExceptionFilter. Here’s the program I used:

    using System;
    using System.Diagnostics;
    using System.Net;
    using System.Threading.Tasks;
    using ExceptionFilter;
    
    class Program
    {
        static void Main()
        {
            new Task(new Action(TestCrash).CrashFilter()).RunSynchronously();
        }
    
        static void TestCrash()
        {
            try
            {
                new WebClient().DownloadData("http://filenoexist");
            }
            catch (WebException)
            {
                Debug.WriteLine("Caught a WebException!");
            }
            throw new InvalidOperationException("test");
        }
    }
    

    I ran the C# program, opened up the DMP file, and checked out the call stack. The TestCrash function was on the stack (a few frames up) with throw new as the current line.

    FYI, I think I would use Environment.FailFast() over your minidump/kill operation, but that might not work as well in your workflow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.