xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 505 forks source link

AppInfo.Version throws exception for disposed object on Android 11 and onwards #1959

Open joshardt opened 2 years ago

joshardt commented 2 years ago

I use Xamarin.Essentials version 1.7.0 and sometimes experience 2 similar exceptions with the Xamarin.Essentials.AppInfo.Version API on Android 11 and Android 12. Android 8, 9 and 10 and iOS 15 do not cause problems and are not effected.

Sometimes I get this System.ObjectDisposedException:

{System.ObjectDisposedException: Cannot access a disposed object.
    Object name: 'Android.Content.PM.PackageInfo'.
      at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00029] in <00c315a988634383b446eff646084784>:0 
      at Java.Interop.JniPeerMembers+JniInstanceFields.GetObjectValue (System.String encodedMember, Java.Interop.IJavaPeerable self) [0x00000] in <00c315a988634383b446eff646084784>:0 
      at Android.Content.PM.PackageInfo.get_VersionName () [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Android.Content.PM.PackageInfo.cs:604 
      at Xamarin.Essentials.AppInfo.PlatformGetVersionString () [0x00021] in D:\a\1\s\Xamarin.Essentials\AppInfo\AppInfo.android.cs:31 
      at Xamarin.Essentials.AppInfo.get_VersionString () [0x00000] in D:\a\1\s\Xamarin.Essentials\AppInfo\AppInfo.shared.cs:11 
      at Xamarin.Essentials.AppInfo.get_Version () [0x00000] in D:\a\1\s\Xamarin.Essentials\AppInfo\AppInfo.shared.cs:13 
      at Xamarin.Essentials.Implementation.AppInfoImplementation.Xamarin.Essentials.Interfaces.IAppInfo.get_Version () [0x00000] in <ae253c3e74964fd096e3b69bb652107d>:0 
      at XX.Libraries.Foundation.Platform.Locales.Services.AppVersionComparerService.IsInstalledAppVersionBetween (System.String minFeatureVersion, System.String maxFeatureVersionExclusive) [0x00004] in /XX/Foundation/src/Platform/Locales/Services/AppVersionComparerService.cs:26 }
        base: {System.InvalidOperationException}

But most of the time I get this System.NotImplementedException:

[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NotImplementedException: Do not know how to dispose: Invalid.
[mono-rt] at Xamarin.Essentials.AppInfo.PlatformGetVersionString () [0x0002a] in D:\a\1\s\Xamarin.Essent01-25 16:29:14.002 F/mono-rt (26285): at Java.Interop.JniObjectReference.Dispose (Java.Interop.JniObjectReference& reference) [0x00084] in <00c315a988634383b446eff646084784>:0
[mono-rt] at Java.Interop.JniRuntime+JniValueManager.DisposePeer (Java.Interop.JniObjectReference h, Java.Interop.IJavaPeerable value) [0x00091] in <00c315a988634383b446eff646084784>:0
[mono-rt] at Java.Interop.JniRuntime+JniValueManager.DisposePeer (Java.Interop.IJavaPeerable value) [0x00056] in <00c315a988634383b446eff646084784>:0
[mono-rt] at Java.Lang.Object.Dispose () [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:210
[mono-rt] at XX.Modules.Domestic.PendingAppWorkFlows.PendingAppWorkFlowService+<>c__DisplayClass7_0.<GatherPendingActions>b__2 (XX.Modules.Domestic.Contracts.PendingAppWorkFlows.IPendingAppWorkFlowContract pendingAppWorkFlowContract, Sials\AppInfo\AppInfo.android.cs:31
[mono-rt] at Xamarin.Essentials.AppInfo.get_VersionString () [0x00000] in D:\a\1\s\Xamarin.Essentials\AppInfo\AppInfo.shared.cs:11
[mono-rt] at Xamarin.Essentials.AppInfo.get_Version () [0x00000] in D:\a\1\s\Xamarin.Essentials\AppInfo\AppInfo.shared.cs:13
[mono-rt] at Xamarin.Essentials.Implementation.AppInfoImplementation.Xamarin.Essentials.Interfaces.IAppInfo.get_Version () [0x00000] in <ae253c3e74964fd096e3b69bb652107d>:0
[mono-rt] at XX.Modules.CPA.WhatsNew.PendingAppWorkflows.WhatsNewPendingAppWorkflow.GetPendingWorkflowAsync () [0x00011] in <aedab81df272457b957d5b391f43e1d1>:0
[mono-rt] at XX.Modules.Domestic.PendingAppWorkFlows.PendingAppWorkFlowService+<>c__DisplayClass7_0.<GatherPendingActions>b__2 (XX.Modules.Domestic.Contracts.PendingAppWorkFlows.IPendingAppWorkFlowContract pendingAppWorkFlowContract, S01-25 16:29:14.002 F/mono-rt (26285): at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in /Users/builder/jenkins/workspace/archive-mono/2020ystem.Threading.Tasks.ParallelLoopState state, System.Int64 index) [0x0006a] in <7f236d075eb341819d6c6a57150cded5>:0
[mono-rt] at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_1 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1037
[mono-rt] at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs01-25 16:29:14.002 F/mono-rt (26285): at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context (System.Object state) [0x0000d] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1370
[mono-rt] at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:968
[mono-rt] at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:910
[mono-rt] at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00021] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1341
[mono-rt] at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:899
[mono-rt] at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1261

Tracing down the StackTrace I found these lines that are causing the problem: https://github.com/xamarin/Essentials/blob/2627bf2db0c009392e6558f31b9844fa2d4185fd/Xamarin.Essentials/AppInfo/AppInfo.android.cs#L29-L32

This using statement disposes everything what is inside of the round brackets right after the using keyword. I don't know why one of these exceptions is thrown but would hope that someone with native Android experience can find out what the problem is and fix it.

joshardt commented 2 years ago

After further investigations it looks like in the app we are calling the Xamarin.Essentials.AppInfo.Version API in parallel. The API is not thread safe I guess.