8000 [release/v7.4] Fix the NREs when writing to console from multiple threads by pwshBot · Pull Request #25503 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

[release/v7.4] Fix the NREs when writing to console from multiple threads #25503

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,14 @@ private void WriteLineToConsole()
/// </exception>
public override void Write(string value)
{
WriteImpl(value, newLine: false);
lock (_instanceLock)
{
WriteImpl(value, newLine: false);
}
}

// The WriteImpl() method should always be called within a lock on _instanceLock
// to ensure thread safety and prevent issues in multi-threaded scenarios.
private void WriteImpl(string value, bool newLine)
{
if (string.IsNullOrEmpty(value) && !newLine)
Expand Down Expand Up @@ -845,7 +850,10 @@ private void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, s
/// </exception>
public override void WriteLine(string value)
{
this.WriteImpl(value, newLine: true);
lock (_instanceLock)
{
this.WriteImpl(value, newLine: true);
}
}

/// <summary>
Expand All @@ -862,7 +870,10 @@ public override void WriteLine(string value)
/// </exception>
public override void WriteLine()
{
this.WriteImpl(Environment.NewLine, newLine: false);
lock (_instanceLock)
{
this.WriteImpl(Environment.NewLine, newLine: false);
}
}

#region Word Wrapping
Expand Down
Loading
0