11

This error occurs sometimes, usually this step works fine, but in about 10% cases it fails with below message.

Nuget installer step is first build step, and also "clean checkout" is enabled in TeamCity, so there shouldn't be any process that uses file.

[restore] The process cannot access the file 'C:\BuildAgent\work...\packages\Microsoft.Bcl.Build.1.0.21\Microsoft.Bcl.Build.1.0.21.nupkg' because it is being used by another process.

[restore] Process exited with code 1

Community
  • 1
  • 1
Lev
  • 3,719
  • 6
  • 41
  • 56

3 Answers3

6

Root cause of "my file is being used by another process"

There are several things you can do, depending on what is the root cause:

  • You use parallel build feature of MSBuild: According to his blog, It seems to not work well with NuGet package restore, because it could runs parallel package restore for each projects (and if several projects require Microsoft.Bcl.Build... Boom!). Either disable parallel build or do package restore outside of solution (in a command line step). By the way, it's not a good practice to do NuGet restore with solution MSBuild.

  • There is another process:
    Use process monitor/explorer to find out which process hold the lock on the file. It could be an antivirus, file indexing tool, an additional build agent instance which keep running in the background. As the lock is not permanent, and 1 out of 10 is quite a lot for this kind of issue, I would not bet that this is the root cause.

  • There is no other process:
    It's a bug in the Teamcity NuGet installer which do something. Then you could replace the step by a command line step with a call to nuget restore and check if it fixes your problem.

  • Diagnose yourself with a nuget plugin of your flavor:
    NuGet plugin code is available here. It means you can compile a version of your own, with additional debugging information when these kind of problem happen (list of process locking the file for example). Or even more useful, you can add a retry step in case the file is locked.

EDIT: For those who use Teamcity on Unix...

I've tried to install Teamcity on Unix and make a build on mono with the NuGet plugin. By the way, NuGet plugin does not work at all on Unix (It's not supported by JetBrains). I even tried to fix it but it was easier to just replace it with a command line.

Fab
  • 14,327
  • 5
  • 49
  • 68
3

Do you have separate working directory for each build agent? Otherwise two paralel builds could execute in the same time.

mano
  • 136
  • 3
  • Yes, I have 2 AWS EC2 cloud build agents, both installed as single build agents, on separate instances. (+1 for mentioning it) – Lev Feb 01 '16 at 11:07
  • 1
    Well than i would use some lock checking tool like process explorer to determine who holds that file when the build fails. But you have to have access to that server. – mano Feb 01 '16 at 14:50
2

Try this:

  1. Add a special build step in TeamCity which runs if "Even if some of the previous steps failed". (https://confluence.jetbrains.com/display/TCDL/Configuring+Build+Steps)
  2. In this build step find out which process is locking a file and print it in log. (How do I find out which process is locking a file using .NET?)
Dmitrii Zyrianov
  • 2,208
  • 1
  • 21
  • 27