I am using .net HTTPClient across multiple multithreaded consumers making GetAsync web requests to a local service at 127.0.0.1 once per second.
The web requests complete 99.9% of the time but occasionally a few requests (over a 3-4 hour period) will get stuck in the GetAsyc and will not complete or timeout. Requests to the same service url/port in the same time period will work fine and new requests will complete fine.
The GetAsync is being fired off in a fire and forget mode where a callback is called on completion to handle the resulting parsed data (as it is integrated with some older code that doesn't use async.)
public void Execute(Action<IAsyncCommand> onCompletion)
{
this.onAsyncCompletion = onCompletion;
try
{
// do not await as this is fire and forget
this.HandlRequestAysnc(this.Target, new StringContent(this.CommandPayload));
return;
}
catch(Exception e)
{
//log exception
}
}
private async Task HandlRequestAysnc(Uri uri, StringContent stringContent)
{
try
{
ConfiguredTaskAwaitable<HttpResponseMessage> request = stringContent != null ? webClient.PostAsync(uri, stringContent).ConfigureAwait(false) : webClient.GetAsync(uri).ConfigureAwait(false);
//this will never return or timeout 1 in 10000 times
using (HttpResponseMessage response = await request)
{
if (response.IsSuccessStatusCode)
{
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
//handle result
}
}
else
{
//handle failure
}
}
}
catch (Exception ex)
{
//log exception
}
if (this.onAsyncCompletion != null)
{
this.onAsyncCompletion(this);
}
}