Closed Flash3001 closed 4 years ago
Thanks for your feedback. We will look into the issue as soon as possible.
@Flash3001 This looks like a Mono issue, can you attach a simple repro which triggers the behavior? Thanks!
I suppose the code and situation here is similar to https://github.com/xamarin/xamarin-android/issues/4695, if this is so @Flash3001, please refer to https://github.com/xamarin/xamarin-android/issues/4695#issuecomment-630644846 for a possible solution. If not, please reopen this issue and attach the requested information, thanks!
Steps to Reproduce
Expected Behavior
A HttpRequestException should be thrown.
Actual Behavior
A Android.OS.NetworkOnMainThreadException is thrown.
Version Information
Mono Framework MDK Runtime: Mono 6.8.0.123 (2019-10/1d0d939dc30) (64-bit) Package version: 608000123
Xamarin.Android Version: 10.2.0.100 (Visual Studio Community) Commit: xamarin-android/d16-5/988c811
Log File
Sample of of a stack trace:
More info
Our fix for that is just to make sure that code is running on another thread.
In the version of HttpResponseMessage that is being run the EnsureSuccessStatusCode method is calling Dispose() on its _content stream. Which down the line tries to read more content: https://github.com/duzechao/OKHttpUtils/blob/1c1ee3667737c67d0ff523658bb9acad769030f7/okhttp/src/main/java/okhttp3/internal/http/Http1xStream.java#L458-L465
Trying to read the code for HttpResponseMessage.cs caused some confusion was I don't know which on is really in use.
When I look at Mono repository with the tag 6.8.0.123 the Dispose is not being called. https://github.com/mono/mono/blob/1d0d939dc30a5b56f478bc9f097cab146276b9af/mcs/class/System.Net.Http/System.Net.Http/HttpResponseMessage.cs#L118-L124
But when I disassemble the code on System.Net.Http.dll it is there:
Resolved from:
This issue started to appear for us not long ago so we were trying to check HttpResponseMessage code to know if there was regression in there where the
Dispose()
started to be called recently or if it is just hiding another issue on our side - which we can now investigate by avoiding Android.OS.NetworkOnMainThreadException from being thrown.