How to fix Azure deployment issue: Web Deploy cannot modify the file on the destination because it is locked by an external process.

ryan.saul's Avatar

ryan.saul

Dec 13, 2017 @ 07:13 PM

I realize this is an Azure issue, but I am not sure the best way to go about fixing it within AppVeyor. I believe I either want the Azure service to restart, or to run a script that checks when the DLL is released.

Here is the full error I am getting on Deployment, this happens semi-regularly as we have multiple devs pushing code and so it naturally runs into some locking:
```
Web Deploy cannot modify the file 'Deploy.dll' on the destination because it is locked by an external process. In order to allow the publish operation to succeed, you may need to either restart your application to release the lock, or use the AppOffline rule handler for .Net applications on your next publish attempt. Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_FILE_IN_USE.
```

I have read some solutions that recommend using a `before-deploy.ps1` script to check that all files are released before starting to deploy. Something like this: https://gist.github.com/IlyaFinkelshteyn/6ec4c814b81b16363254d860fcaac3f9
The problem I have had with that solution is that I cannot tell if this script is running or not, I have before-deploy.ps1 on my root directory, as suggested here: https://www.appveyor.com/docs/deployment/agent/#running-powershell-scripts-on-target-server-during-deployment
But when deployment happens I don't see this script being run anywhere in AppVeyor. How do I get this file to run?

Another way to fix the problem might be getting the Azure service to restart before deployment. Is there an easy way to do that from AppVeyor?

Help and guidance would be greatly appreciated on this one, thank you!
Ryan

  1. 1 Posted by Ilya Finkelshte... on Dec 13, 2017 @ 08:21 PM

    Ilya Finkelshteyn's Avatar

    I am not sure I fully understand the scenario. Are you deploying to Azure App Service or Azure VM? Are you using Web Deploy provider or Deployment Agent + IIS setting?

    If you are not using Deployment Agent, then scripts you mentioned cannot be executed, and this is most probably your case because I believe you are deploying to Azure App Service and with Web Deploy provider.

    In any scenario (both WebDeploy provider and Deployment agent), app_offline setting is available and should help. This exactly adds AppOffline rule handler for .Net applications error message suggests.

    I assume your app is .NET one. If not, please provide your build configuration in YAML format for us to better understand.

  2. 2 Posted by ryan.saul on Dec 13, 2017 @ 08:28 PM

    ryan.saul's Avatar

    Yes, we are deploying to Azure Web App service using Web Deploy. We also have a .Net Core application that is being deployed. I am using msDeploy.axd in the publish site string to deploy the artifacts.

    Could you explain the recommended setting `app_offline` further? I am using AppVeyor's website to do the setup for our environment and "Take ASP.NET application offline during deployment" is checked ON right now, is that the same thing as `app_offline`?

  3. 3 Posted by Ilya Finkelshte... on Dec 13, 2017 @ 09:14 PM

    Ilya Finkelshteyn's Avatar

    Yes, app_offline setting is exactly the same Take ASP.NET application offline during deployment if you use UI and AppVeyor sets WebDeploy do the same as this.

    That should be enough to avoid that kind of issues but apparently it still does not always works in your case. Are those rare issues can be correlated with virtually simultaneous commits and builds?

    Also can you open https://your_site_name.scm.azurewebsites.net/ProcessExplorer, press Find Handle, enter Deploy.dll and check if it normally used by your website worker process?

  4. 4 Posted by ryan.saul on Dec 13, 2017 @ 09:20 PM

    ryan.saul's Avatar

    This is what I get back when I do that:
    dotnet.exe:14556 -> D:\home\site\wwwroot\Deploy.dll

    So from my history I can see that the last time we ran into this error, it was the only deployment being run. The previous deployment was about 2 hours prior. So I don't believe it is a problem with simultaneous deployments.

  5. 5 Posted by Ilya Finkelshte... on Dec 13, 2017 @ 11:24 PM

    Ilya Finkelshteyn's Avatar

    If you are OK with site to be stopped during deployment you can try to stop it with Azure Resource Manager PowerShell as before deploy script.
    Here is how to authenticate against it during the build. I believe it is pretty straightforward to translate this to UI from YAML. Keep app_offline setting in place anyway, it will help you if some request comes during deployment.
    You do not need to start site. it will be started after first request. If you need to pre-heat it before first request come you can ping it from after deploy script. Or enable Always On feature for your website.

  6. 6 Posted by ryan.saul on Dec 14, 2017 @ 07:13 PM

    ryan.saul's Avatar

    Is there a way to do these commands with the App Service's credentials instead of my own?

  7. 7 Posted by Ilya Finkelshte... on Dec 14, 2017 @ 07:59 PM

    Ilya Finkelshteyn's Avatar

    You can try to kill w3wp process using Kudu API. Try this sample, but run powershell stop-process -name w3wp -f instead of npm install. Publishing profile credentials is enough for that.

  8. 8 Posted by ryan.saul on Dec 14, 2017 @ 08:02 PM

    ryan.saul's Avatar

    I'll give this a shot. Would you know if there are any other ways to get the deployment to wait until the locks have been released? I wouldn't mind trying out a simple loop that waits a few seconds.

  9. 9 Posted by Ilya Finkelshte... on Dec 14, 2017 @ 09:32 PM

    Ilya Finkelshteyn's Avatar

    Sure: sync_retry_attempts and sync_retry_interval in WebDeploy provider settings (Retry attempts and Retry interval in UI). Retry interval is in milliseconds.
    By default web deploy already doing 5 retries with 1 second timeout. You can try to adjust those settings and see if it helps.

  10. ryan.saul closed this discussion on Jan 23, 2018 @ 01:20 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac