Skip to content

Fix a crash when focus is lost while text is being output - #21011

Open
Sebastien Tardif (SebTardif) wants to merge 5 commits into
microsoft:mainfrom
SebTardif:fix/uia-focus-output-race
Open

Sebastien Tardif (SebTardif) wants to merge 5 commits into
microsoft:mainfrom
SebTardif:fix/uia-focus-output-race

Conversation

@SebTardif

Copy link
Copy Markdown
Contributor

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.0 on 2026-12-02:

  • Exception 0xC0000005 in Microsoft.Terminal.Control.dll at offset 0x624fd
  • Faulting function: std::wstring::push_back inside UiaEngine::NotifyNewText
  • The UI thread is in TermControl::_LostFocusHandler -> ControlCore::LostFocus, blocked on the terminal lock
  • The output thread is in ControlCore::_connectionOutputHandler, which holds that lock and is appending the new text

Disable() replaces _newOutput and does not take the terminal lock. NotifyNewText appends to _newOutput while holding it. One thread frees the string while the other grows it.

Detailed Description of the Pull Request / Additional comments

UiaEngine now takes a mutex around _newOutput and _isEnabled in Enable, Disable, NotifyNewText, StartPaint, and EndPaint. Present holds the mutex only for the enabled check, then raises UIA events after releasing it.

Validation Steps Performed

  • ControlUnitTests::UiaRendererTests::DisableDuringNotifyNewText races Disable and NotifyNewText. Without the mutex, TE.exe exits 0xC0000374 (heap corruption). With the mutex, the test passes.
  • Built src/cascadia/UnitTests_Control/Control.UnitTests.vcxproj Release x64.

PR Checklist

  • Closes #xxx
  • Tests added/passed
  • Documentation updated
  • Schema updated (if necessary)

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.

@lhecker Leonard Hecker (lhecker) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@SebTardif

Copy link
Copy Markdown
Contributor Author

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.

@DHowett

Copy link
Copy Markdown
Member

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. ;)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// NotifyNewText appends on the output thread, which already holds this lock.

Comment on lines +79 to +87
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants