Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why task exception not rethrown on GC.Collect

Tags:

c#

c#-4.0

task

My understanding is that when a Task throws an exception, it's get stored and re-thrown when either one of (Result, WaitAll) property of Tasks is observed or when GC happened. Given that, I run following code.

Task t = Task.Factory.StartNew(() =>
{                    
    throw new Exception("Hello World");
});

for (int i = 0; i < 10000; i++)
{
    Console.WriteLine(i);
}    

 GC.Collect();

 for (int a = 20; a < 30; a++)
 {
      Console.WriteLine(a);
 }

But when I run the code above, I except an exception to be thrown at GC.Collect but it does not happen, rather it continues to print output from the second loop. Where is my understanding wrong here?

like image 434
imak Avatar asked Oct 14 '12 15:10

imak


People also ask

How do you handle exceptions thrown by tasks?

Exceptions are propagated when you use one of the static or instance Task. Wait methods, and you handle them by enclosing the call in a try / catch statement. If a task is the parent of attached child tasks, or if you are waiting on multiple tasks, multiple exceptions could be thrown.

What happens if a task throws an exception?

If Bar throws an exception, it will be thrown right at the point where you call it. However, if the Task that Bar returns wraps an exception, what happens depends on your version of . NET runtime - for .

How do you handle a task in C#?

A task in C# is used to implement Task-based Asynchronous Programming and was introduced with the . NET Framework 4. The Task object is typically executed asynchronously on a thread pool thread rather than synchronously on the main thread of the application.

What happens when a task throws an aggregateexception?

If a task has an attached child task that throws an exception, that exception is wrapped in an AggregateException before it is propagated to the parent task, which wraps that exception in its own AggregateException before it propagates it back to the calling thread.

What happens when an exception is thrown in a task?

When an exception is thrown in a task without call the Wait () or WaitAll () methods or the Result property for a Futur task), the task is actually garbaged by the finalizer thread. The unobserved exception is rethrown by the finalizer thread.

What does GC collect do in Python?

You shouldn't expect gc.collect () to do anything here. gc simply controls the cyclic garbage collector, which is an auxilliary garbage collector because CPython uses reference counting for its main memory management strategy. The cyclic garbage collector handles reference cycles, there are no reference cycles here so gc.collect won't do anything.

How do you recover from an exception in a task?

In a meaningful application, the continuation delegate could log detailed information about the exception and possibly spawn new tasks to recover from the exception. If a task faults, the following expressions throw the exception: Use a try-catch statement to handle and observe thrown exceptions.


1 Answers

In your example code, the Task object t is still in scope, so is not eligible for collection when you call GC.Collect().

Apart from that, the behaviour changed between .NET 4.0 and .NET 4.5:

In .NET 4.0, unobserved exceptions will throw an exception on the finalizer thread, causing the process to crash.

In .NET 4.5, this behaviour was changed so unobserved exceptions are ignored by default. There is a config switch you can set to turn the old, strict behaviour back on.

.NET 4.0: Tasks and Unhandled Exceptions

.NET 4.5: Task Exception Handling in .NET 4.5

like image 119
Nick Butler Avatar answered Oct 23 '22 03:10

Nick Butler