8000 Fix the NREs when writing to console from multiple threads by kborowinski · Pull Request #25440 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Fix the NREs when writing to console from multiple threads #25440

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

Merged
merged 1 commit into from
Apr 25, 2025

Conversation

kborowinski
Copy link
Contributor

PR Summary

Fix #21021

(Reopening this small PR on a new dev branch after closure due to the GitHub issue confirmed by support.)

This PR adds additional locks to prevent NullReferenceException when writing to the console from multiple threads. By ensuring proper synchronization, it improves the stability and reliability of concurrent console output in PowerShell.

PR Context

The issue #21021 was caused by race conditions where multiple threads attempted to write to the console simultaneously, leading to potential NullReferenceExceptions. This PR introduces additional locking mechanisms to prevent such occurrences and ensure thread-safe console access.

I would like to thank @iSazonov for finding the cause and proposing the solution.

Visuals

On the left with the fix, on the right current state:

Animation

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Apr 25, 2025
@kborowinski
Copy link
Contributor Author

@iSazonov Could you re-add the backport consider labels? Or it does not matter.

@iSazonov

This comment was marked as outdated.

This comment was marked as outdated.

@iSazonov iSazonov added WG-Reviewed A Working Group has reviewed this and made a recommendation BackPort-7.4.x-Consider BackPort-7.5.x-Consider labels Apr 25, 2025
@iSazonov iSazonov merged commit 658a8e4 into PowerShell:master Apr 25, 2025
39 of 40 checks passed
@iSazonov
Copy link
Collaborator

@kborowinski Thanks for your contribution!

@TravisEz13
Copy link
Member

@PowerShell/powershell-maintainers triage decision:
For 7.4, I don't think a fix that doesn't affect the actual script results makes the bar for an LTS. The regression risk would be higher than the potential gains here.

@kborowinski
Copy link
Contributor Author

@TravisEz13

I understand the decision not to backport this PR to the LTS version, but I would like to clarify that the actual script results are affected in PS 7.4.X and up.

I first reported this issue on the 7Zip4PowerShell repo, where the unpacking process was crashing mid-task.
I proposed a PR that was merged to implement a workaround that swallows NREs coming from progress pane:

try {
    worker.Progress.StatusDescription = "Finished";
    worker.Progress.RecordType = ProgressRecordType.Completed;
    WriteProgress(worker.Progress);
} catch (NullReferenceException) {
    // Possible bug in PowerShell 7.4.0 leading to a null reference exception being thrown on ProgressPane completion
    // This is not happening on PowerShell 5.1
}

This issue affects all scripts that write to the console from multiple threads, leading to potential crashes in the LTS version of PowerShell. Affected users are forced to disable the progress bar either globally or per script.

pwshBot pushed a commit to pwshBot/PowerShell that referenced this pull request May 3, 2025
…l#25440)

The WriteImpl() method should always be called within a lock on _instanceLock to ensure thread safety.
pwshBot pushed a commit to pwshBot/PowerShell that referenced this pull request May 3, 2025
…l#25440)

The WriteImpl() method should always be called within a lock on _instanceLock to ensure thread safety.
@TravisEz13
Copy link
Member

@kborowinski I had the bot open PRs for the backports and added your comments. These still need to be triaged, but we are moving over to a new process that helps us track all the information we need.

@TravisEz13
Copy link
Member
  1. @kborowinski It would be good if you know exactly when it was introduced, that will help us with our decision.

@kborowinski
Copy link
Contributor Author

@TravisEz13 In comment on the original issue @iSazonov pinpointed it to PR #13758 PowerShell 7.2.0-preview.2 and #15864 PowerShell 7.2.0-preview.9 when PSAnsiRendering was made stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backport-7.4.x-Migrated Backport-7.5.x-Migrated CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log WG-Reviewed A Working Group has reviewed this and made a recommendation
Projects
None yet
3 participants
0