Powershell Wait-Job does not return reliably

  Kiến thức lập trình

from a Jenkins Pipeline we call a powershell script that calls a tool to execute some tests. We want the pipeline to wait for the test execution being finished. As a fallback in case of any issues we define a maximum test execution time as a timeout for the job. Usually this works fine. In some occassions, however, the Wait-Job does not return before the timeout, although we can see on the VM where the process is executed, that the tool process exited. All test results are there, no exceptions visible. We have a clean test run but the job seems to be hanging somehow.

That’s part of the script we use:

$code = {
        param($toolPath,$configfile,$workdir)

        & $toolPath -ConfigurationFile $configfile
        $ExitCodeInScript=$LASTEXITCODE
        return $ExitCodeInScript
    }

$j = Start-Job -ScriptBlock $code -ArgumentList "$Path","$configurationfile","$WorkDir"
    if (Wait-Job $j -Timeout $TimeoutSeconds) {
        # If the timeout does not hit, we analyse the return values of the script block here
        $jobOutput = Receive-Job $j
        foreach ($line in $jobOutput) {
            Write-Host "$line"
        }
        # Get the exit code
        # $jobOutput is a array of strings that contains all lines written to stdout
        # and as last element the exitcode
        $ExitCode = @($jobOutput)[-1]
    } else {
        # Timeout hit. We set our own exit code`
        # This is where we always land if the process does not return
}

We use Powershell v5.1
PSVersion 5.1.19041.3031

  • Checked the processes on the VM and found that the test tool has exited and results are complete
  • We could see some powershell processes still active, we could not relate to what is executed there
  • Checked windows eventlog and couldn’t see any issues

Any ideas how we can go on analyzing this issue? I cannot see a clear pattern. It happens in different pipelines on different agents.

Is there any approach to make this more reliable or even a completely different way of implementing a call tool, wait for exit in powershell?

New contributor

MDukeR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT