Resolve the TSF InputSite HWND within the owning Terminal window #20913
<!-- AzurePipelinesStatusComment -->
<samp>
Azure Pipelines:<br>
There may be pipelines that require an authorized user to comment /azp run to run.<br>
</samp>
@microsoft-github-policy-service agree
The spelling check failed during dependency installation due to a DNS resolution error for azure.archive.ubuntu.com, before spelling validation ran. Could a maintainer please rerun the failed job?
Failed job: https://github.com/microsoft/terminal/actions/runs/43042027734/job/130596059108
Thank you for making this PR. But it's incomplete:
It states that FindWindowOfActiveTSF is wrong and yet it still exists
It also states that it may not be cached but not why it should not be cached. _tsfDataProvider is per pane and the only bug I see is that we don't reset _tsfDataProvider._hwnd when the tab gets dragged.
Thanks for the review. My earlier explanation was too broad: the issue is which window's HWND gets cached, rather than caching itself. I've revised the change to address both points:
FindWindowOfActiveTSF, including its declarations and the Handle wrapper. This also removes the recursion guard from #18248, which is no longer needed since GetHwnd() no longer calls into TSF.
TermControl through NewControlByAttachingContent, rather than moving the existing control. A control's owning window therefore never changes, so tab moves cannot leave its cached HWND stale. When no InputSite window is found, the lookup returns the owner HWND without caching it, allowing a later call to retry.
The underlying problem is that all Terminal windows share one UI thread and ITfThreadMgr. EnumDocumentMgrs enumerates document managers in creation order rather than focus order, so the previous implementation could select and cache another window's InputSite HWND. This reproduces with multiple windows open, without any tab dragging.
For validation, I used automated UI tests (AutoHotkey) with eight windows open simultaneously, checking whether the Hangul/English toggle took effect:
main: the toggle took effect in 20/20 per-tab checks and 30/30 randomized focus-switch checks.
These results are limited to the tested environment and automated input sequence.
@96-kdh We do not permit LLM-authored comments in our repository, and I'll not read what you wrote out of principle. We're basically down to ~1 maintainer for this project and have little time to read large amounts of text, as typically produced by LLMs. You may use LLMs for translating your comments, however.
Seems like you may want to return nullptr instead. The comparison against owner is not obvious without reading FindInputSiteWindow's implementation.
Ah I see now, your theory is that EnumDocumentMgrs may return any windows' input site.
My assumption was that focusing a window enables the current windows' input site _first_ and only then raises the XAML focus event, and only _then_ we enter this function after all - by this time, FindWindowOfActiveTSF would thus return the input site HWND.
Done in 0cb4382cf.
FindWindowOfActiveTSF takes only the first entry from EnumDocumentMgrs via Next(1, …), without checking whether it belongs to the focused window. Even with the focus sequence you described, that first entry is not necessarily associated with the current window.
If the first lookup selects another window's InputSite, that HWND remains cached for subsequent calls.
In the tests, failures clustered on the same tabs across clicks, window activation, and tab switches, though those tabs sometimes succeeded.
lhecker
96-kdh
Summary of the Pull Request
Resolve the TSF InputSite HWND within the control's owning window instead of caching a HWND obtained from the thread's document-manager enumeration. With multiple Terminal windows sharing a UI thread, the cached handle can belong to another window, leaving Korean input toggles ineffective after window reactivation.
References and Relevant Issues
Closes #20912.
Draft for discussion of the proposed approach. The issue has been filed; maintainer agreement on the approach has not yet been obtained.
Detailed Description of the Pull Request / Additional comments
Search for the InputSite descendant under
OwningHwnd(). If it does not exist yet, return the owner and search again on the next call. A null owner returns null. Remove the HWND cache so owner changes and InputSite recreation do not retain stale handles or borrow another window's TSF context.Five tests use real hidden Win32 windows to cover separate owners, null owners, delayed creation, nested descendants/reparenting, and destruction/recreation. These exercise the helper rather than the complete TSF event sequence.
The cross-window cache mismatch was directly observed in official 1.24.12741.0. The exact initialization ordering that first selects the incorrect handle is not isolated; the visible failure is intermittent. See #20912 for evidence and reproduction details.
AI assistance was used for investigation, implementation, tests and drafting. The submitting contributor personally observed the automated reproduction, checked the results, and read and understood the complete code change.
Validation Steps Performed
Control.Unit.Tests.dll /name:InputSiteWindowTests: 5 passed, 0 failed.Control.Unit.Tests.dll: 34 passed, 0 failed, 0 blocked/skipped/not run.5f7dc283013152f89bfb76950515c33f45157ad7: original implementation failed in 7/20 tabs; proposed implementation failed in 0/20. All 40 cases had complete received-text records and owned focus at every observation. Each variant retained windows in one process, so these counts are not population failure-rate estimates.Microsoft.Terminal.Control.dlldiffered. Actual loaded DLL paths were independently recorded. Both layouts successfully launched PowerShell child processes.PR Checklist