title: The session for this agent already exists
body: <p>I am using TFS to execute a nightly build that includes several steps that use the <a href="https://www.visualstudio.com/en-us/docs/test/lab-management/test-machines/install-configure-test-agents" rel="noreferrer">TFS Test Agent</a>. I am running the latest version of TFS/Test Agent(2015 - Update 3) and there are no other builds being run at this time. Often(maybe half the time), when the nightly job is run the step "Visual Studio Test Agent Deployment" fails with the following error:</p>
  
  <blockquote>
    <p>The job has been abandoned because agent Agent-XXX did not renew the
    lock. Ensure agent is running, not sleeping, and has not lost
    communication with the service.</p>
  </blockquote>
  
  <p>This is due to the error found in the Test Agent's log file(under _diag):</p>
  
  <blockquote>
    <p>The session for this agent already exists. Sleeping for 30 seconds
    before next retry.</p>
    
    <p>Microsoft.TeamFoundation.DistributedTask.WebApi.TaskAgentSessionConflictException:
    The task agent Agent-XXX already has an active session for owner XXX.</p>
  </blockquote>
  
  <p>This issue is directly referenced <a href="http://ericphan.net/blog/2016/6/10/solving-the-tfs-build-agent-error-the-session-for-this-agent-already-exists" rel="noreferrer">here</a>, and indirectly talked about <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/a188d2cd-3985-4d8a-84e1-723ef49ab314/vnext-build-agent-service-fails-to-start-at-reboot-vsoagentservice?forum=tfsbuild" rel="noreferrer">here</a>.</p>
  
  <p>The solution I've found to this issue is to restart the server that the test agent is running on, this clears any dead sessions, and after the server starts back up, the tests run just fine. I think this is effectively what is being done in the <a href="http://ericphan.net/blog/2016/6/10/solving-the-tfs-build-agent-error-the-session-for-this-agent-already-exists" rel="noreferrer">previously mentioned post</a>. The result of resetting the configs is that the service is restarted.</p>
  
  <p>While being presented as a solution in the linked article, it is only temporary. Even after the server has been restarted and the build runs successfully, the next day the issue will again reappear necessitating manual intervention to get the build to run.</p>
  
  <p>I could schedule a task to reset the service or even restart the server directly before the nightly build is run, but it strikes me as a bandage rather than a fix. Has anyone experienced this issue before, and if so is there any way to prevent it from occurring in the first place?</p>
  
  <h1>Update 1</h1>
  
  <p>I simply set up a build that runs 5 minutes before my main tests that runs a <a href="https://stackoverflow.com/questions/162304/how-do-i-shutdown-restart-logoff-windows-via-a-bat-file">Bat script</a> to restart all my servers hosting my test agents. This is a workaround, but one that seems to resolve the issue. Hopefully someday someone can come up with a better solution than this, but for now, it's how I have to run automated testing in TFS.</p>
  
  <h1>Update 2</h1>
  
  <p>I have three servers now, all three exhibit the same issue, though it is hard to pin down exactly when it occurs. Scaling up the workaround without creating downtime it proving to be quite challenging.</p>
  
  <h1>Update 3</h1>
  
  <p>A better day came, I upgraded TFS to 2018, and the build agent to the latest version, this issue no longer occurs, I think its a bug in the old build agent. I still don't have a solution for the original version of the build agent...</p>