CommunityToolkit / WindowsCommunityToolkit

The Windows Community Toolkit is a collection of helpers, extensions, and custom controls. It simplifies and demonstrates common developer tasks building .NET apps with UWP and the Windows App SDK / WinUI 3 for Windows 10 and Windows 11. The toolkit is part of the .NET Foundation.
https://docs.microsoft.com/windows/communitytoolkit/
Other
5.89k stars 1.38k forks source link

PipelineVisualFactory BlurEffect crash when run on VM #4855

Open kjean667 opened 1 year ago

kjean667 commented 1 year ago

Describe the bug

Using PipelineVisualFactory effects in XAML, such as BlurEffect or GrayscaleEffect from the CommunityToolkit.WinUI.UI.Media library, throws an unhandled exception when application is run on a virtual machine (Hyper-V).

This may cause the application to crash.

Regression

Unknown

Reproducible in sample app?

Steps to reproduce

  xmlns:media="using:CommunityToolkit.WinUI.UI.Media"

  <StackPanel>
    <TextBlock Text="Testing" Margin="10"/>
    <media:UIElementExtensions.VisualFactory>
      <media:PipelineVisualFactory Source="{media:BackdropSource}">
        <media:BlurEffect Amount="2" />
      </media:PipelineVisualFactory>
    </media:UIElementExtensions.VisualFactory>
  </StackPanel>
    public App()
    {
      UnhandledException += OnUnhandledException;

      this.InitializeComponent();
    }

    private void OnUnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
    {
      EventLog eventLog = new();
      eventLog.Source = "Application Error";
      eventLog.WriteEntry(Package.Current.DisplayName + ": Unhandled exception.\n" + e.Exception, EventLogEntryType.Error);
    }

Expected behavior

Either:

  1. The effect shall be made to work even when run on a Hyper-V VM or
  2. The exception shall be handled and the effect creation shall be skipped because the hardware does not support it.

The application shall not crash.

Stack trace for the unhandled exception

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.Runtime.InteropServices.COMException (0x80040154): Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))
   at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName)
   at WinRT.ActivationFactory`1..ctor()
   at System.RuntimeType.CreateInstanceOfT()
   --- End of inner exception stack trace ---
   at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at WinRT.WeakLazy`1.get_Value()
   at WinRT.ActivationFactory`1.ActivateInstance[I]()
   at Microsoft.Graphics.Canvas.Effects.GaussianBlurEffect..ctor()
   at CommunityToolkit.WinUI.UI.Media.Pipelines.PipelineBuilder.<>c__DisplayClass12_0.<<Blur>g__Factory|0>d.MoveNext()
--- End of stack trace from previous location ---
   at CommunityToolkit.WinUI.UI.Media.Pipelines.PipelineBuilder.BuildAsync()
   at CommunityToolkit.WinUI.UI.Media.PipelineVisualFactoryBase.GetAttachedVisualAsync(UIElement element)
   at CommunityToolkit.WinUI.UI.Media.PipelineVisualFactory.GetAttachedVisualAsync(UIElement element)
   at CommunityToolkit.WinUI.UI.Media.UIElementExtensions.<>c__DisplayClass3_0.<<OnVisualFactoryPropertyChanged>g__Set|0>d.MoveNext()
--- End of stack trace from previous location ---
   at CommunityToolkit.WinUI.UI.Media.UIElementExtensions.<>c__DisplayClass3_0.<<OnVisualFactoryPropertyChanged>g__Element_Loaded|1>d.MoveNext()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
   at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0()

Windows Build Number

Other Windows Build number

Windows 10 22H2 (Build 19045.2006)

App minimum and target SDK version

Other SDK version

No response

Visual Studio Version

2022

Visual Studio Build Number

17.5.1

Device form factor

Desktop

Nuget packages

CommunityToolkit.WinUI.UI.Media (7.1.2) Microsoft.Windows.SDK.BuildTools (10.0.22621.755) Microsoft.WindowsAppSDK (1.2.221109.1) System.Diagnostics.EventLog (7.0.0)

Additional context

Application is built as 64-bit and run on a Hyper-V VM under Windows 10 21H2 (19044.2604) on an Intel Core i5-7500T CPU.

Help us help you

No.

ghost commented 1 year ago

Hello kjean667, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

michael-hawker commented 1 year ago

@Sergio0694 any of this looks familiar to you? Is there some Win2D or other device check we may be missing for WindowsAppSDK that we wouldn't need to do in UWP? (Or maybe this is an issue there too?)

Sergio0694 commented 1 year ago

That at Microsoft.Graphics.Canvas.Effects.GaussianBlurEffect..ctor() line is suspect, that shouldn't possibly crash (and that "class not registered" is weird in this context). As a sanity check, does just creating new GaussianBlurEffect() also fail in the VM?

kjean667 commented 1 year ago

@Sergio0694 Yes, new GaussianBlurEffect() fails and throws an unhandled exception if run in code behind when for example the Loaded event is triggered by a Page.

Stack trace of new GaussianBlurEffect():

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.Runtime.InteropServices.COMException (0x80040154): Klassen har inte registrerats (0x80040154 (REGDB_E_CLASSNOTREG))
   at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName)
   at WinRT.ActivationFactory`1..ctor()
   at System.RuntimeType.CreateInstanceOfT()
   --- End of inner exception stack trace ---
   at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at WinRT.WeakLazy`1.get_Value()
   at WinRT.ActivationFactory`1.ActivateInstance[I]()
   at Microsoft.Graphics.Canvas.Effects.GaussianBlurEffect..ctor()
   at PipelineVisualFactoryBug.MainPage.MainPage_Loaded(Object sender, RoutedEventArgs e)
   at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

As a side note, the unhandled exception does not crash the application (but is still thrown as an unhandled exception) if it is triggered from the MainWindow (class Window) object. However if the blur effect is created by a Page it will crash the application. I.e. if the MainWindow contains a Page reference that uses GaussianBlurEffect it will crash the entire application. In my stack trace above, the MainWindow.xaml contains a Page called MainPage. The MainPage code behind looks like this in the stack trace above:

  public sealed partial class MainPage : Page
  {
    private GaussianBlurEffect blur;
    public MainPage()
    {
      InitializeComponent();

      Loaded += MainPage_Loaded;
    }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
      blur = new GaussianBlurEffect();
    }
  }
lunelake commented 1 year ago

I also received error reports from users for the same issue, and it doesn't seem to be limited to just VMs. The user is running Windows 11 (10.0.22621.1555) on a Dell desktop model, OptiPlex 980, and reported the same error.

To test it out, I installed the WinUI3-developed app on a VM and found that the Fallback Color wasn't displayed correctly. In contrast, the UWP-developed app displayed the Blur effect correctly despite having the same code.

I didn't encounter the issue on the devices I tested, but after the app was released, several errors were reported on various devices, and I'm not sure of the cause.

zhuxb711 commented 10 months ago

Solution: https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4963#issuecomment-1884192311