Skip to content

Exceptions in Fire&Forget tasks can be missed by our tests #46808

Open
@adamsitnik

Description

@adamsitnik

#46774 has shown, that there are some Fire&Forget tasks that can throw an unnoticed exception when we introduce a bug in BCL (see #46807 (comment) for details)

This is of course very rare as we try to avoid Fire&Forget tasks, but anyway it is possible (at least for the MemoryCache tests).

I wonder whether there is anything that we have done in the past to detect such issues?

If not, should we do something about it? Like for example extending xunit test runner to report a failure on TaskScheduler.UnobservedTaskException event? (I don't know if it's currently possible, it's just a hypothetical example to start a discussion)

[Fact]
public void ThrowingFireAndForgetThatPasses()
{
    Schedule(new object());

    static void Schedule(object obj)
    {
        Task.Factory.StartNew(state =>
        {
            string text = (string)state; // fails
            Console.WriteLine(text);
        }, obj, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
    }
}

@stephentoub @eerhardt

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions