Skip to content

Commit 26cd15a

Browse files
authored
Eagerly persist on WM_ENDSESSION (#18912)
Once an application has returned from handling `WM_ENDSESSION` the OS may kill the app at any point. This means we must persist our state while inside the message handler. Closes #17179 ## Validation Steps Performed Honestly, none. It's a race condition in the first place.
1 parent a093ca3 commit 26cd15a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/cascadia/WindowsTerminal/WindowEmperor.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,8 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
899899
RegisterApplicationRestart(nullptr, RESTART_NO_CRASH | RESTART_NO_HANG);
900900
return TRUE;
901901
case WM_ENDSESSION:
902-
_forcePersistence = true;
902+
_finalizeSessionPersistence();
903+
_skipPersistence = true;
903904
PostQuitMessage(0);
904905
return 0;
905906
default:
@@ -946,7 +947,7 @@ void WindowEmperor::_persistState(const ApplicationState& state, bool serializeB
946947
state.PersistedWindowLayouts(nullptr);
947948
}
948949

949-
if (_forcePersistence || _app.Logic().Settings().GlobalSettings().ShouldUsePersistedLayout())
950+
if (_app.Logic().Settings().GlobalSettings().ShouldUsePersistedLayout())
950951
{
951952
for (const auto& w : _windows)
952953
{
@@ -960,6 +961,13 @@ void WindowEmperor::_persistState(const ApplicationState& state, bool serializeB
960961

961962
void WindowEmperor::_finalizeSessionPersistence() const
962963
{
964+
if (_skipPersistence)
965+
{
966+
// We received WM_ENDSESSION and persisted the state.
967+
// We don't need to persist it again.
968+
return;
969+
}
970+
963971
const auto state = ApplicationState::SharedInstance();
964972

965973
_persistState(state, true);

src/cascadia/WindowsTerminal/WindowEmperor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class WindowEmperor
7777
UINT WM_TASKBARCREATED = 0;
7878
HMENU _currentWindowMenu = nullptr;
7979
bool _notificationIconShown = false;
80-
bool _forcePersistence = false;
80+
bool _skipPersistence = false;
8181
bool _needsPersistenceCleanup = false;
8282
SafeDispatcherTimer _persistStateTimer;
8383
std::optional<bool> _currentSystemThemeIsDark;

0 commit comments

Comments
 (0)