question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSNotApplyErrorActionToStderr does not work as expected with ForEach-Object -Parallel

See original GitHub issue

Steps to reproduce

Enable PSNotApplyErrorActionToStderr - no longer needed

$ErrorActionPreference = 'Stop'
@(1,2,3) | ForEach-Object -Parallel { cmd.exe /c "echo $_ >&2" }

Expected behavior

Process all records, with no error records produced:

1
2
3

Actual behavior

Stops after first record, produces an error record for native command stderr:

1 # in red

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 10.0.20206
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aelijcommented, Jan 13, 2021

@PaulHigin I’ve investigated it a bit, and it doesn’t seem that the issue is about experimental features at all:

1 | ForEach-Object -Parallel { Get-ExperimentalFeature PSNotApplyErrorActionToStderr }

yields:

Name                                Enabled Source                              Description
----                                ------- ------                              -----------
PSNotApplyErrorActionToStderr          True PSEngine                            Don't have $ErrorActionPreference affect stderr output

I think the problem is in NativeCommandProcessor and how it handles output from background processes. So it should work regardless of the suggested -UseCurrentState.

This code gets executed only in parallel:

https://github.com/PowerShell/PowerShell/blob/4a09585fe72c3679e23a1aa047b23ac827609c55/src/System.Management.Automation/engine/NativeCommandProcessor.cs#L657-L673

So please reopen as it’s definitely unrelated to the other issue.

0reactions
msftbot[bot]commented, Jan 14, 2021

This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Powershell 7 -> ForEach -Parallel in a Function does not ...
1 Answer. The problem here is that threads running in parallel have to alter the same array. There are methods to make that...
Read more >
PowerShell ForEach-Object Parallel Feature
Here script is run on the local machine within the same process but on separate threads. This new feature uses the third method...
Read more >
ForEach-Object - Module: Microsoft.PowerShell.Core
The ForEach-Object cmdlet performs an operation on each item in a collection of input objects. The input objects can be piped to the...
Read more >
Foreach vs. ForEach-Object -Parallel : r/PowerShell
Hi Guy's I have been searching high and low on this seemingly very simple Foreach loop I have been playing with PowerShell 7...
Read more >
Error with foreach-object -parallel : r/PowerShell
I think the problem is that the -parallel keyword makes everything inside the loop {} run in a different parallel universe where they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found