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

ToastNotificationManagerCompat: Cannot create a stable subkey under a volatile parent key #4858

Closed
1 of 14 tasks
caobug opened this issue Mar 26, 2023 · 23 comments
Closed
1 of 14 tasks
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior notifications 🔔

Comments

@caobug
Copy link

caobug commented Mar 26, 2023

Describe the bug

Hi, I'm currently referencing "Microsoft.Toolkit.Uwp.Notifications" in WPF project to send local notifications and this works fine on most devices. but on a small number of computers, a "RegistryKey.Win32Error" error will be thrown.

I can be sure that I'm operating on the main thread. I had to use "try catch" to catch the exception, but this way I can't display the notifications on the target computer.

Source Code:

// ...
// running the service on a different thread...
// ...
// switch to main thread...
Application.Current.Dispatcher.Invoke(delegate { 
    new ToastContentBuilder().SetToastDuration(ToastDuration.Short)
    .AddText("Title")
    .AddText("Content");
    .Show(toast =>
    {
        toast.Tag = NotificationTag.Connection.ToString();
        toast.ExpirationTime = DateTime.Now.AddSeconds(10);
    });
});

Erros:

RegistryKey.Win32Error (Int32 errorCode, String str)
System.InvalidOperationException: Cannot create a stable subkey under a volatile parent key.
Microsoft.Toolkit.Uwp.Notifications
ToastNotificationManagerCompat.CreateToastNotifier ()
Microsoft.Toolkit.Uwp.Notifications
ToastContentBuilder.Show (CustomizeToast customize)
my_b.my_a (my_ab A_0)
MyWindows App.my_a (my_ab A_0)
my_ae b.my_c ()
System.Windows.Threading
ExceptionWrapper.InternalRealCall (Delegate callback, Object args, Int32 numArgs)
System.Windows.Threading
ExceptionWrapper.TryCatchWhen (Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
System.Windows.Threading
DispatcherOperation.InvokeImpl ()
MS.Internal
CulturePreservingExecutionContext.CallbackWrapper (Object obj)
System.Threading
ExecutionContext.RunInternal (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
System.Threading
ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
System.Threading
ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state)
MS.Internal
CulturePreservingExecutionContext.Run (CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
System.Windows.Threading
DispatcherOperation.Invoke ()
System.Windows.Threading
Dispatcher.ProcessQueue ()
System.Windows.Threading
Dispatcher.WndProcHook (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
MS.Win32
HwndWrapper.WndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
MS.Win32
HwndSubclass.DispatcherCallbackOperation (Object o)
System.Windows.Threading
ExceptionWrapper.InternalRealCall (Delegate callback, Object args, Int32 numArgs)
System.Windows.Threading
ExceptionWrapper.TryCatchWhen (Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
System.Windows.Threading
Dispatcher.LegacyInvokeImpl (DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
MS.Win32
HwndSubclass.SubclassWndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
MS.Win32
UnsafeNativeMethods.DispatchMessage (MSG& msg)
System.Windows.Threading
Dispatcher.PushFrameImpl (DispatcherFrame frame)
System.Windows.Threading
DispatcherOperation.Wait (TimeSpan timeout)
System.Windows.Threading
Dispatcher.InvokeImpl (DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout)
System.Windows.Threading
Dispatcher.LegacyInvokeImpl (DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)

Regression

No response

Reproducible in sample app?

  • This bug can be reproduced in the sample app.

Steps to reproduce

Everything works fine locally on me, but a small number of users are getting this error.

Expected behavior

Normal display notifications.

Screenshots

No response

Windows Build Number

  • Windows 10 1809 (Build 17763)
  • Windows 10 1903 (Build 18362)
  • Windows 10 1909 (Build 18363)
  • Windows 10 2004 (Build 19041)
  • Windows 10 20H2 (Build 19042)
  • Windows 10 21H1 (Build 19043)
  • Windows 11 21H2 (Build 22000)
  • Other (specify)

Other Windows Build number

10.0.22621

App minimum and target SDK version

  • Windows 10, version 1809 (Build 17763)
  • Windows 10, version 1903 (Build 18362)
  • Windows 10, version 1909 (Build 18363)
  • Windows 10, version 2004 (Build 19041)
  • Other (specify)

Other SDK version

10.0

Visual Studio Version

2019

Visual Studio Build Number

16.11.25

Device form factor

Desktop

Nuget packages

Microsoft.Toolkit.Uwp.Notifications 7.1.3

Additional context

No response

Help us help you

Yes, but only if others can assist.

@caobug caobug added the bug 🐛 An unexpected issue that highlights incorrect behavior label Mar 26, 2023
@ghost ghost added the needs triage 🔍 label Mar 26, 2023
@ghost
Copy link

ghost commented Mar 26, 2023

Hello caobug, 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
Copy link
Member

@caobug the Windows App SDK Notifications APIs should handle most scenarios now. Have you tried your scenario with them?

FYI @vaheeshta @adambarlow

@VictoriousRaptor
Copy link

VictoriousRaptor commented Apr 20, 2023

We have encoutered the same issue Flow-Launcher/Flow.Launcher#2054. Possibly Windows 11 22621.1413 or 22621.1485 breaks something since we started receiving reports in the past few weeks.

@caobug the Windows App SDK Notifications APIs should handle most scenarios now. Have you tried your scenario with them?

Could you give some advice or document on migrating to Windows App SDK Notifications APIs?

@ThomasSuHo
Copy link

I see the same thing happening in my app. Although my observations would suggest that it's not a matter of the Windows build version per se, as I have three machines here with the same build (22621.1555) and on two of them, the error does not appear, on the third one it does.

@ThomasSuHo
Copy link

Looking at your code, the error is probably in this line:

using (var rootKey = Registry.CurrentUser.CreateSubKey(GetRegistrySubKey()))

If Computer\HKEY_CURRENT_USER\Software\Classes\AppUserModelId is a volatile key, you probably get the error mentioned here.

I haven't found a good way to check whether a key is volatile, so one possible solution would be to surround this call with a try/catch block and if the call fails, try again to create the subkey as a volatile key:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.createsubkey?view=netframework-4.7#microsoft-win32-registrykey-createsubkey(system-string-system-boolean-microsoft-win32-registryoptions)

@Ross-Patterson
Copy link

I'm facing the same problem, all WINDOWS 10.0.22621 in my case.

From what I see @ThomasSuHo is correct with the line that is throwing the error.

@Daniellled
Copy link

I am seeing this issue as well with Microsoft.Toolkit.Uwp.Notifications 7.1.2 on 22621.1778. In our specific case it is after an install of our application. A reboot appears to fix the issue.

@Daniellled
Copy link

Daniellled commented Jun 1, 2023

@caobug the Windows App SDK Notifications APIs should handle most scenarios now. Have you tried your scenario with them?

FYI @vaheeshta @adambarlow

In the case where upgrading to Windows APP SDK is not feasible... Did something change in the OS to break this functionality?

Also if this is nuget package is not valid can the documentation be updated to reflect that - On https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/toast-desktop-apps and https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=uwp I see no indication that this nuget package is no longer supported.

@rbutterworth
Copy link

I think I am seeing this one random machines I have deployed the app to - most work fine. I'll see if rebooting helps (but typically I can't reproduce it on any of my machines, so I need a friendly end-user to help).

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
at Microsoft.Win32.RegistryKey.Win32Error(Int32, System.String)
at Microsoft.Win32.RegistryKey.CreateSubKeyInternal(System.String, Microsoft.Win32.RegistryKeyPermissionCheck, System.Object, Microsoft.Win32.RegistryOptions)
at Microsoft.Win32.RegistryKey.CreateSubKey(System.String, Microsoft.Win32.RegistryKeyPermissionCheck)
at Microsoft.Win32.RegistryKey.CreateSubKey(System.String)
at Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat.Initialize()
at Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat..cctor()

@ThomasSuHo
Copy link

@michael-hawker I think, this issue needs a higher priority because the description is basically "on some machines, notifications won't work at all".

@vspoelstra71
Copy link

We have this exact problem with the newest Windows 11 version in an Intune managed environment. An application using this toolkit crashes with the same error. We were able to start the application using the Windows 8 compatibility mode. We have no idea what the exact problem is.

@rbutterworth
Copy link

rbutterworth commented Jun 6, 2023 via email

@smithmw
Copy link

smithmw commented Jun 30, 2023

Our product is also encountering this with high frequency from some of our users.

@michael-hawker
Copy link
Member

Folks, as I mentioned at the top of the thread, the Windows App SDK notification API is the path forward, have you tried migrating to that solution and seeing if the same issue occurs?

If so, please file an issue at their repo here: https://github.com/microsoft/WindowsAppSDK

FYI @vaheeshta @adambarlow

@smithmw
Copy link

smithmw commented Jun 30, 2023

@michael-hawker That is fine to try for a path forward. However, we have software in the field that is experiencing this issue only on certain environments. Those on this thread are likely looking for a fix from Microsoft that doesn't involve modifying the already delivered software.

@Daniellled
Copy link

@michael-hawker I agree with @smithmw. This functionality worked on prior versions of Windows. We have our application running on 10k+ endpoints. Refactoring our code and pushing a patch because something broke between versions of Windows is not ideal. Especially since the last version of this package was in November and it is not marked as deprecated.

@Daniellled
Copy link

@michael-hawker Could this nuget package be refactored to use Windows App SKD notification API? It would allow development teams to continue using the package and upgrade to Windows App SDK at their leisure.

@michael-hawker
Copy link
Member

@smithmw @Daniellled if there's a bug with Notifications in Windows it should be reported I believe in Feedback hub so that the OS can be patched; if you've done so and can link here, we can try and connect the dots to the notifications team that owns these APIs.

All work is being done on the new notifications API in the Windows App SDK, that is the current version of this API. We have plans to deprecate packages and point to the 1.4 version of the Windows App SDK when it ships. We have no plans to patch the current codebase nor attempt to polyfill these APIs. I've been told the API surface is very similar between the Toolkit and the new APIs (as they were designed from one another by the same group, just shipped in different places). Upgrading to the new API is the best path forward.

FYI @robertzhou-wpn

@Daniellled
Copy link

So I attempt to update my WPF project to use Windows App SDK and I run into this error microsoft/microsoft-ui-xaml#5689. I am guessing that Windows App SDK and WPF WebView2 are not compatible?

@Difegue
Copy link
Contributor

Difegue commented Jul 24, 2023

Are we really supposed to add a 75MB nuget package containing all of WinUI 3 just to get the fixed Notifications API that's being shipped in it?

This hurts using native notifications from WPF apps (supposedly still actively supported by microsoft :^) ) quite badly.
I could accept it if at least there were plans to chop up the WASDK package into smaller ones...

@mavanmanen
Copy link

mavanmanen commented Jul 29, 2023

It was already a pain in the behind to get this working even a little and when I finally got it all configured right now this error, really needs a fix or an easier way to access notifications in WPF, this is such basic functionality, I'm baffled why it has to be so difficult.

@michael-hawker Issue also happens on the "Windows App SDK notification API" method you linked btw.

@rafayahmed317
Copy link

Great, until this gets fixed, we are basically left with no notifications. Does anybody know another way?

@michael-hawker
Copy link
Member

Thanks @mavanmanen for opening the issue on the platform repo, I've raised it up with the notifications team. Since microsoft/WindowsAppSDK#3771 is tracking this problem now in the platform, I'm going to close this issue. I've asked them to respond with more information on that issue.


So I attempt to update my WPF project to use Windows App SDK and I run into this error microsoft/microsoft-ui-xaml#5689. I am guessing that Windows App SDK and WPF WebView2 are not compatible?

@Daniellled that issue you linked may look similar, but it sounds different from what you're describing as it involves Microsoft Graph and not just WebView2. Can you file an issue in the Windows App SDK repo describing your issues having a WPF app using WebView2 referencing the Windows App SDK (feel free to still point to the links above as related). Thanks!

@michael-hawker michael-hawker closed this as not planned Won't fix, can't repro, duplicate, stale Aug 17, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Oct 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior notifications 🔔
Projects
None yet
Development

No branches or pull requests