proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
541 stars 87 forks source link

Error streams duplicated in every job #171

Closed codykonior closed 6 years ago

codykonior commented 6 years ago

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Start-RsJob { $ErrorActionPreference = "Stop"; Write-Error "Break me!" }
Start-RsJob { Write-Host "Hi" }
Receive-RSJob -Id 2

Results in

Exception calling "EndInvoke" with "1" argument(s): "The running command stopped because the preference variable 
"ErrorActionPreference" or common parameter is set to Stop: Break me!"

Basically one failed job pollutes the streams of all the others.

Which versions of Powershell and which OS are affected by this issue? Did this work in previous versions of our scripts?

PoshRSJob 1.7.3.11

Name                           Value                                                                            
----                           -----                                                                            
PSVersion                      5.1.14409.1005                                                                   
PSEdition                      Desktop                                                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                          
BuildVersion                   10.0.14409.1005                                                                  
CLRVersion                     4.0.30319.42000                                                                  
WSManStackVersion              3.0                                                                              
PSRemotingProtocolVersion      2.3                                                                              
SerializationVersion           1.1.0.1     
codykonior commented 6 years ago

In PoshRSJob.psm1 there's a "Routine to handle completed runspaces" and that's what seems to extract the error and add it to each job.

But I don't understand why it's dragging them out of other jobs and then appending them to later jobs.

codykonior commented 6 years ago

It's because you check $CaughtErrors and never reset it. Modifying it to this fixes the problem:

$PoshRS_jobCleanup.PowerShell = [PowerShell]::Create().AddScript( {
        #Routine to handle completed runspaces
        #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("Begin Do Loop")
        Do {
            [System.Threading.Monitor]::Enter($PoshRS_Jobs.syncroot)
            try {
                Foreach ($job in $PoshRS_Jobs) {
                    $CaughtErrors = $false
MVKozlov commented 6 years ago

I fixed a similar error for $Data in #122, but did not look at the $errors :)

codykonior commented 6 years ago

I'm not able to reproduce this anymore on the current build.