Repository: microsoft/terminal (language: cpp)

Issue:
1.18 - RACE - Settings can reload while window is being constructed
At least 1x on my dev box, I observed the following:

- Main thread is crashing in TerminalWindow::UpdateSettings because `_root` is `nullptr`, while trying to call `_root->Dispatcher()`.
- The window thread is actively constructing the UI and has not yet assigned `_root`.

Boom


Discussion / hints:
Noted while digging:

`AppLogic` is reaching inside `TerminalWindow` to register `TerminalWindow` for `AppLogic`'s `SettingsChanged` event. It does this quite early (and I was bothered that it happened inside AppLogic?)

Fix patch:
diff --git a/src/cascadia/TerminalApp/AppLogic.cpp b/src/cascadia/TerminalApp/AppLogic.cpp
index 9ffe1421718..a31246a031c 100644
--- a/src/cascadia/TerminalApp/AppLogic.cpp
+++ b/src/cascadia/TerminalApp/AppLogic.cpp
@@ -685,7 +685,6 @@ namespace winrt::TerminalApp::implementation
 
         auto window = winrt::make_self<implementation::TerminalWindow>(*ev, _contentManager);
 
-        this->SettingsChanged({ window->get_weak(), &implementation::TerminalWindow::UpdateSettingsHandler });
         if (_hasSettingsStartupActions)
         {
             window->SetSettingsStartupArgs(_settingsAppArgs.GetStartupActions());
diff --git a/src/cascadia/TerminalApp/TerminalWindow.cpp b/src/cascadia/TerminalApp/TerminalWindow.cpp
index b6efc6db9ed..e62d25c4e99 100644
--- a/src/cascadia/TerminalApp/TerminalWindow.cpp
+++ b/src/cascadia/TerminalApp/TerminalWindow.cpp
@@ -239,6 +239,8 @@ namespace winrt::TerminalApp::implementation
         });
         _root->Create();
 
+        AppLogic::Current()->SettingsChanged({ get_weak(), &TerminalWindow::UpdateSettingsHandler });
+
         _RefreshThemeRoutine();
 
         auto args = winrt::make_self<SystemMenuChangeArgs>(RS_(L"SettingsMenuItem"),


Test patch: