Bug 23594 - A task will not complete if an exception is thrown in an attached child task with a task continuation that specifies TaskContinuationOptions.NotOnFaulted
Summary: A task will not complete if an exception is thrown in an attached child task ...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: Other Other
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-10-03 14:56 UTC by Dan Rigby
Modified: 2014-10-15 11:11 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
A zip file containing a solution with a console application, Xamarin.Android application, and a WPF application that demonstrates this bug. (27.40 KB, application/x-zip-compressed)
2014-10-03 14:58 UTC, Dan Rigby
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Dan Rigby 2014-10-03 14:56:00 UTC
In mono, a task will not complete if an exception is thrown in an attached child task with a task continuation that specifies TaskContinuationOptions.NotOnFaulted.

Specifically, the Console.WriteLine at the bottom of this code block will never be run as the parent task never completes:

Task.Factory.StartNew(() => {
    Task.Run(() => {
        throw new Exception();
    })
    .ContinueWith(task => {
        // Do Nothing
    }, TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.NotOnFaulted);

}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default)
.ContinueWith(task => {
    Console.WriteLine("This code will never run");
});

However, if the TaskContinuationOptions.NotOnFaulted is removed, the Console.WriteLine will be executed:

Task.Factory.StartNew(() => {
    Task.Run(() => {
        throw new Exception();
    })
    .ContinueWith(task => {
        // Do Nothing
    }, TaskContinuationOptions.AttachedToParent);

}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default)
.ContinueWith(task => {
    Console.WriteLine("This code will never run");
});

I have confirmed that on Windows using .NET (4.5.2), the code above works as expected (the parent task completes and the Console.WriteLine code is excuted).

I am have confirmed that this bug exists in the current stable channel of Xamarin.Android (4.16.0.17) and in Mono for Linux (CentOS) 3.2.3.

I have attached a zip file containing a solution with a console application, Xamarin.Android application, and a WPF application that demonstrates this bug.
Comment 1 Dan Rigby 2014-10-03 14:58:23 UTC
Created attachment 8301 [details]
A zip file containing a solution with a console application, Xamarin.Android application, and a WPF application that demonstrates this bug.
Comment 2 Dan Rigby 2014-10-03 15:00:45 UTC
The Console.WriteLine method is in second example above should actually read:

Console.WriteLine("This *will* run");
Comment 3 Marek Safar 2014-10-05 14:38:54 UTC
Fixed in master
Comment 4 Dan Rigby 2014-10-15 11:11:12 UTC
Adding commit reference to this bug so it can be found easily later:

"[corlib] Notify parent task when child task uses NotOnFaulted. Fixes #23594"
https://github.com/mono/mono/commit/f39cc88e58d01d22ec5e220e04560eedc110b3b3