Work around a compiler bug w/ coroutines and exceptions (#9893)

There is a bug in the compiler that we trip over when we handle the
exception generated by Package::Current inside a coroutine. It appears
to destruct an invalid instance of winrt::factory_guard_count.

Learned from the compiler folks: "coroutine frame pointer wasn't being
stored ... properly".

Fixes #9821
This commit is contained in:
Dustin L. Howett 2021-04-19 13:27:30 -07:00 committed by GitHub
parent 7e2a0e193a
commit 21b2e01643
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -919,12 +919,32 @@ namespace winrt::TerminalApp::implementation
_ApplyTheme(_settings.GlobalSettings().Theme());
}
// Function Description:
// Returns the current app package or nullptr.
// TRANSITIONAL
// Exists to work around a compiler bug. This function encapsulates the
// exception handling that we used to keep around calls to Package::Current,
// so that when it's called inside a coroutine and fails it doesn't explode
// terribly.
static winrt::Windows::ApplicationModel::Package GetCurrentPackageNoThrow() noexcept
{
try
{
return winrt::Windows::ApplicationModel::Package::Current();
}
catch (...)
{
// discard any exception -- literally pretend we're not in a package
}
return nullptr;
}
fire_and_forget AppLogic::_ApplyStartupTaskStateChange()
try
{
// First, make sure we're running in a packaged context. This method
// won't work, and will crash mysteriously if we're running unpackaged.
const auto package{ winrt::Windows::ApplicationModel::Package::Current() };
const auto package{ GetCurrentPackageNoThrow() };
if (package == nullptr)
{
return;