TFS: 解决The build agent error - the session for this agent already exists


Recently we've been getting the following error for our TFS build agents which caused all our builds to fail

Figure: TFS Builds are failing

The error reported is "The job has been abandoned because agent."

Investigating the _diag folder of the build agent itself reveals the actual error.

The session for this agent already exists. Sleeping for 30 seconds before next retry. Attempts=1/10.
Microsoft.TeamFoundation.DistributedTask.WebApi.TaskAgentSessionConflictException: The task agent Agent-SYDTFSBLDP01-1 already has an active session for owner SYDTFSBLDP01.
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.HandleResponse(HttpResponseMessage response)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.

To fix this issue, you need to re-run the ConfigureAgent.cmd and on the first option "Do you want to also replace the server registration (default is N)" Choose "Y"


PS C:\Agents\2> .\ConfigureAgent.cmd
An existing configuration file was detected.  This will update the local agent settings.  Do you want to also replace th
e server registration (default is N)? Y

This will remove the existing agent registration and clean up the locked "session".