Detached powershell locks Agent directory

A small post about a problem I ran into the other day on an on-premise TFS.

Ever since the new agentless phase, you have the possibility to release your agent to do new work as opposed to defining new environments with specific approvers. In the agentless phase, I use the manual intervention to wait for approval to pick up the rest of the environment. The scenario in which I use this is to start distributed Tosca UI tests and when they finish the Powershell script will call the intervention to continue and upload the test results. I will write a full blog post on this topic in the near future.

Keep in mind that the agent can be used for other releases and it’s not guaranteed to give the same agent when your agent phases continuous. To release the pipeline you need to make sure that the actual process isn’t running and so is disconnected. You can do this by starting a new PowerShell reference to the script you want to run. Your new session will run your script while the old session finishes and returns to the next step in the pipeline.

$argumentList = "-file `"$PSscriptRoot\RunTosca.ps1`"", `
                "-releaseid $ReleaseId"
if ($PersonalAccessToken)
{
    $argumentList += "-PersonalAccessToken $PersonalAccessToken"
}
$argumentList += "-OutputFile `"$OutputFile`"", `
                 "-CITestExecutionConfigurationFile `"$CITestExecutionConfigurationFile`"", `
                 "-ManagerServiceEndpoint $ManagerServiceEndpoint", `
                 "-ToscaCIClient `"$ToscaCIClient`"", `
                 "-ApprovalType $ApprovalType"
if ($ContinuousTosca){
    $argumentList += "-ContinuousTosca $([int]$ContinuousTosca)"
}

Write-Output $argumentList

#start a new powershell process, so the main will continue => start runtosca.ps1 with arguments
Start-Process powershell -ArgumentList $argumentList

This will work until you release a second deployment on the same agent while the process is still running in the back. You will notice that the Release artifact directory is locked.

Failed to create Release artifact directory F:\Agents\1\ws\r2\a with an exception 
Microsoft.VisualStudio.Services.Agent.Worker.Release.Artifacts.ArtifactDirectoryCreationFailedException: 
Failed to create Release artifact directory 'F:\Agents\1\ws\r2\a'. ---> System.IO.IOException: 
The process cannot access the file '\\?\F:\Agents\1\ws\r2\a' because it is being used by another process.

To resolve this you need to change the working directory Start-Process powershell -ArgumentList $argumentList -WorkingDirectory C:\TEMP . This will release the artifact directory and no more unintended locking will occur.

I hope this saves you some time and a headache!

Share

You may also like...

Leave a Reply