ModernHttpClient icon indicating copy to clipboard operation
ModernHttpClient copied to clipboard

ReadAsync/SendAsync app crash

Open stesvis opened this issue 5 years ago • 3 comments
trafficstars

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

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

stesvis avatar Sep 17 '20 20:09 stesvis

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

alexrainman avatar Oct 07 '20 12:10 alexrainman

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

stesvis avatar Oct 07 '20 14:10 stesvis

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

petro2050 avatar Dec 28 '20 20:12 petro2050