Unable to copy the package to the specified directory 'xxxxx'. One or more files in the directory may be locked by another process

I’m attempting to set-up Octopus Deploy version Octopus Deploy 2.4.5.46 to deploying a web application to six web servers. The deployment stage typically fails on one or more servers with the following message:

Copying package contents to 'c:\CustomDeploymentLocation'
Error    17:04:36Unable to copy the package to the specified directory 'c:\CustomDeploymentLocation'. One or more files in the directory may be locked by another process. You could use a PreDeploy.ps1 script to stop any processes that may be locking the file. Error details follow.
The process cannot access the file 'c:\CustomDeploymentLocation\Web.config' because it is being used by another process.
System.IO.IOException: The process cannot access the file 'c:\CustomDeploymentLocation\Web.config' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
   at Octopus.Platform.Util.OctopusPhysicalFileSystem.CopyFile(String sourceFile, String targetFile, Int32 overwriteFileRetryAttempts) in y:\work\db516cd4dfb6f424\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 342
   at Octopus.Platform.Util.OctopusPhysicalFileSystem.CopyDirectory(String sourceDirectory, String targetDirectory, CancellationToken cancel, Int32 overwriteFileRetryAttempts) in y:\work\db516cd4dfb6f424\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 317
   at Octopus.Tentacle.Procedures.Implementations.Packaging.CopyPackageProcedure.CopyDirectory(ProcedureState state, CancellationToken cancel) in y:\work\db516cd4dfb6f424\source\Octopus.Tentacle\Procedures\Implementations\Packaging\CopyPackageProcedure.cs:line 44
Fatal    17:04:36The process cannot access the file 'c:\CustomDeploymentLocation\Web.config' because it is being used by another process.
System.IO.IOException: The process cannot access the file 'c:\CustomDeploymentLocation\Web.config' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
   at Octopus.Platform.Util.OctopusPhysicalFileSystem.CopyFile(String sourceFile, String targetFile, Int32 overwriteFileRetryAttempts) in y:\work\db516cd4dfb6f424\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 342
   at Octopus.Platform.Util.OctopusPhysicalFileSystem.CopyDirectory(String sourceDirectory, String targetDirectory, CancellationToken cancel, Int32 overwriteFileRetryAttempts) in y:\work\db516cd4dfb6f424\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 317
   at Octopus.Tentacle.Procedures.Implementations.Packaging.CopyPackageProcedure.CopyDirectory(ProcedureState state, CancellationToken cancel) in y:\work\db516cd4dfb6f424\source\Octopus.Tentacle\Procedures\Implementations\Packaging\CopyPackageProcedure.cs:line 49
   at Pipefish.Async.CaptiveThread`1.ThreadAction(Action`2 action, Guid operationId, IActivitySpace space, Guid captiveThreadId) in c:\TeamCity\buildAgent\work\cf0b1f41263b24b9\source\Pipefish\Async\CaptiveThread.cs:line 114

This seems similar to the issue raised [in another thread] (http://help.octopusdeploy.com/discussions/problems/19875-one-or-more-files-in-the-directory-may-be-locked-by-another-process) where the suggested work round is not to use a custom installation folder.

However this isn’t a viable option for us, as we require an specific install folder for our particular web servers.

Hi Peter,

Thanks for getting in touch!

As you are seeing in the error messages you have processes that are locking files and stopping them from being accessed. Our suggestion here would be to run a PowerShell script in your predeploy.ps1 file to kill any processes that are locking the files that need to he accessed.

One example might be to stop the application pool prior to deployment.

You can read about PowerShell and Octopus further here: http://docs.octopusdeploy.com/display/OD/PowerShell+scripts

I hope this helps!
Vanessa

Hello Vanessa

Thanks for your response and suggestion. Unfortunately we already have a PowerShell script in place to stop the website, prior to deploying the package:

`Import-Module WebAdministration

Write-Output "Stopping website"
Stop-WebSite $OctopusParameters[‘WebsiteIISName’]

Write-Output "Starting maintainance website"
Start-WebSite $OctopusParameters[‘MaintainanceWebsiteIISName’]`

We have two separate websites bound to the same domain, one being the actual production website and the other a maintenance website.

The script above runs before the deploy and stops the IIS website we’re deploying to and puts up a temporary maintenance website in its place.

I’ve even added a Start-Sleep -s 10 to the bottom of this script, just in case the process hadn’t fully stopped. However I’m still getting this failure.

Regards
Peter

Hi Peter,

Thanks for your response!

My next suggestion would be after you run the stop command try the following:


It should list the processes that are still running that have the file locked, so they can also be stopped prior to deployment.

Give this a try and let me know how it goes.

Vanessa

Hello Vanessa

Thanks for your suggestion

I found a script at http://ye110wbeard.wordpress.com/2011/02/13/combining-powershell-and-sysinternalsclosing-a-file-handle/ which I’ve adapted into the following and added as a step in my deployment process before the package is deployed:

Write-Output "Using SysInternals handle.exe to close handle for web.config"
$HandleFile = "C:\temp\handle.exe"
$DATA = (& $HandleFile \WebsiteDirectory\web.config)

if ( $DATA[5] -ne "No matching handles found." ){
    $MYPID=($DATA[5].Substring(24,7)).trim() 
    $HEX=($DATA[5].Substring(41,14)).trim() 
    (& $HandleFile –c $HEX –p $MYPID -y) 
    Write-Output "Handle removed"
} else {
    Write-Output "Web.config is not locked"
}

However when I run a deploy it hangs on this step, with no change after 20 minutes until I cancel the deployment.

I can see from the directory that handle.exe is in it’s being run as a handle64.exe file is created.

Any advise or sample code would be appreciated.

Thanks
Peter

Hi Peter,

Sorry for the delay in getting back to you about this.

Handle, the first time that it is accessed, requires a license to be accepted for each of the users accessing it.
A way around this is to add:

reg.exe ADD "HKCU\Software\Sysinternals\Handle" /v EulaAccepted /t REG_DWORD /d 1 /f

To the beginning of your script as the first line, then it will execute the rest of your script.

Hope this helps!

Vanessa

Hello Vanessa

Thanks for your continued support in solving this issue.

I appreciate you providing the script above and can report it’s successfully allowed Octopus Deploy to run handle.exe via PowerShell.

Unfortunately I’m still getting the same original error message reported on the Deploy process step:

`Unable to copy the package to the specified directory 'C:\CustomDeploymentLocation'. One or more files in the directory may be locked by another process. You could use a PreDeploy.ps1 script to stop any processes that may be locking the file. Error details follow.
The process cannot access the file 'C:\CustomDeploymentLocation\Web.config' because it is being used by another process.
System.IO.IOException: The process cannot access the file 'C:\CustomDeploymentLocation\Web.config' because it is being used by another process.`

I’ve run the package twice through on the six production web servers we have. The first time one of the servers failed with the above message. The second time four servers failed with the same message.

Looking at the output of the previous step, which runs handle.exe against CustomDeploymentLocation\Web.config it’s reporting: No matching handles found for all six servers on both deploys.

Is it possible that the Octopus Deploy tentacle itself is locking the file? Given that the IIS website has been stopped and handle is reporting web.config isn’t locked prior to running the deploy step.

Thanks
Peter

Hi Peter,

No problems at all, and I appreciate your patience while we try to resolve this issue.

Alright so another thing id like you to try for me is stopping the whole application pool.
We have a library template for that specific purpose:
http://library.octopusdeploy.com/#!/step-template/actiontemplate-iis-apppool-stop

I know you have mentioned previously about stopping the website, but I think we might just have to try the application pool, and sorry if you have tried this, it just hasn’t been mentioned yet.
We are not yet grasping at straws, but like all debugging gotta give everything a try!

Let me know how that goes!

Vanessa

Hello Vanessa

I’d added the script you’ve referenced, so that the IIS application pool gets stopped in addition to the website.

On my first deploy after making this change the deployment failed on one out of the six servers, with the error message I’ve reported previously.

On a redeploy four of the six web servers failed with the same error message.

I appreciate your time and detailed suggestions in finding the cause of this problem. We’ve invested in Octopus Deploy so I’m committed to get it fully working in our environment.

Thanks
Peter

Hi Peter,

Thanks for getting back and the details about how it is failing.
Our next step to try to figure this out will have to be via Skype.
If you could please use the following link to choose a time that we can connect we will try to get to the bottom of this.
https://octopusdeploy.acuityscheduling.com/schedule.php

Very sorry for the time it is taking to resolve this!

Vanessa

Hello Vanessa

I tried to schedule a call for Wednesday 11th June at 11:00am (BST), however I accidentally selected 11pm instead! I’m no longer able to cancel this appointment through the web interface.

I’ve scheduled another support call for tomorrow (Thursday 12th June at 11:00am).

Sorry for the mistake, hopefully it won’t cause anyone any wasted time.

Peter

Hi Peter,

Thanks for letting us know, no time wasted at all, and the new appointment came through fine :slight_smile:

Vanessa

Hello

I am also facing a very similar issue after upgrading to Octopus 2.4
@@@
Unable to copy the package to the specified directory ‘D:\Websites\XXXX\wwwroot’. One or more files in the directory may be locked by another process. You could use a PreDeploy.ps1 script to stop any processes that may be locking the file. Error details follow.
Access to the path is denied.
System.ArgumentException: Access to the path is denied.
at System.IO.FileSystemInfo.set_Attributes(FileAttributes value)
at Octopus.Platform.Util.OctopusPhysicalFileSystem.DeleteDirectory(String path, DeletionOptions options) in y:\work\refs\heads\release-2.4\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 109
at Octopus.Platform.Util.OctopusPhysicalFileSystem.PurgeDirectory(String targetDirectory, Predicate1 include, DeletionOptions options, CancellationToken cancel, Boolean includeTarget) in y:\work\refs\heads\release-2.4\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 252 at Octopus.Platform.Util.OctopusPhysicalFileSystem.PurgeDirectory(String targetDirectory, Predicate1 include, DeletionOptions options, CancellationToken cancel, Boolean includeTarget) in y:\work\refs\heads\release-2.4\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 247
at Octopus.Platform.Util.OctopusPhysicalFileSystem.PurgeDirectory(String targetDirectory, DeletionOptions options, CancellationToken cancel) in y:\work\refs\heads\release-2.4\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 205
at Octopus.Tentacle.Procedures.Implementations.Packaging.CopyPackageProcedure.CopyDirectory(ProcedureState state, CancellationToken cancel) in y:\work\refs\heads\release-2.4\source\Octopus.Tentacle\Procedures\Implementations\Packaging\CopyPackageProcedure.cs:line 43
Fatal 20:37:34
Access to the path is denied.
System.ArgumentException: Access to the path is denied.
at System.IO.FileSystemInfo.set_Attributes(FileAttributes value)
at Octopus.Platform.Util.OctopusPhysicalFileSystem.DeleteDirectory(String path, DeletionOptions options) in y:\work\refs\heads\release-2.4\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 109
at Octopus.Platform.Util.OctopusPhysicalFileSystem.PurgeDirectory(String targetDirectory, Predicate1 include, DeletionOptions options, CancellationToken cancel, Boolean includeTarget) in y:\work\refs\heads\release-2.4\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 252 at Octopus.Platform.Util.OctopusPhysicalFileSystem.PurgeDirectory(String targetDirectory, Predicate1 include, DeletionOptions options, CancellationToken cancel, Boolean includeTarget) in y:\work\refs\heads\release-2.4\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 247
at Octopus.Platform.Util.OctopusPhysicalFileSystem.PurgeDirectory(String targetDirectory, DeletionOptions options, CancellationToken cancel) in y:\work\refs\heads\release-2.4\source\Octopus.Platform\Util\OctopusPhysicalFileSystem.cs:line 205
at Octopus.Tentacle.Procedures.Implementations.Packaging.CopyPackageProcedure.CopyDirectory(ProcedureState state, CancellationToken cancel) in y:\work\refs\heads\release-2.4\source\Octopus.Tentacle\Procedures\Implementations\Packaging\CopyPackageProcedure.cs:line 49
at Pipefish.Async.CaptiveThread1.ThreadAction(Action2 action, Guid operationId, IActivitySpace space, Guid captiveThreadId) in y:\work\3cbe05672d69a231\source\Pipefish\Async\CaptiveThread.cs:line 114
@@@

I believe it might be related. The deployment fails when it’s triggered from TeamCity however, when I Retry the deployment, it succeeds.

Hi Sayeed,

Thanks for getting in touch! It turned out the issue here that Peter was facing was related to a secondary application on his production servers “New Relic”.
It was locking the files, so the solution was to stop it or remove it from the servers pre-deployment.
If you do not have New Relic or some other type of management/montioring software then could you please send through a full deployment log and also use handle as we suggested to Peter.

Please let me know how that goes!
Vanessa

Hi Vanessa

I have upgraded to Octopus Deploy 2.5.4 yesterday and I am not having this issue anymore. I appears that the issue has been fixed.

We are running into the same issue Peter has referenced and we do have New Relic installed. Can you send me the script you used to stop and start the service?

Thanks,

Chris Rathermel

Hi Chris,

Since our last communication, and the status of the problem Peter faced, he had to uninstall New Relic on those servers, not just stop and start the process.
We did make a change around 2.5 where we do a time-increment-scaling check if the file is still locked, and try again, and it resolved majority of the cases, but still the only 100% solution was uninstalling.

Vanessa