Abort the current process from an asynchronous task

I have some complex functions that run in the async Task , spawned via TPL, which can sometimes occur unexpectedly. If such a malfunction occurs, this indicates a programming error that could lead to damage to the state of the program. Therefore, I do not want my program to detect an exception, handle it and "limp"; I want my process to crash and terminate.

I also want the thing to die in such a way that the Windows error reporting system detects it as a failure and does all the useful debugging things like grabbing a mini-disk, sending it to Microsoft, etc.

I understand that this may contradict your opinions about what programs should be done in the face of errors, but that is not the issue.

The problem that I have is that an exception arises from the task, this does not immediately lead to a process failure. After some time, it crashes when the garbage collector in his wisdom decides to collect an "unobserved" exception.

I want the process to work immediately because ...

  • The call column and the stream dump from the actual error is what I want to collect in the crash dump
  • The process of "chrome plating" and the failure of some indefinite time later can lead to further damage, since it will work with a possible damaged software state
  • Users are confused about which operation actually caused the failure.

So, in short, the question is:

How can I crash my process from async Task created using TPL so that Windows Error Reporting can create a useful mini-drive?

Thanks in advance!

+4
source share
2 answers

You can try this or something similar:

 public static Task FailFastOnException(this Task task) { task.ContinueWith(c => Environment.FailFast("Task faulted", c.Exception), TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.DetachedFromParent); return task; } 

and then:

 var t = Task.Factory.StartNew(…).FailFastOnException(); 

We just used it with the “fire and oblivion” tasks that we want to remove if for some reason they fail.

Taken from a blog post written by Stephen Tub: http://blogs.msdn.com/b/pfxteam/archive/2009/05/31/9674669.aspx

+4
source

See ThrowUnobservedTaskExceptions ( app.config ):

 <configuration> <runtime> <ThrowUnobservedTaskExceptions enabled="true"/> </runtime> </configuration> 

If the exception associated with the Task is not observed, there is no wait operation, the parent is not connected, and the System.Threading.Tasks.Task.Exception Property was not read by the task, the exception is considered unobservable.

In the .NET Framework 4 default, if the Task with the inconspicuous exception is garbage collected, the finalizer throws an exception and finishes processing. The termination of the process is determined by the timing of the collection and collection of garbage.

To make it easier for developers to write task-based asynchronous code, the .NET Framework 4.5 changes this default behavior for invisible exceptions. undetected exceptions still raise the UnobservedTaskException event, but the process does not terminate by default. Instead, the exception is ignored after the event occurs, regardless of whether the event occurred, the handler observes the exception.

In the .NET Framework 4.5, you can use an element in the application's configuration file to enable the behavior of the .NET Framework 4 browser exception.

You can also specify exception behavior in one of the following ways:

  • By setting the environment variable COMPlus_ThrowUnobservedTaskExceptions (set COMPlus_ThrowUnobservedTaskExceptions = 1).

  • By setting the registry DWORD value ThrowUnobservedTaskExceptions = 1 in HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft.NETFramework.

0
source

Source: https://habr.com/ru/post/1445584/


All Articles