Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutex to keyEvents in TermControlAutomationPeer #14694

Merged
1 commit merged into from
Jan 18, 2023

Conversation

carlos-zamora
Copy link
Member

Some investigation revealed that _keyEvents would get a NULL_POINTER_READ error. On the main thread, RecordKeyEvent() would be called, which mainly updates _keyEvents. On the renderer thread, NotifyNewOutput() would be called, which starts by iterating through _keyEvents and slowly clearing it out. On occasion, these two threads are modifying _keyEvents simultaneously, causing a crash.

The fix is to add a mutex on this variable.

Closes #14592

@carlos-zamora carlos-zamora requested a review from lhecker January 17, 2023 23:27
@ghost ghost added Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Product-Terminal The new Windows Terminal. Severity-Crash Crashes are real bad news. labels Jan 17, 2023
@carlos-zamora carlos-zamora added this to the Terminal v1.17 milestone Jan 17, 2023
@@ -112,7 +112,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
if (const auto keyEventChar{ gsl::narrow_cast<wchar_t>(charCode) }; IsReadable({ &keyEventChar, 1 }))
{
_keyEvents.emplace_back(keyEventChar);
_keyEvents.lock()->emplace_back(keyEventChar);
Copy link
Member

Choose a reason for hiding this comment

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

@lhecker this is an acceptable use of lock(), right?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah the guard should get destroyed at ; (= after the insertion finished).

_keyEvents.pop_front();
}
else
auto keyEvents = _keyEvents.lock();
Copy link
Member

Choose a reason for hiding this comment

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

you might need to make this auto& or even (C++14) decltype(auto). I think that auto keyEvents will result in a copy of keyEvents (thanks to how auto works).

image

image

See how the first one compiles down to escape(9) even though we just set the value to 7 through auto m!

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Oh, but this returns a proxy object... so really it doesn't matter. You're actually using a guard<deque>, which is fine. Ignore me! You have not been bitten by C++; in fact, I have!

_keyEvents.pop_front();
}
else
auto keyEvents = _keyEvents.lock();
Copy link
Member

Choose a reason for hiding this comment

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

@ghost ghost added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Jan 17, 2023
@DHowett DHowett added the AutoMerge Marked for automatic merge by the bot when requirements are met label Jan 17, 2023
@ghost
Copy link

ghost commented Jan 17, 2023

Hello @DHowett!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

Do note that I've been instructed to only help merge pull requests of this repository that have been opened for at least 8 hours, a condition that will be fulfilled in about 7 hours 43 minutes. No worries though, I will be back when the time is right! 😉

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 8e04169 into main Jan 18, 2023
@ghost ghost deleted the dev/cazamor/key-events-race branch January 18, 2023 07:28
DHowett pushed a commit that referenced this pull request Jan 20, 2023
Some investigation revealed that `_keyEvents` would get a `NULL_POINTER_READ` error. On the main thread, `RecordKeyEvent()` would be called, which mainly updates `_keyEvents`. On the renderer thread, `NotifyNewOutput()` would be called, which starts by iterating through `_keyEvents` and slowly clearing it out. On occasion, these two threads are modifying `_keyEvents` simultaneously, causing a crash.

The fix is to add a mutex on this variable.

Closes #14592

(cherry picked from commit 8e04169)
Service-Card-Id: 87649541
Service-Version: 1.16
@ghost
Copy link

ghost commented Jan 24, 2023

🎉Windows Terminal v1.16.1023 (10231 and 10232) has been released which incorporates this pull request.:tada:

Handy links:

@ghost
Copy link

ghost commented Jan 24, 2023

🎉Windows Terminal Preview v1.17.1023 has been released which incorporates this pull request.:tada:

Handy links:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something Product-Terminal The new Windows Terminal. Severity-Crash Crashes are real bad news.
Projects
None yet
3 participants