Skip to content

[Bug] Unpackaged WinUI 3 process lingers in background on window close & recommended AppWindow lifecycle handling #256

Backlog

Description

@varunagarwal-pro

Summary

In unpackaged WinUI 3 (WindowsPackageType=None, NativeAOT / self-contained), closing the application via the caption close button (X), Alt+F4, or taskbar context menu destroys the window surface, but does not reliably terminate the host CLR process if background thread pool workers, async preload tasks, or WinRT synchronization contexts remain active.

In our testing, the process can remain running indefinitely in Task Manager as a background process holding ~180–220 MB RAM.

Root Cause

In FluentCleaner/App.xaml.cs:

MainWindow.Closed += (_, _) =>
{
    var size = MainWindow.AppWindow.Size;
    AppSettings.Instance.WindowWidth  = size.Width;
    AppSettings.Instance.WindowHeight = size.Height;
    AppSettings.Instance.Save();
};

In unpackaged desktop WinUI 3, Window.Closed does not automatically invoke Application.Exit(). Furthermore, when window customization or AppWindowTitleBar is active, the caption button close event routes through AppWindow.Closing before Window.Closed.

Proposed Fix

Wire both MainWindow.Closed and MainWindow.AppWindow.Closing to explicitly exit the application and runtime:

MainWindow.Closed += (_, _) =>
{
    try
    {
        var size = MainWindow.AppWindow.Size;
        AppSettings.Instance.WindowWidth  = size.Width;
        AppSettings.Instance.WindowHeight = size.Height;
        AppSettings.Instance.Save();
    }
    catch { }
    finally
    {
        Microsoft.UI.Xaml.Application.Current.Exit();
        Environment.Exit(0);
    }
};

MainWindow.AppWindow.Closing += (_, _) =>
{
    try
    {
        var size = MainWindow.AppWindow.Size;
        AppSettings.Instance.WindowWidth  = size.Width;
        AppSettings.Instance.WindowHeight = size.Height;
        AppSettings.Instance.Save();
    }
    catch { }
    finally
    {
        Microsoft.UI.Xaml.Application.Current.Exit();
        Environment.Exit(0);
    }
};

Note on AppInfo.cs

For forks and community PRs migrating to 26.11.123: ensure any pre-release or duplicate declarations of public static bool IsEleven are consolidated to line 28 (which was canonically added in commit 83828a1) to prevent Roslyn CS0102 duplicate symbol errors during merge.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions