System.UnauthorizedAccessException: Access to the Path is Denied During Azure Pipeline Execution

Copper Contributor

Hello,

I'm experiencing an issue with our Azure DevOps pipeline running on a self-hosted agent. During the initial execution, the pipeline fails with the following error message:

System.UnauthorizedAccessException: Access to the path 'D:\path\to\file.dll' is denied.

The error occurs during built-in tasks like Initialize Job and Checkout. However, when I retrigger the pipeline, it passes successfully.

Details:

  1. When the error occurs, the DLL file mentioned in the error is locked, and attempting to delete it manually results in a prompt indicating that the DLL is in use by another process.
  2. To mitigate this, I created a PowerShell script that restarts explorer.exe every 5 minutes. This allows the pipeline to pass on retriggering.
  3. The issue persists during the initial run of the pipeline, which is causing disruptions.

Questions:

  1. Why does this error only occur during the first execution of the pipeline, and why is it resolved upon retriggering?
  2. Could this be related to file lock management by the self-hosted agent, or interference from explorer.exe or other processes?
  3. What would be the best approach to avoid this issue altogether, without having to rely on restarting explorer.exe?

Any guidance or recommendations to resolve this issue would be greatly appreciated.

Environment Details:

  • Agent Type: Self-hosted
  • OS: windows
1 Reply

@sisirasaru-abey 

These steps should help prevent the access issue from recurring.

1. Why does this error only occur during the first execution of the pipeline, and why is it resolved upon retriggering?

The error happens on the first run because the DLL might be locked by another process. By the time you retrigger the pipeline, that process has released the lock, allowing the second attempt to succeed.

2. Could this be related to file lock management by the self-hosted agent, or interference from explorer.exe or other processes?

Yes, it’s probably due to file locks. The self-hosted agent may be trying to access the DLL while another application is still using it. Background processes, including explorer.exe, could also interfere.

3. What would be the best approach to avoid this issue altogether, without relying on restarting explorer.exe?

To avoid this issue:

  • Add Retry Logic: Implement retries in your pipeline if it encounters a lock.

  • Pre-Execution Check: Check if the DLL is locked before running tasks. If it is, wait a moment and try again.

  • Optimize Agent Setup: Ensure the agent runs under a dedicated user with proper permissions.

  • Identify Locking Processes: Use tools like Process Explorer to find what’s locking the DLL.

Reference URL:

https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=azure-devops

CreateFileA function (fileapi.h) - Win32 apps | Microsoft Learn