Repository navigation
Fix a crash when focus is lost while text is being output - #21011
Sebastien Tardif (SebTardif) wants to merge 5 commits into
Conversation
UiaEngine::Disable replaces the buffered output string on the UI thread. NotifyNewText appends to that string on the output thread. The output thread holds the terminal lock, and Disable does not, so the two calls race. A 1.25.3131.0 crash dump is an access violation in wstring push_back while the UI thread is inside LostFocus. Guard the buffered string and the enabled flag with a mutex. The new control unit test races Disable and NotifyNewText.
AuditMode treats std::lock_guard as a potentially throwing call inside the noexcept UiaEngine methods (C26447). Take the lock through a noexcept helper with the same suppression used elsewhere in the tree.
Leonard Hecker (lhecker)
left a comment
There was a problem hiding this comment.
Oh, thank you for finding this. However, wouldn't it be simpler to just acquire the console lock before calling Disable()?
The writer thread looped until a flag flipped, so it held the mutex and Disable never ran. arm64 Unit Tests killed it at the 10s class timeout. Both sides now do a fixed number of iterations and yield.
NotifyNewText appends buffered text while the output thread holds the terminal lock. LostFocus and Detach called Disable on the UI thread without that lock, so the string could be replaced mid-append. Disable under LockForWriting from both call sites, including when the engine is removed. Drop the separate UiaEngine mutex.
|
Switched to that. Disable() now runs under LockForWriting, from LostFocus (DisableUiaEngine) and from DetachUiaEngine before the engine is removed. The mutex is gone. CI is green on d5470ef. |
|
I am certain your PR body is out of date. PR bodies are not "write-once" and should be kept up to date as the reality of the PR changes. ;) |
Dustin L. Howett (DHowett)
left a comment
There was a problem hiding this comment.
Thanks, this seems reasonable. Minor nits.
| void ControlCore::DetachUiaEngine(::Microsoft::Console::Render::UiaEngine* const pEngine) | ||
| { | ||
| const auto lock = _terminal->LockForWriting(); | ||
| // NotifyNewText appends on the output thread, which already holds this lock. |
There was a problem hiding this comment.
| // NotifyNewText appends on the output thread, which already holds this lock. |
| HRESULT ControlCore::DisableUiaEngine(::Microsoft::Console::Render::UiaEngine* const pEngine) | ||
| { | ||
| const auto lock = _terminal->LockForWriting(); | ||
| // NotifyNewText appends on the output thread, which already holds this lock. |
There was a problem hiding this comment.
| // NotifyNewText appends on the output thread, which already holds this lock. |
| // possible that it had already started presenting here. | ||
| LOG_IF_FAILED(_uiaEngine->Disable()); | ||
| // DetachUiaEngine disables it under the terminal lock, which the | ||
| // output thread holds while appending buffered text. |
There was a problem hiding this comment.
| // output thread holds while appending buffered text. | |
| // To alleviate, core will both disable the UIA engine and remove it, | |
| // Core.Detach will take care of the TriggerTeardown (which will stop | |
| // the renderer after all current engines are done painting). |
There was a problem hiding this comment.
ugh. no. this is not where I intended to put this comment.
I think we should replace the entire comment starting with "To alleviate" and ending with your AI-generated "while appending buffered text" with the new significantly shorter comment I have here.
Summary of the Pull Request
Losing focus crashes Windows Terminal when a pane is still receiving output. The process dies, so every window and tab goes with it.
References and Relevant Issues
Local crash of package
Microsoft.WindowsTerminal_1.25.3131.0on 2026-12-02:0xC0000005inMicrosoft.Terminal.Control.dllat offset0x624fdstd::wstring::push_backinsideUiaEngine::NotifyNewTextTermControl::_LostFocusHandler->ControlCore::LostFocus, blocked on the terminal lockControlCore::_connectionOutputHandler, which holds that lock and is appending the new textDisable()replaces_newOutputand does not take the terminal lock.NotifyNewTextappends to_newOutputwhile holding it. One thread frees the string while the other grows it.Detailed Description of the Pull Request / Additional comments
UiaEnginenow takes a mutex around_newOutputand_isEnabledinEnable,Disable,NotifyNewText,StartPaint, andEndPaint.Presentholds the mutex only for the enabled check, then raises UIA events after releasing it.Validation Steps Performed
ControlUnitTests::UiaRendererTests::DisableDuringNotifyNewTextracesDisableandNotifyNewText. Without the mutex,TE.exeexits0xC0000374(heap corruption). With the mutex, the test passes.src/cascadia/UnitTests_Control/Control.UnitTests.vcxprojRelease x64.PR Checklist