Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliding async and await on HttpClient is not throwing exception on OSX

After reading Stephen Cleary blog post about eliding async and await I've decided to go and play around with it. I wrote very simple console app with HttpClient using Visual Studio For Mac.

public static async Task Main(string[] args)
{
    Console.WriteLine(await Get());
    Console.WriteLine("Hello World!");
}

public static Task<string> Get()
{
    using (var http = new HttpClient())
        return http.GetStringAsync("http://google.com");
}

According to blog post it should throw an exception but it didn't. If I switch to Windows and try to run this app, I will get TaskCancelledException as expected, but on macOS it works perfectly fine.

Proof that Google.com was printed into console without exception on macOS

As I believe the reason behind this behaviour is different implementation of IDisposable in HttpClient on both platforms, but... why?

like image 834
Dominik Przywara Avatar asked Apr 09 '18 21:04

Dominik Przywara


1 Answers

Comparing mono repository with dotnet repository I've found, that mono implementation of HttpClient do not call CancellationTokenSource.Cancel() in Dispose method and I believe that's the reason of inconsistency between platforms.

like image 105
Dominik Przywara Avatar answered Oct 06 '22 19:10

Dominik Przywara