alexrainman / ModernHttpClient

ModernHttpClient
MIT License
126 stars 27 forks source link

ReadAsync/SendAsync app crash #73

Open stesvis opened 3 years ago

stesvis commented 3 years ago

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.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: image

I hope that someone may help me figure this out, thank you.

alexrainman commented 3 years ago

The max timeout you can set is 30 seconds (by platform specific limitations). Can you provide a reproduction demo?

stesvis commented 3 years ago

After further testing i was able to simulate poor internet connection and reproduce this exception. I think it's just related to weak internet.

petro2050 commented 3 years ago

I think the implementation of #72 can help with this.