Skip to content

Commit

Permalink
Fix VT parser memory leak in tracing (#8618)
Browse files Browse the repository at this point in the history
Fix memory leak that occurs from not dispatching the end of sequences on all actions (since it is buffering up all characters for trace reasons.) Also don't bother storing if no one is listening.

## PR Checklist
- [x] Closes #8283
* [x] Fixes leak found while bumbling around.
* [x] I work here.

## Detailed Description of the Pull Request / Additional comments
- We trace all the things leading up to the Action phase in the VT parser for ETW tracing to make debugging the parser easier, but we made two mistakes.
- At some point, three of the actions (related to print/execute) weren't dispatching the stored up sequence to tracing and not clearing it. So printing/executing in a giant run over and over caused the vector to bloat and bloat and bloat forever.
- We're storing things even when no one is listening. That's a waste.

## Validation Steps Performed
- Watched it grow every time I did `type big.txt` under `taskman.exe`. Then watched it not do that after.
- I did technically WPR it to figure out this was the culprit.
  • Loading branch information
miniksa authored Jan 4, 2021
1 parent 0b0161d commit 5220738
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/terminal/parser/stateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ static constexpr bool _isActionableFromGround(const wchar_t wch) noexcept
void StateMachine::_ActionExecute(const wchar_t wch)
{
_trace.TraceOnExecute(wch);
_engine->ActionExecute(wch);
const bool success = _engine->ActionExecute(wch);

// Trace the result.
_trace.DispatchSequenceTrace(success);
}

// Routine Description:
Expand All @@ -397,7 +400,11 @@ void StateMachine::_ActionExecute(const wchar_t wch)
void StateMachine::_ActionExecuteFromEscape(const wchar_t wch)
{
_trace.TraceOnExecuteFromEscape(wch);
_engine->ActionExecuteFromEscape(wch);

const bool success = _engine->ActionExecuteFromEscape(wch);

// Trace the result.
_trace.DispatchSequenceTrace(success);
}

// Routine Description:
Expand All @@ -409,7 +416,11 @@ void StateMachine::_ActionExecuteFromEscape(const wchar_t wch)
void StateMachine::_ActionPrint(const wchar_t wch)
{
_trace.TraceOnAction(L"Print");
_engine->ActionPrint(wch);

const bool success = _engine->ActionPrint(wch);

// Trace the result.
_trace.DispatchSequenceTrace(success);
}

// Routine Description:
Expand Down
6 changes: 5 additions & 1 deletion src/terminal/parser/tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ void ParserTracing::TraceCharInput(const wchar_t wch)

void ParserTracing::AddSequenceTrace(const wchar_t wch)
{
_sequenceTrace.push_back(wch);
// Don't waste time storing this if no one is listening.
if (TraceLoggingProviderEnabled(g_hConsoleVirtTermParserEventTraceProvider, WINEVENT_LEVEL_VERBOSE, 0))
{
_sequenceTrace.push_back(wch);
}
}

void ParserTracing::DispatchSequenceTrace(const bool fSuccess) noexcept
Expand Down

0 comments on commit 5220738

Please sign in to comment.