ModernHttpClient
ModernHttpClient copied to clipboard
ReadAsync/SendAsync app crash
Hello,
I have many crash reports in AppCenter that are related to the GetAsync() and SendAsync() methods.
ModernHttpClient ByteArrayListStream.ReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancellationToken) ModernHttpClient ProgressStreamContent+ProgressStream.ReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancellationToken) System.IO Stream.CopyToAsyncInternal (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken) System.Net.Http StreamToStreamCopy+<>c.<DisposeSourceWhenCompleteAsync>b__1_0 (System.Threading.Tasks.Task completed, System.Object innerSource) System.Threading.Tasks ContinuationTaskFromTask.InnerInvoke () System.Threading.Tasks Task.Execute () System.Net.Http HttpContent.LoadIntoBufferAsyncCore (System.Threading.Tasks.Task serializeToStreamTask, System.IO.MemoryStream tempBuffer) System.Net.Http HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts)
I have the relevant code lines inside a try/catch, but still it causes a crash (sometimes, when the connection times out i think):
try
{
// ...
var response = await WebApiClient.GetAsync(url);
var responseJson = await response.Content.ReadAsStringAsync();
// ...
}
catch (Exception ex)
{
// ... handle it
}
Where WebApiClient is a global HttpClient instance:
public static HttpClient WebApiClient = new HttpClient(new NativeMessageHandler(false, new TLSConfig()))
{
Timeout = TimeSpan.FromSeconds(45),
BaseAddress = new Uri(Misc.Constants.ApiUrlsBase.BaseUrl),
});
Why would it make the app crash even tho i am using try/catch? Like I said, the error is related to timeouts:

I hope that someone may help me figure this out, thank you.
The max timeout you can set is 30 seconds (by platform specific limitations). Can you provide a reproduction demo?
After further testing i was able to simulate poor internet connection and reproduce this exception. I think it's just related to weak internet.
I think the implementation of #72 can help with this.