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

Fix a crash setting the hotkey during teardown #12580

Merged
5 commits merged into from
Mar 1, 2022

Conversation

zadjii-msft
Copy link
Member

@zadjii-msft zadjii-msft commented Feb 25, 2022

From MSFT:36797001. Okay so this is only .22% of our crashes, but every little bit helps, right?

Turns out this is also hitting in:

and together they're a fairly hot bug.

There's a large class of bugs where we might get a callback to one of our event handlers when we call app.Close() in the AppHost dtor. This PR adds manual revokers to these events, and makes sure to revoke them BEFORE nulling out the _window. That will prevent callbacks during the rest of the dtor, when the _window is null.

From MSFT:36797001. Okay so this is only .22% of our crashes, but every little bit helps, right?
@zadjii-msft zadjii-msft added Severity-Crash Crashes are real bad news. zStable-Service-Queued-1.12 A floating label that tracks the current Stable version for servicing purposes. zPreview-Service-Queued-1.13 A floating label that tracks the current Preview version for servicing purposes. labels Feb 25, 2022
@@ -967,6 +967,15 @@ winrt::fire_and_forget AppHost::_setupGlobalHotkeys()
// The hotkey MUST be registered on the main thread. It will fail otherwise!
co_await winrt::resume_foreground(_logic.GetRoot().Dispatcher());

if (!_window)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah we null it out properly too? Awesome

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, is this sufficient? The only time we will observe _window to be null is after our destructor has run... which would make accessing &this->_window an AV (or UB). We need to make sure that AppHost is properly lifetime-managed before we get here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AH YOU'D THINK

Looking at the stack, this hits in this sliver of time where we've called ~AppHost, we've nulled out the _window, but then the Dispatcher like, runs a bunch more and... flushes callbacks? I'm not sure what it's doing here. There's been miscellaneous crashes over the last few months where like, we try to set the title of the window but it doesn't exist, because we're in this bit of Dispatcher rundown. This is just another version of that. They're all crashes once we've already started tearing down, so they're not that user-impactful, but they still happen.

IDK if it's safe to just gank the Dispatcher before we null out the _window to release the XAML bits.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the ~AppHost dtor is not on the stack, it is still UB to access this member; I'm concerned this is a band-aid? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, to clarify, the ~AppHost dtor is on the stack here.

Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notes

@ghost ghost added Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Feb 25, 2022
@miniksa miniksa dismissed DHowett’s stale review March 1, 2022 16:54

He's on vacation and this fix is affecting internal reliability calculations. And Griese answered the notes.

@github-actions

This comment was marked as resolved.

Comment on lines 136 to 157
winrt::TerminalApp::AppLogic::CloseRequested_revoker _CloseRequestedRevoker;
winrt::TerminalApp::AppLogic::RequestedThemeChanged_revoker _RequestedThemeChangedRevoker;
winrt::TerminalApp::AppLogic::FullscreenChanged_revoker _FullscreenChangedRevoker;
winrt::TerminalApp::AppLogic::FocusModeChanged_revoker _FocusModeChangedRevoker;
winrt::TerminalApp::AppLogic::AlwaysOnTopChanged_revoker _AlwaysOnTopChangedRevoker;
winrt::TerminalApp::AppLogic::RaiseVisualBell_revoker _RaiseVisualBellRevoker;
winrt::TerminalApp::AppLogic::SystemMenuChangeRequested_revoker _SystemMenuChangeRequestedRevoker;
winrt::TerminalApp::AppLogic::ChangeMaximizeRequested_revoker _ChangeMaximizeRequestedRevoker;
winrt::TerminalApp::AppLogic::TitleChanged_revoker _TitleChangedRevoker;
winrt::TerminalApp::AppLogic::LastTabClosed_revoker _LastTabClosedRevoker;
winrt::TerminalApp::AppLogic::SetTaskbarProgress_revoker _SetTaskbarProgressRevoker;
winrt::TerminalApp::AppLogic::IdentifyWindowsRequested_revoker _IdentifyWindowsRequestedRevoker;
winrt::TerminalApp::AppLogic::RenameWindowRequested_revoker _RenameWindowRequestedRevoker;
winrt::TerminalApp::AppLogic::SettingsChanged_revoker _SettingsChangedRevoker;
winrt::TerminalApp::AppLogic::IsQuakeWindowChanged_revoker _IsQuakeWindowChangedRevoker;
winrt::TerminalApp::AppLogic::SummonWindowRequested_revoker _SummonWindowRequestedRevoker;
winrt::TerminalApp::AppLogic::OpenSystemMenu_revoker _OpenSystemMenuRevoker;
winrt::TerminalApp::AppLogic::QuitRequested_revoker _QuitRequestedRevoker;
winrt::Microsoft::Terminal::Remoting::WindowManager::ShowNotificationIconRequested_revoker _ShowNotificationIconRequestedRevoker;
winrt::Microsoft::Terminal::Remoting::WindowManager::HideNotificationIconRequested_revoker _HideNotificationIconRequestedRevoker;
winrt::Microsoft::Terminal::Remoting::WindowManager::QuitAllRequested_revoker _QuitAllRequestedRevoker;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woof. This is going to be the next candidate to be put into the macros.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quite nearly had a std::deque<> of event revokers that I just pushed them all into, then pop'd and revoked, but alas, I'm not lucky enough to have them all share some common base class ;____;

Copy link
Member

@miniksa miniksa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good with this.

If you can imagine a way that we could remove this foot-gun with further X-macros... please file that follow-on (aka automate the creation of the revokers and the revocations).

Otherwise just get your typo and let's get this on its way to merging in.

@zadjii-msft zadjii-msft added the AutoMerge Marked for automatic merge by the bot when requirements are met label Mar 1, 2022
@ghost
Copy link

ghost commented Mar 1, 2022

Hello @zadjii-msft!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@zadjii-msft zadjii-msft removed the AutoMerge Marked for automatic merge by the bot when requirements are met label Mar 1, 2022
@zadjii-msft zadjii-msft added the AutoMerge Marked for automatic merge by the bot when requirements are met label Mar 1, 2022
@ghost ghost merged commit 8962c75 into main Mar 1, 2022
@ghost ghost deleted the dev/migrie/b/36797001-register-hotkey-crash branch March 1, 2022 18:50
@miniksa miniksa removed the zStable-Service-Queued-1.12 A floating label that tracks the current Stable version for servicing purposes. label Mar 2, 2022
miniksa pushed a commit that referenced this pull request Mar 2, 2022
From MSFT:36797001. Okay so this is only .22% of our crashes, but every little bit helps, right?

Turns out this is also hitting in:
* MSFT:35726322
* MSFT:34662459

and together they're a fairly hot bug.

There's a large class of bugs where we might get a callback to one of our event handlers when we call `app.Close()` in the `AppHost` dtor. This PR adds manual revokers to these events, and makes sure to revoke them BEFORE nulling out the `_window`. That will prevent callbacks during the rest of the dtor, when the `_window` is null.
DHowett pushed a commit that referenced this pull request Mar 10, 2022
From MSFT:36797001. Okay so this is only .22% of our crashes, but every little bit helps, right?

Turns out this is also hitting in:
* MSFT:35726322
* MSFT:34662459

and together they're a fairly hot bug.

There's a large class of bugs where we might get a callback to one of our event handlers when we call `app.Close()` in the `AppHost` dtor. This PR adds manual revokers to these events, and makes sure to revoke them BEFORE nulling out the `_window`. That will prevent callbacks during the rest of the dtor, when the `_window` is null.

(cherry picked from commit 8962c75)
@ghost
Copy link

ghost commented Mar 25, 2022

🎉Windows Terminal v1.12.1073 has been released which incorporates this pull request.:tada:

Handy links:

@ghost
Copy link

ghost commented Mar 25, 2022

🎉Windows Terminal Preview v1.13.1073 has been released which incorporates this pull request.:tada:

Handy links:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoMerge Marked for automatic merge by the bot when requirements are met Severity-Crash Crashes are real bad news. zPreview-Service-Queued-1.13 A floating label that tracks the current Preview version for servicing purposes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants