Skip to content

System.ExecutionEngineException #264

Closed

Description

HResult=0x80131506

>	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructureHelper(System.IntPtr ptr, System.Type structureType) Line 203	C#
 	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructure(System.IntPtr ptr, System.Type structureType) Line 560	C#
 	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructure<WinRT.Interop.IUnknownVftbl>(System.IntPtr ptr) Line 580	C#
 	WinRT.Runtime.dll!WinRT.ObjectReference<WinRT.Interop.IUnknownVftbl>.GetVtables(System.IntPtr thisPtr) Line 210	C#
 	WinRT.Runtime.dll!WinRT.ObjectReference<WinRT.Interop.IUnknownVftbl>.FromAbi(System.IntPtr thisPtr) Line 200	C#
 	WinRT.Runtime.dll!WinRT.MarshalInspectable.FromAbi(System.IntPtr ptr) Line 953	C#
 	WinRT.Runtime.dll!WinRT.MarshalInterface<Windows.Foundation.IAsyncOperation<System.Collections.Generic.IReadOnlyList<Windows.Storage.IStorageItem>>>.FromAbi(System.IntPtr ptr) Line 799	C#
 	Microsoft.Windows.SDK.NET.dll!ABI.Windows.Foundation.AsyncOperationCompletedHandler<System.Collections.Generic.IReadOnlyList<Windows.Storage.IStorageItem>>.Do_Abi_Invoke.AnonymousMethod__0(System.Delegate invoke)	Unknown
 	WinRT.Runtime.dll!WinRT.ComWrappersSupport.MarshalDelegateInvoke<System.Delegate>(System.IntPtr thisPtr, System.Action<System.Delegate> invoke) Line 62	C#
 	Microsoft.Windows.SDK.NET.dll!ABI.Windows.Foundation.AsyncOperationCompletedHandler<System.Collections.Generic.IReadOnlyList<Windows.Storage.IStorageItem>>.Do_Abi_Invoke(void* thisPtr, System.IntPtr asyncInfo, Windows.Foundation.AsyncStatus asyncStatus)	Unknown

Which happened with this source:

var proxyType = Microsoft.UI.Xaml.VerticalAlignment.Center;
var assembly = proxyType.GetType().GetTypeInfo().Assembly;

foreach (var typeInfo in assembly.**ExportedTypes**)
...

System.Private.CoreLib.dll!System.Reflection.RuntimeAssembly.GetExportedTypes() Line 192 C#

System.Private.CoreLib.dll!System.Reflection.Assembly.ExportedTypes.get() Line 72 C#
Microsoft.Toolkit.Uwp.SampleApp.dll!Microsoft.Toolkit.Uwp.SampleApp.Sample.LookForTypeByName(string typeName) Line 660 C#
Microsoft.Toolkit.Uwp.SampleApp.dll!Microsoft.Toolkit.Uwp.SampleApp.Sample.PreparePropertyDescriptorAsync() Line 572 C#

Activity

  1. jkoritzinsky commented on May 7, 2020

    @jkoritzinsky
    Member

    That stack trace doesn't match up with the provided source. Are you sure you posted the source from the correct thread?

  2. azchohfi commented on May 7, 2020

    @azchohfi
    ContributorAuthor

    I've updated to dotnet core 5.0.100-preview.4.20257.1 and the exception is gone!

  3. azchohfi commented on May 8, 2020

    @azchohfi
    ContributorAuthor

    This code repros it:

                const string _root = "https://api.github.com";
                const string _repoName = "WindowsCommunityToolkit";
                const string _repoOwner = "Microsoft";
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.TryAppendWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
    
                    var uri = $"{_root}/repos/{_repoOwner}/{_repoName}/releases";
                    var result = await client.GetStringAsync(new Uri(uri));
                }

    This is the stack:

    >	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructureHelper(System.IntPtr ptr, System.Type structureType) Line 203	C#
     	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructure(System.IntPtr ptr, System.Type structureType) Line 560	C#
     	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructure<WinRT.Interop.IUnknownVftbl>(System.IntPtr ptr) Line 580	C#
     	winrt.runtime.dll!WinRT.ObjectReference<WinRT.Interop.IUnknownVftbl>.GetVtables(System.IntPtr thisPtr)	Unknown
     	winrt.runtime.dll!WinRT.ObjectReference<WinRT.Interop.IUnknownVftbl>.FromAbi(System.IntPtr thisPtr)	Unknown
     	winrt.runtime.dll!WinRT.MarshalInspectable.FromAbi(System.IntPtr ptr)	Unknown
     	winrt.runtime.dll!WinRT.MarshalInterface<Windows.Foundation.IAsyncOperationWithProgress<string, Windows.Web.Http.HttpProgress>>.FromAbi(System.IntPtr ptr)	Unknown
     	Microsoft.Windows.SDK.NET.dll!ABI.Windows.Foundation.AsyncOperationWithProgressCompletedHandler<string, Windows.Web.Http.HttpProgress>.Do_Abi_Invoke.AnonymousMethod__0(System.Delegate invoke)	Unknown
     	winrt.runtime.dll!WinRT.ComWrappersSupport.MarshalDelegateInvoke<System.Delegate>(System.IntPtr thisPtr, System.Action<System.Delegate> invoke)	Unknown
     	Microsoft.Windows.SDK.NET.dll!ABI.Windows.Foundation.AsyncOperationWithProgressCompletedHandler<string, Windows.Web.Http.HttpProgress>.Do_Abi_Invoke(void* thisPtr, System.IntPtr asyncInfo, Windows.Foundation.AsyncStatus asyncStatus)	Unknown
    
    

    System.ExecutionEngineException
    HResult=0x80131506

  4. jkoritzinsky commented on May 8, 2020

    @jkoritzinsky
    Member

    Thanks for the repro! I'll take a look tomorrow!

    My guess is we messed up the refcounting somewhere in the custom Async support and are trying to operate on an already-released object. That's usually the cause of this error.

  5. jkoritzinsky commented on May 8, 2020

    @jkoritzinsky
    Member

    Wait, can you give me the outer wrapper of this code as well that calls this through the Windows APIs? Or preferably a zipped up repro of some source I can build locally?

  6. jkoritzinsky commented on May 8, 2020

    @jkoritzinsky
    Member

    The snippet you've given me looks to be entirely .NET-based C# without any of the WinRT apis, so I don't believe I have enough information to put together a good repro to validate that I fix the bug fully.

  7. azchohfi commented on May 8, 2020

    @azchohfi
    ContributorAuthor

    This is not the System.Net.Http.HttpClient, this is the Windows.Web.Http.HttpClient.
    System.Net.Http.HttpClient.HttpClient is working fine, and was my work around to this issue.

  8. jkoritzinsky commented on May 8, 2020

    @jkoritzinsky
    Member

    Thanks for clarifying! I'll take a look and fixing this tomorrow.

  9. azchohfi commented on May 8, 2020

    @azchohfi
    ContributorAuthor

    I've also updated to .net core 5 preview 5 (5.0.100-preview.5.20257.3) and I'm also hitting this with the same code as before. Stack on Worker Thread:

     	[Managed to Native Transition]	
     	System.Private.CoreLib.dll!System.Reflection.RuntimeAssembly.GetExportedTypes()	Unknown
     	System.Private.CoreLib.dll!System.Reflection.Assembly.ExportedTypes.get()	Unknown
    >	Microsoft.Toolkit.Uwp.SampleApp.dll!Microsoft.Toolkit.Uwp.SampleApp.Sample.LookForTypeByName(string typeName) Line 671	C#
     	Microsoft.Toolkit.Uwp.SampleApp.dll!Microsoft.Toolkit.Uwp.SampleApp.Sample.PreparePropertyDescriptorAsync() Line 572	C#
    
    

    Lines of code inside LookForTypeByName method:

                var wuxType = Windows.UI.Input.RadialControllerMenuKnownIcon.InkColor;
                assembly = wuxType.GetType().GetTypeInfo().Assembly;
    
                foreach (var typeInfo in assembly.ExportedTypes)

    Actual thread that throws the exception:

     	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructureHelper(System.IntPtr ptr, System.Type structureType)	Unknown
     	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructure(System.IntPtr ptr, System.Type structureType)	Unknown
     	System.Private.CoreLib.dll!System.Runtime.InteropServices.Marshal.PtrToStructure<WinRT.Interop.IUnknownVftbl>(System.IntPtr ptr)	Unknown
    >	WinRT.Runtime.dll!WinRT.ObjectReference<WinRT.Interop.IUnknownVftbl>.GetVtables(System.IntPtr thisPtr) Line 210	C#
     	WinRT.Runtime.dll!WinRT.ObjectReference<WinRT.Interop.IUnknownVftbl>.FromAbi(System.IntPtr thisPtr) Line 200	C#
     	WinRT.Runtime.dll!WinRT.MarshalInspectable.FromAbi(System.IntPtr ptr) Line 953	C#
     	WinRT.Runtime.dll!WinRT.MarshalInterface<Windows.Foundation.IAsyncOperation<System.Collections.Generic.IReadOnlyList<Windows.Storage.IStorageItem>>>.FromAbi(System.IntPtr ptr) Line 799	C#
     	Microsoft.Windows.SDK.NET.dll!ABI.Windows.Foundation.AsyncOperationCompletedHandler<System.Collections.Generic.IReadOnlyList<Windows.Storage.IStorageItem>>.Do_Abi_Invoke.AnonymousMethod__0(System.Delegate invoke)	Unknown
     	WinRT.Runtime.dll!WinRT.ComWrappersSupport.MarshalDelegateInvoke<System.Delegate>(System.IntPtr thisPtr, System.Action<System.Delegate> invoke) Line 62	C#
     	Microsoft.Windows.SDK.NET.dll!ABI.Windows.Foundation.AsyncOperationCompletedHandler<System.Collections.Generic.IReadOnlyList<Windows.Storage.IStorageItem>>.Do_Abi_Invoke(void* thisPtr, System.IntPtr asyncInfo, Windows.Foundation.AsyncStatus asyncStatus)	Unknown
    

    Lines of code of CSWinRT that threw the exception:

            private static unsafe (IUnknownVftbl vftblIUnknown, T vftblT) GetVtables(IntPtr thisPtr)
            {
                var vftblPtr = Unsafe.AsRef<VftblPtr>(thisPtr.ToPointer());
                var vftblIUnknown = Marshal.PtrToStructure<IUnknownVftbl>(vftblPtr.Vftbl);

    There is also this other GC Finalizer Thread:

     	[Managed to Native Transition]	
    >	WinRT.Runtime.dll!ABI.WinRT.Interop.IContextCallback.ContextCallback(WinRT.Interop.PFNCONTEXTCALL pfnCallback, WinRT.Interop.ComCallData* pParam, System.Guid riid, int iMethod) Line 76	C#
     	WinRT.Runtime.dll!WinRT.ObjectReferenceWithContext<WinRT.IInspectable.Vftbl>.Release() Line 245	C#
     	WinRT.Runtime.dll!WinRT.IObjectReference.Dispose(bool disposing) Line 113	C#
     	WinRT.Runtime.dll!WinRT.IObjectReference.~IObjectReference() Line 50	C#
    
    

    Running this code:

            public unsafe void ContextCallback(global::WinRT.Interop.PFNCONTEXTCALL pfnCallback, ComCallData* pParam, Guid riid, int iMethod)
            {
                Marshal.ThrowExceptionForHR(_obj.Vftbl.ContextCallback_4(ThisPtr, pfnCallback, pParam, ref riid, iMethod, IntPtr.Zero));
            }
  10. jkoritzinsky commented on May 8, 2020

    @jkoritzinsky
    Member

    I finally got a repro. As we've both discovered, this only repros under a managed debugger.

    What's interesting is that the COM object pointer looks completely valid. It's IUnknown vtable is fully intact and in the right place. Something in the marshalling IL stub is causing it to throw an EngineExecutionException. I'm trying to narrow that down now.

  11. jkoritzinsky commented on May 12, 2020

    @jkoritzinsky
    Member

    .NET 5 runtime builds version 5.0.0-preview.6.20262.3 or later have the fix.

  12. mqudsi commented on May 25, 2020

    @mqudsi

    Is this the same underlying issue? microsoft/microsoft-ui-xaml#2518 I originally filed it in the WinUI repo, but I'm thinking I should have actually opened a ticket here.

    It's an access violation coming on the heels of an WinRT.IInspectable bad cast, but the violation only happens with the managed debugger attached and reproduces in a clean WinUI project with .NET 5 preview 4 doing WinRT interop. I tried to use the latest .NET preview but WinUI still calls RegisterAsGlobalInstance instead of RegisterForMarshalling so I can't test it out (presumably it's fixed in the WinUI 3 git repo but I haven't gotten around to building it from code).

  13. jkoritzinsky commented on May 25, 2020

    @jkoritzinsky
    Member

    Yes that is the same underlying issue. Once we update to use RegisterForReferenceTracking, the InvalidCastException will go away. The related AV will go away as well, but the actual fix for the underlying issue is in .NET 5.0.0-preview.6.20262.3 or newer.

  14. mqudsi commented on May 28, 2020

    @mqudsi

    Thank you very much for the confirmation, I was very close to losing my mind trying to figure it out and really confused that I seemed to be the only one tripping over it left and right w/ the WinUI 3 preview release.

  15. added
    bug
    on Jun 16, 2020
  16. jkoritzinsky commented on Jul 1, 2020

    @jkoritzinsky
    Member

    Now that .NET 5 Preivew 6 is released, I'm going to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions