Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DebuggerStepThrough, DebuggerHidden don't work in an async-await method

When you turn on the "Break when an exception is thrown" feature in the Visual Studio debugger it breaks everywhere for selected exception types. The way to tell it not to break in a specific method is to decorate these methods with DebuggerStepThrough attribute (or DebuggerHidden).

This, evidently, doesn't work for an async method for some reason. Here's a snippet that reproduces the issue. The debugger will break inside the TestAsync even though it's marked with the attributes and it will not break inside Test as excepted ( the only difference between them is the first is marked with the async keyword):

public class Attributes
{
    public async Task Run()
    {
        await TestAsync();
        await Test();
    }

    [DebuggerHidden]
    [DebuggerStepThrough]
    public async Task TestAsync()
    {
        try
        {
            throw new Exception("Async");
        }
        catch
        {
        }
        await Task.Delay(100);
    }

    [DebuggerHidden]
    [DebuggerStepThrough]
    public Task Test()
    {
        try
        {
            throw new Exception("sync");
        }
        catch
        {
        }
        return Task.Delay(100);
    }
}

So, is this behavior intended? Is it a bug? Is there a workaround?

like image 352
i3arnon Avatar asked Jun 26 '14 14:06

i3arnon


1 Answers

Attributes don't play well with async/await since async methods get re-written under the covers--and the attributes do not follow. See https://stackoverflow.com/a/22412597/495262 for a similar situation.

like image 60
Matt Smith Avatar answered Nov 04 '22 12:11

Matt Smith