maui icon indicating copy to clipboard operation
maui copied to clipboard

Windows app gets a unknown win32 error on exit

Open davefxy opened this issue 2 years ago • 9 comments

Description

WebViewDemos.zip

Start the ap WebVideoDemos app on Windows. Select the last 2 entries, entering and exits. Then exit the app. Upon exiting this windows app there is an unidentified win32 error.

Steps to Reproduce

Start the ap WebVideoDemos app on Windows. Select the last 2 entries, entering and exits. Then exit the app. Upon exiting this windows app there is an unidentified win32 error.

Included video of sequence of steps to create the error.

Version with bug

Release Candidate 3 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

net6.0-windows 10.0.19041

Did you find any workaround?

https://user-images.githubusercontent.com/2566250/169185987-63494b05-315a-48ac-a6aa-914837f4d35d.mp4

Relevant log output

No response

davefxy avatar May 19 '22 01:05 davefxy

Verified this issue with Visual Studio Enterprise 17.3.0 Preview 2.0 [32515.61.main]. Not repro on Windows 10 with above project.

XamlTest avatar May 19 '22 01:05 XamlTest

Cannot reproduce the issue on Windows 10. I will test on Windows 11.

jsuarezruiz avatar May 19 '22 09:05 jsuarezruiz

Is there a procedure I can perform to help eliminate problems (clean up) from having installed all the VS previews? 

davefxy avatar May 19 '22 13:05 davefxy

Cannot reproduce the issue on Windows 10. I will test on Windows 11.

Tested and can reproduce it with Windows 11.

jsuarezruiz avatar May 30 '22 08:05 jsuarezruiz

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Aug 25 '22 18:08 ghost

Related: https://github.com/microsoft/terminal/issues/11724 https://github.com/microsoft/microsoft-ui-xaml/issues/7260

I find it amusing that apparently WebViews have been broken for almost a year at least and no one at Microsoft seems to care about it.

namazso avatar Aug 30 '22 03:08 namazso

I wrote a long explanation on the bug here: https://github.com/microsoft/microsoft-ui-xaml/issues/7260#issuecomment-1231314776

A workaround I found is that leaking exactly 3 references to Windows.System.Threading.ThreadPoolTimer should be working now, and even when Microsoft fixes this issue, as the issue is triggered when a late decremented reference results in a reference count of 1. After this fix I can't reproduce the issue anymore.

Code:

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
{
    [DllImport("api-ms-win-core-winrt-l1-1-0.dll")]
    private static extern int RoGetActivationFactory(
        IntPtr activatableClassId,
        ref Guid iid,
        out IntPtr factory);

    [DllImport("api-ms-win-core-winrt-string-l1-1-0.dll")]
    private static extern int WindowsCreateString(
        [MarshalAs(UnmanagedType.LPWStr)] string sourceString,
        int length,
        out IntPtr str);

    // https://github.com/dotnet/maui/issues/7317
    // https://github.com/microsoft/microsoft-ui-xaml/issues/7260
    void WorkaroundMicrosoftBug()
    {
        string name = "Windows.System.Threading.ThreadPoolTimer";
        WindowsCreateString(name, name.Length, out var str);
        var guid = new Guid(0x1a8a9d02, 0xe482, 0x461b, 0xB8, 0xC7, 0x8E, 0xFA, 0xD1, 0xCC, 0xE5, 0x90);
        RoGetActivationFactory(
            str,
            ref guid,
            out _
        );
        RoGetActivationFactory(
            str,
            ref guid,
            out _
        );
        RoGetActivationFactory(
            str,
            ref guid,
            out _
        );
    }


    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        WorkaroundMicrosoftBug();
        this.InitializeComponent();
	}

	protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}

namazso avatar Aug 30 '22 17:08 namazso

Thank you @namazso After trying the suggested workaround, I no longer get a Win32 exception when closing the app. I was at a loss as to what to do as I thought it would take over a year for Microsoft to fix it, but it helped. Thank you very much.

u1-kino avatar Aug 31 '22 10:08 u1-kino

You might want to retry the last weeks bug reports I submitted you were not able to reproduce on windows 10 and retry them on windows 11. Sent from Mail for Windows From: Javier SuárezSent: Thursday, May 19, 2022 3:00 AMTo: dotnet/mauiCc: Davefxy; AuthorSubject: Re: [dotnet/maui] Windows app gets a unknown win32 error on exit (Issue #7317) Cannot reproduce the issue on Windows 10. I will test on Windows 11.—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***> 

davefxy avatar Oct 11 '22 09:10 davefxy

Thiis now fixed, YAY!

https://github.com/microsoft/microsoft-ui-xaml/issues/7260#issuecomment-1404393441

@namazso, your analysis is spot-on, thank you. The fix is simply to let the global threadpool factory objects "leak" at app shutdown (which of course, the OS will clean up) - as also recommended by Raymond Chen.

Fix is now available: https://www.nuget.org/packages/Microsoft.WindowsAppSDK/1.2.230118.102

mattleibow avatar Feb 01 '23 23:02 mattleibow

@namazso

I don't know from what planet you come from, but I want to thank you for your gift to humanity. Your fix is beyond my simple human mind understanding. But it works.

If only you could work for MS.

applefanbois avatar Feb 09 '23 20:02 applefanbois