dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

"Cannot access a disposed object" when reading large JSON from Web #9262

Open DelphinRP opened 2 weeks ago

DelphinRP commented 2 weeks ago

Description

Hello everyone,

we are deserializing a large JSON reply from our web server. The source code has been generated with NSwag.CodeGeneration.CSharp and used to work fine with WPF, .NET MAUI iOS and .NET MAUI Android. Since a few weeks ago, the functionality has stopped working with .NET MAUI Android and sporadically throws ObjectDisposedException exceptions. Both WPF and iOS work fine.

We have not found out why and how this occurs. The only big change we had in the last few weeks was updating Visual Studio and .NET MAUI workloads.

EDIT: The error does not occur in .NET MAUI 8.0.61

Steps to Reproduce

    try
    {
        using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
        using (var streamReader = new System.IO.StreamReader(responseStream))
        using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(streamReader))
        {
            var serializer = Newtonsoft.Json.JsonSerializer.Create(JsonSerializerSettings);
            var typedBody = serializer.Deserialize<T>(jsonTextReader);
            return new ObjectResponseResult<T>(typedBody, string.Empty);
        }
    }
    catch (Newtonsoft.Json.JsonException exception)
    {
        var message = "Could not deserialize the response body stream as " + typeof(T).FullName + ".";
        throw new ApiException(message, (int)response.StatusCode, string.Empty, headers, exception);
    }

The exception occurs on return new ObjectResponseResult<T>(typedBody, string.Empty);.

Link to public reproduction project repository

No response

Version with bug

8.0.72

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.61

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

Unfortunately, we could not find any workaround.

Relevant log output

 dotnet workload list

Installed Workload Id      Manifest Version       Installation Source
---------------------------------------------------------------------------------
android                    34.0.113/8.0.100       SDK 8.0.400, VS 17.11.35219.272
aspire                     8.1.0/8.0.100          SDK 8.0.400, VS 17.11.35219.272
ios                        17.5.8020/8.0.100      SDK 8.0.400, VS 17.11.35219.272
maccatalyst                17.5.8020/8.0.100      SDK 8.0.400, VS 17.11.35219.272
maui-windows               8.0.72/8.0.100         SDK 8.0.400, VS 17.11.35219.272
System.ObjectDisposedException: Cannot access a disposed object.

 Object name: 'Java.IO.InputStreamInvoker'.
    at Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable self) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.cs:line 153
    at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 57
    at Java.IO.InputStream.Close() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Java.IO.InputStream.cs:line 116
    at Android.Runtime.InputStreamInvoker.Close() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/InputStreamInvoker.cs:line 62
    at System.IO.Stream.Dispose()
    at System.IO.BufferedStream.Dispose(Boolean disposing)
    at System.IO.Stream.Close()
    at System.IO.Stream.Dispose()
    at System.IO.DelegatingStream.Dispose(Boolean disposing)
    at System.IO.Stream.Close()
    at System.IO.StreamReader.Dispose(Boolean disposing)
    at System.IO.StreamReader.Close()
    at Newtonsoft.Json.JsonTextReader.Close()
    at Newtonsoft.Json.JsonReader.Dispose(Boolean disposing)
    at Newtonsoft.Json.JsonReader.System.IDisposable.Dispose()
    at PSServerClients.Api.V5_0.Client.<ReadObjectResponseAsync>d__87`1[[System.Collections.Generic.List`1[[PSServerClients.Api.V5_0.ShortProject, PSServerClients, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() in /builds/software/profisignal-20/psserver-clients/PSServerClients/Api/V5_0/Client.cs:line 7102
github-actions[bot] commented 2 weeks ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

DelphinRP commented 2 weeks ago

We have just been able to verify that the exception does not occur in .NET MAUI 8.0.61!

The following makes our app work in Android again:

dotnet workload install maui --from-rollback-file https://maui.blob.core.windows.net/metadata/rollbacks/8.0.61.json

dotnet workload list

Installierte Workload-ID      Manifestversion        Installationsquelle
------------------------------------------------------------------------------------
android                       34.0.79/8.0.100        SDK 8.0.400, VS 17.11.35222.181
aspire                        8.1.0/8.0.100          SDK 8.0.400, VS 17.11.35222.181
ios                           17.2.8022/8.0.100      SDK 8.0.400, VS 17.11.35222.181
maccatalyst                   17.2.8022/8.0.100      SDK 8.0.400, VS 17.11.35222.181
maui                          8.0.61/8.0.100         SDK 8.0.400
maui-windows                  8.0.61/8.0.100         SDK 8.0.400, VS 17.11.35222.181

This is a major show stopper for our next release in october...

mikebaumannch commented 2 weeks ago

We are facing the same issue in our application. This issue is a blocker for our upcoming release as well.

During my analysis I saw that within the .NET MAUI SR7 there was a tiny PR merged into it: Bump to Android 34.0.114

The bump of the .NET Android pulled the following into it: https://github.com/dotnet/android/pull/8764

In my point of view this PR looks very suspicious 👀

There is an issue about that in the dotnet/android repo: https://github.com/dotnet/android/issues/9039