Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DelphinRP opened this issue Aug 27, 2024 · 3 comments
Open

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

DelphinRP opened this issue Aug 27, 2024 · 3 comments
Assignees
Labels
Area: HTTP Issues with sockets / HttpClient.

Comments

@DelphinRP
Copy link

DelphinRP commented Aug 27, 2024

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
Copy link

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
Copy link
Author

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
Copy link

mikebaumannch commented Aug 28, 2024

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:
#8764

In my point of view this PR looks very suspicious 👀

There is an issue about that in the dotnet/android repo: #9039

@PureWeen PureWeen transferred this issue from dotnet/maui Aug 29, 2024
@dotnet-policy-service dotnet-policy-service bot added the needs-triage Issues that need to be assigned. label Aug 29, 2024
@jpobst jpobst added Area: HTTP Issues with sockets / HttpClient. and removed needs-triage Issues that need to be assigned. labels Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: HTTP Issues with sockets / HttpClient.
Projects
None yet
Development

No branches or pull requests

4 participants