Skip to content

Not clear when the return task of an async function is set to a cancelled state #775

Closed
@pkulikov

Description

@pkulikov

13.10.6 The throw statement section contains the following sentence:

If the current function is async and task-returning, the exception is recorded in the return task, which is put into a faulted or cancelled state as described in §15.15.2.

The linked 15.15.2 section contains only the following sentence relevant to the quoted sentence from 13.10.6:

If the function body terminates as the result of an uncaught exception (§13.10.6) the exception is recorded in the return task which is put into a faulted state.

When I read the sentence in 13.10.6, I expect that 15.15.2 explains the two things

  • when the task is put into a faulted state and
  • when the task is put into a cancelled state.

The current text of 15.15.2 doesn't explain that difference. Even more, it doesn't mention the cancelled state at all. So, one may think that the return task is always put into a faulted state, if an exception is thrown. That is not true, as the following example shows:

using System;
using System.Threading.Tasks;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Task t = Async();
        try
        {
            await t;
        }
        catch
        {
            Console.WriteLine(t.IsFaulted);  // False
            Console.WriteLine(t.IsCanceled); // True
        }
    }

    public static async Task Async() => throw new OperationCanceledException();
}

So, 15.15.2 misses the description of when the return task is set to a cancelled state.


Associated WorkItem - 187389

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions