microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.28k stars 675 forks source link

App crashes when closing window immediately after hiding content dialog (under specific circumstances) #8365

Open DHancock opened 1 year ago

DHancock commented 1 year ago

Describe the bug

My app is an unpackaged self contained c# desktop app that can support multiple windows. Each window can display an about box, implemented as a pop up content dialog. When the user closes a window the about box is hidden to allow another content dialog to be shown prompting the user to save first. If the window contents aren't dirty then the window is just closed. I've found that if there are two windows open, both displaying about boxes and the user closes a window that doesn't need it's contents to be saved then the app traps.

It's a little contrived but not difficult to reproduce.

I have uploaded a dump file to OneDrive

Stack trace...

[0x0]   KERNELBASE!RaiseFailFastException + 0x152   
[0x1]   combase!RoFailFastWithErrorContextInternal2 + 0x4d9   
[0x2]   Microsoft_ui_xaml!DirectUI::ErrorHelper::ProcessUnhandledError + 0xfc   
[0x3]   Microsoft_ui_xaml!DirectUI::FinalUnhandledErrorDetectedRegistration::OnFinalUnhandledErrorDetected + 0xd4   
[0x4]   twinapi_appcore!Windows::Internal::Details::GitInvokeHelper,Windows::Internal::GitPtr,2>::Invoke + 0x64   
[0x5]   twinapi_appcore!Windows::ApplicationModel::Core::UnhandledErrorInvokeHelper::Invoke + 0x24   
[0x6]   twinapi_appcore!Microsoft::WRL::InvokeTraits<2>::InvokeDelegates<,Windows::Foundation::IEventHandler > + 0x62   
[0x7]   twinapi_appcore!Microsoft::WRL::EventSource,Microsoft::WRL::InvokeModeOptions<2> >::DoInvoke< > + 0x78   
[0x8]   twinapi_appcore!Microsoft::WRL::EventSource,Microsoft::WRL::InvokeModeOptions<2> >::InvokeAll + 0x29   
[0x9]   twinapi_appcore!Windows::ApplicationModel::Core::CoreApplication::ForwardLocalError + 0x79   
[0xa]   twinapi_appcore!Windows::ApplicationModel::Core::CoreApplicationFactory::ForwardLocalError + 0x77   
[0xb]   combase!CallErrorForwarder + 0x150   
[0xc]   Microsoft_ui_xaml!DirectUI::ErrorHelper::ReportUnhandledError + 0xd3   
[0xd]   Microsoft_ui_xaml!CCoreServices::CLR_FireEvent + 0x1d02c4   
[0xe]   Microsoft_ui_xaml!CommonBrowserHost::CLR_FireEvent + 0x1d   
[0xf]   Microsoft_ui_xaml!CControlBase::ScriptCallback + 0x151   
[0x10]   Microsoft_ui_xaml!CXcpDispatcher::OnScriptCallback + 0x17b   
[0x11]   Microsoft_ui_xaml!CXcpDispatcher::OnWindowMessage + 0x3e   
[0x12]   Microsoft_ui_xaml!CXcpDispatcher::WindowProc + 0x59   
[0x13]   Microsoft_ui_xaml!CDeferredInvoke::DispatchQueuedMessage + 0x6f   
[0x14]   Microsoft_ui_xaml!Microsoft::WRL::Details::DelegateArgTraits,IInspectable *>::*)(ABI::Microsoft::UI::Dispatching::IDispatcherQueueTimer *,IInspectable *)>::DelegateInvokeHelper,ABI::Windows::Foundation::ITypedEventHandler,Microsoft::WRL::FtmBase>,`CXcpDispatcher::Init'::`75':: &,1,ABI::Microsoft::UI::Dispatching::IDispatcherQueueTimer *,IInspectable *>::Invoke + 0x1d   
[0x15]   CoreMessagingXP!Microsoft::WRL::Details::DelegateArgTraits,IInspectable * __ptr64>::*)(Microsoft::UI::Dispatching::IDispatcherQueueTimer * __ptr64,IInspectable * __ptr64) __ptr64>::DelegateInvokeHelper,Windows::Foundation::ITypedEventHandler,Microsoft::WRL::FtmBase>,,-1,Microsoft::UI::Dispatching::IDispatcherQueueTimer * __ptr64,IInspectable * __ptr64>::Invoke + 0xa8   
[0x16]   CoreMessagingXP!Microsoft::WRL::InvokeTraits<-2>::InvokeDelegates<,Windows::Foundation::ITypedEventHandler > + 0x8e   
[0x17]   CoreMessagingXP!Microsoft::UI::Dispatching::DispatcherQueueTimer::TimerCallback + 0xdb   
[0x18]   CoreMessagingXP!CFlat::SehSafe::Execute< > + 0x2c   
[0x19]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::TimeoutHandler::ImportAdapter$ + 0x5b   
[0x1a]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::TimeoutManager::Callback_OnDispatch + 0x1b0   
[0x1b]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::Dispatcher::Callback_DispatchNextItem + 0x1e4   
[0x1c]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::Dispatcher::Callback_DispatchLoop + 0x180   
[0x1d]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::EventLoop::Callback_RunCoreLoop + 0x16b   
[0x1e]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::UserAdapter::DrainCoreMessagingQueue + 0x138   
[0x1f]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::UserAdapter::OnUserDispatch + 0xaf   
[0x20]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::UserAdapter::OnUserDispatchRaw + 0x80   
[0x21]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::UserAdapter::DoWork + 0x59   
[0x22]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::UserAdapter::HandleDispatchNotifyMessage + 0x13e   
[0x23]   CoreMessagingXP!Microsoft::CoreUI::Dispatch::UserAdapter::WindowProc + 0x73   
[0x24]   USER32!UserCallWinProcCheckWow + 0x2d1   
[0x25]   USER32!DispatchClientMessage + 0x9c   
[0x26]   USER32!_fnDWORD + 0x3d   
[0x27]   ntdll!KiUserCallbackDispatcherContinue   
[0x28]   win32u!NtUserGetMessage + 0x14   
[0x29]   USER32!GetMessageW + 0x2a   
[0x2a]   Microsoft_ui_xaml!DirectUI::FrameworkApplication::RunDesktopWindowMessageLoop + 0x3a   
[0x2b]   Microsoft_ui_xaml!DirectUI::FrameworkApplication::StartDesktop + 0x14c   
[0x2c]   Microsoft_ui_xaml!DirectUI::FrameworkApplicationFactory::Start + 0x6e   
[0x2d]   0x7ffd543bcc2d   
[0x2e]   Microsoft_WinUI!ABI.Microsoft.UI.Xaml.IApplicationStaticsMethods.Start + 0x102   
[0x2f]   Microsoft_WinUI!Microsoft.UI.Xaml.Application.Start + 0x2a   
[0x30]   CrashOnClose!CrashOnClose.Program.
d__2.MoveNext + 0x170 [0x31] System_Private_CoreLib!System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start<
d__2> + 0x63 [0x32] System_Private_CoreLib!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<
d__2> + 0x1b [0x33] CrashOnClose!CrashOnClose.Program.Main + 0x3d [0x34] CrashOnClose!CrashOnClose.Program.
+ 0x1d [0x35] coreclr!CallDescrWorkerInternal + 0x83 [0x36] coreclr!MethodDescCallSite::CallTargetWorker + 0x176 [0x37] coreclr!MethodDescCallSite::Call_RetArgSlot + 0xd [0x38] coreclr!RunMainInternal + 0xbb [0x39] coreclr!RunMain + 0xd2 [0x3a] coreclr!Assembly::ExecuteMainMethod + 0x1c9 [0x3b] coreclr!CorHost2::ExecuteAssembly + 0x1c6 [0x3c] coreclr!coreclr_execute_assembly + 0xe2 [0x3d] hostpolicy!coreclr_t::execute_assembly + 0x2a [0x3e] hostpolicy!run_app_for_context + 0x56b [0x3f] hostpolicy!run_app + 0x3c [0x40] hostpolicy!corehost_main + 0x107 [0x41] hostfxr!execute_app + 0x2e9 [0x42] hostfxr!`anonymous namespace'::read_config_and_execute + 0xa6 [0x43] hostfxr!fx_muxer_t::handle_exec_host_command + 0x161 [0x44] hostfxr!fx_muxer_t::execute + 0x482 [0x45] hostfxr!hostfxr_main_startupinfo + 0xab [0x46] CrashOnClose_exe!exe_start + 0x8d8 [0x47] CrashOnClose_exe!wmain + 0xab [0x48] CrashOnClose_exe!invoke_main + 0x22 [0x49] CrashOnClose_exe!__scrt_common_main_seh + 0x10c [0x4a] KERNEL32!BaseThreadInitThunk + 0x1d [0x4b] ntdll!RtlUserThreadStart + 0x28

Steps to reproduce the bug

  1. Download attached min repro project
  2. Build debug or release x64 target
  3. Start with out the debugger (Ctrl + F5)
  4. When the first window opens, create a second window using the File menu option
  5. Open the about box on the second window using the View menu option
  6. Switch to the first window by clicking on it and open a second about box
  7. Switch back to the second window and close it using the title bar close box

That usually causes it to trap, at least on my machine. Attaching WinDbg-Preview to the running process usually makes it more difficult to repro but still fairly easy.

CrashOnClose.zip

Expected behavior

No response

Screenshots

No response

NuGet package version

Windows App SDK 1.3 Preview 1: 1.3.230228005-preview1

Packaging type

Unpackaged

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

Visual Studio 2022

Additional context

I can also reproduce this with WinAppSdk 1.2.4 and 1.2.5

codendone commented 1 year ago

Thanks for the report! Looking at the stowed exception stacks in your dump, this stack appears to be the cause:

0:000> !pde.dse
... snipping out header and Stowed Exception microsoft/WindowsAppSDK#1, which is just a noisy exception...
...
Stowed Exception microsoft/WindowsAppSDK#2 @ 0x000001f443066078
    0x8000FFFF (FACILITY_NULL - Default): E_UNEXPECTED - Unexpected failure

    Stack    : 0x1f443065060
        7ffd9f0988a7 Microsoft_ui_xaml!DirectUI::DXamlCore::GetInputPaneOccludeRect+0xeb
        7ffd9f09890b Microsoft_ui_xaml!DirectUI::DXamlCore::GetInputPaneOccludeRect+0x47
        7ffd9ef86a6f Microsoft_ui_xaml!DirectUI::ContentDialog::AdjustVisualStateForInputPane+0x43
        7ffd9ef88a04 Microsoft_ui_xaml!DirectUI::ContentDialog::ChangeVisualState+0x684
        7ffd9eaad87d Microsoft_ui_xaml!DirectUI::Control::UpdateVisualState+0x21
        7ffd9ef8d54b Microsoft_ui_xaml!DirectUI::ContentDialog::OnFinishedClosing+0x4b
        7ffd9ef8da99 Microsoft_ui_xaml!DirectUI::ContentDialog::OnPopupChildUnloaded+0x29
        7ffd9eb41028 Microsoft_ui_xaml!std::_Func_class<long,IInspectable *,IInspectable *>::operator()+0x3c
        7ffd9eb40fda Microsoft_ui_xaml!ctl::event_handler_base<ABI::Windows::Foundation::IEventHandler<IInspectable *>,IInspectable,IInspectable,DirectUI::FrameworkElementLayoutUpdatedTraits>::Invoke+0x1a
        7ffd9e93d3c1 Microsoft_ui_xaml!DirectUI::CRoutedEventSourceBase<DirectUI::IUntypedEventSource,ABI::Microsoft::UI::Xaml::IRoutedEventHandler,IInspectable,ABI::Microsoft::UI::Xaml::IRoutedEventArgs>::Raise+0xd9
        7ffd9e93d1f2 Microsoft_ui_xaml!DirectUI::CRoutedEventSourceBase<DirectUI::IUntypedEventSource,ABI::Microsoft::UI::Xaml::IRoutedEventHandler,IInspectable,ABI::Microsoft::UI::Xaml::IRoutedEventArgs>::UntypedRaise+0x92
        7ffd9ea68a9f Microsoft_ui_xaml!CCoreServices::CLR_FireEvent+0x3cf
        ...

It looks like shutdown of the window has proceeded enough that the ContentDialog is disconnected from the tree, which the top frames of this stack don't support.