diff --git a/src/tsf/Implementation.cpp b/src/tsf/Implementation.cpp index 234c43df233..f2cd9589336 100644 --- a/src/tsf/Implementation.cpp +++ b/src/tsf/Implementation.cpp @@ -84,8 +84,20 @@ void Implementation::Uninitialize() noexcept } } -HWND Implementation::FindWindowOfActiveTSF() const noexcept -{ +HWND Implementation::FindWindowOfActiveTSF() noexcept +{ + // We don't know what ITfContextOwner we're going to get in + // the code below and it may very well be us (this instance). + // It's also possible that our IDataProvider's GetHwnd() + // implementation calls this FindWindowOfActiveTSF() function. + // This can result in infinite recursion because we're calling + // GetWnd() below, which may call GetHwnd(), which may call + // FindWindowOfActiveTSF(), and so on. + // By temporarily clearing the _provider we fix that flaw. + const auto restore = wil::scope_exit([this, provider = std::move(_provider)]() mutable { + _provider = std::move(provider); + }); + wil::com_ptr enumDocumentMgrs; if (FAILED_LOG(_threadMgrEx->EnumDocumentMgrs(enumDocumentMgrs.addressof()))) { diff --git a/src/tsf/Implementation.h b/src/tsf/Implementation.h index 5bf7251f6a0..fecec35cea4 100644 --- a/src/tsf/Implementation.h +++ b/src/tsf/Implementation.h @@ -22,7 +22,7 @@ namespace Microsoft::Console::TSF void Initialize(); void Uninitialize() noexcept; - HWND FindWindowOfActiveTSF() const noexcept; + HWND FindWindowOfActiveTSF() noexcept; void AssociateFocus(IDataProvider* provider); void Focus(IDataProvider* provider); void Unfocus(IDataProvider* provider);