Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.HttpClient: SendAsync failed with OperationCanceledException without http request throught network

We use System.Net.Http.HttpClientfor calls between microservices inside k8s.

  • OS Linux(inside docker)
  • dotnet TargetFramework: netcoreapp2.1
  • server: Kestrel
  • protocol: http

Few days ago we noticed very strange behaviour of http calls: some calls between microservice(near 2-3%) failed with error

System.OperationCanceledException: The operation was canceled.
   at System.Net.Http.HttpClient.HandleFinishSendAsyncError(Exception e, CancellationTokenSource cts)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at OurCode.HttpServiceClient.GetStringResponseAsync(String methodUri, HttpMethod httpMethod)
   ...another our code...

after our timeout for http calls(it is 3 sec). But there was no logs about call inside callee service.

We enabled packetbeat for tracing http requests and also noticed, that no any http requests from caller service to callee service was executed. CPU, memory and network for this services was OK all the time.

Simplified version of our code for http calls looks like:

public async Task<string> GetStringResponseAsync(String methodUri,  HttpMethod httpMethod)
{
    int timeoutInMilliseconds = 3000;
    var tokenSource = new CancellationTokenSource();
    var rm = new HttpRequestMessage(httpMethod, methodUri);
    Task<HttpResponseMessage> httpTask = HttpClient.SendAsync(rm, tokenSource.Token);
    tokenSource.CancelAfter(timeoutInMilliseconds);
    HttpResponseMessage response = await httpTask;
    await EnsureSuccessStatusCode(response);
    return await response.Content.ReadAsStringAsync();
}

Any ideas about what problem can cause this strange behaviour without http request through network, and what can I do for further investigation?

like image 254
Frank59 Avatar asked Dec 10 '18 17:12

Frank59


People also ask

What is HttpClient Sendasync?

PostAsync Method (System. Net. Http) Send a POST request to the specified Uri as an asynchronous operation.

What is HttpClient in VB net?

HttpClient is a more advanced class, and was added later, than other classes like WebClient. This class is built for asynchronous use. For downloading web pages, it is better to enable and support GZIP compression. Another header can be added to HttpClient. We can use VB.NET to decompress these files.


1 Answers

It just meant that the web service did not respond.

HttpClient throws a TaskCanceledException, (which inherits from OperationCanceledException) when the timeout elapses. It is not intuitive and doesn't make any sense to me (and others), but that's what it does unfortunately.

There is some discussion about it here (a few people mentioned some workarounds to distinguish timeout from a true cancel, if you care).

like image 186
Gabriel Luci Avatar answered Oct 12 '22 22:10

Gabriel Luci