Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chocolatey config changes in 0.10.4+ - The process cannot access the file because it is being used by another process #1241

Closed
ferventcoder opened this issue Apr 5, 2017 · 9 comments

Comments

@ferventcoder
Copy link
Member

ferventcoder commented Apr 5, 2017

The fixes completed for #1047 are now causing the following error when using Puppet's Chocolatey provider to set configuration:

The process cannot access the file because it is being used by another process

This is likely due to the Puppet provider holding a lock on the original file. The original method renamed that file and wrote a new file in it's place, which allowed issues with something holding a lock on a file and so it was never caught in testing - https://github.com/puppetlabs/puppetlabs-chocolatey/blob/5e3af2410863a216a4cb0dce90a74171f9873931/lib/puppet/provider/chocolateysource/windows.rb#L45-L48

https://tickets.puppetlabs.com/browse/MODULES-4678 is the issue for the Puppet provider.

This has been validated by folks using Puppet and outside of Puppet when they run Chocolatey. It's like something now has an lock on the file, except for the folks that have checked, there is nothing holding a lock on the file itself. Seems to be a race condition somewhere.

cc @RichiCoder1

C:\ProgramData\PuppetLabs>choco -version
This is try 1/3. Retrying after 300 milliseconds.
 Error converted to warning:
 (32) The process cannot access the file because it is being used by another process: [\\?\C:\ProgramData\chocolatey\config\chocolatey.config]
This is try 2/3. Retrying after 400 milliseconds.
 Error converted to warning:
 (32) The process cannot access the file because it is being used by another process: [\\?\C:\ProgramData\chocolatey\config\chocolatey.config]
Maximum tries of 3 reached. Throwing error.
Error deserializing response of type chocolatey.infrastructure.app.configuration.ConfigFileSettings:
 (32) The process cannot access the file because it is being used by another process: [\\?\C:\ProgramData\chocolatey\config\chocolatey.config]
(32) The process cannot access the file because it is being used by another process: [\\?\C:\ProgramData\chocolatey\config\chocolatey.config]

More details

This appears to happen after:

Attempting to replace "C:\ProgramData\chocolatey\config\chocolatey.config"
 with "C:\ProgramData\chocolatey\config\chocolatey.config.update". Backup placed at "C:\ProgramData\chocolatey\config\chocolatey.config.backup".

Workaround

Downgrade to Chocolatey v0.10.3 until this is resolved.

@ferventcoder ferventcoder added this to the 0.10.6 milestone Apr 5, 2017
@ferventcoder ferventcoder self-assigned this Apr 5, 2017
@ferventcoder ferventcoder changed the title Chocolatey config changes in 0.10.4+ are causing file in use errors Chocolatey config changes in 0.10.4+ - The process cannot access the file because it is being used by another process Apr 5, 2017
@ferventcoder
Copy link
Member Author

If you run into this error, please provide the output of the following:

cacls C:\ProgramData\chocolatey\config\chocolatey.config

@tek0011
Copy link

tek0011 commented Apr 5, 2017

On first run, and first failure it looks like this.
image

First run:

C:\Windows\system32>C:\ProgramData\chocolatey\config\chocolatey.config NT AUTHORITY\SYSTEM:(ID)F

C:\Windows\system32>                                                   BUILTIN\Administrators:(ID)F
The system cannot find the path specified.

C:\Windows\system32>                                                   BUILTIN\Administrators:(ID)F

On second run, same failure, same red wall of text, but this time it says it can find the files:
image

Second run:

C:\ProgramData\chocolatey\config>cacls C:\ProgramData\chocolatey\config\chocolatey.config
C:\ProgramData\chocolatey\config\chocolatey.config NT AUTHORITY\SYSTEM:(ID)F
                                                   BUILTIN\Administrators:(ID)F
                                                   BUILTIN\Users:(ID)R


C:\ProgramData\chocolatey\config>tree /f
Folder PATH listing for volume OSDisk SSD
Volume serial number is 9ED9-43A5
C:.
    chocolatey.config
    chocolatey.config.backup
    chocolatey.config.update

No subfolders exist

@tek0011
Copy link

tek0011 commented Apr 5, 2017

config.zip

configs at the time of issue, if helps

@ferventcoder
Copy link
Member Author

Thanks! This will help

@sd4705
Copy link

sd4705 commented Apr 14, 2017

Our site is affected by this problem. Thanks for working on this.

You can downgrade chocolatey with this command:
choco upgrade chocolatey --version 0.10.3 --force -y

Close the powershell window and then run puppet again:
puppet agent -t

@RichiCoder1
Copy link
Contributor

This is very odd. File locks were something I was trying hard to avoid :|

@ferventcoder
Copy link
Member Author

This change also affects DSC:

We started getting errors in our deployments recently and after doing a bit of digging here is what we found. First, Chocolatey was updated from 0.10.4 to 0.10.5 recently and with that change it seems the stick a backup of the chocolatey.config file in the config folder named chocolatey.config.backup right after install. This command in your test resource:
$configfile = Get-ChildItem $configfolder | Where-Object {$_.Name -match "chocolatey.config"}
now returns two files and so the following line to parse the xml fails since there are two file objects return. I simply updated the -match to a -eq in the above line and that seems to fix the issue.

@ferventcoder
Copy link
Member Author

I believe this is fixed for 0.10.6.

@ferventcoder ferventcoder reopened this May 25, 2017
ferventcoder added a commit that referenced this issue May 29, 2017
When writing a file with streams, use the built in methods in
IFileSystem for doing so.
ferventcoder added a commit that referenced this issue May 29, 2017
Using File.Replace can have some access violation concerns. It demands
full access to a file, which can cause issues if the file is locked. If
a file is open in another process, it has issues renaming the file,
which causes the rest to not work appropriately.

Implement file replace methods in a move step that will work with files
that are open and then copy the original file to the destination and
delete the original file.

If the file locked is the backup file, it should log that it was not
able to produce the backup and continue without failure. If it has
issues removing the source file, it should log that it was not able
to delete the file and continue without failure.
ferventcoder added a commit that referenced this issue May 29, 2017
To really limit the possibility of something holding a lock on a file,
use process id in files. Also attempt to clean up any locked update
files when getting the configuration.
ferventcoder added a commit that referenced this issue May 29, 2017
For resources that need closed and disposed in XmlService, explicitly
handle closing of those resources. This allows being extremely explicit
about when resources are cleaned up and subsequently released.
ferventcoder added a commit that referenced this issue May 29, 2017
When moving a file to a new location, create the directory for the new
file if it doesn't already exist.
ferventcoder added a commit that referenced this issue May 29, 2017
Log tracing output surrounding working with xml files.
ferventcoder added a commit that referenced this issue May 29, 2017
* pr1264: (26 commits)
  (maint) COMMITTERS - EOL change to CRLF
  (doc) update CONTRIBUTING/COMMITTERS
  (GH-1241) XmlService - trace logging information
  (maint) formatting
  (GH-1241) FileSystem - move file creates directory
  (GH-1241) Explicitly handle closing
  (maint) formatting
  (GH-1292) Ensure manifest extracted on unpackself
  (log) replace_file use new lines for log
  (maint) update CHANGELOG / nuspec
  (maint) nuspec - add info links to description
  (maint) clean up lib nuspec
  (GH-1311) Install-ChocolateyPowershellCommand File/FileFullPath aliases
  (GH-1284) ensure file/filefullpath on functions
  (maint) ReadMe updates
  (GH-1241) Use process id with update file
  (maint) restrict list start/end messages to log file
  (GH-1309) Restrict trace output
  (GH-1241) More Robust File.Replace
  (GH-1241) Use write_file for stream writing
  ...
ferventcoder added a commit that referenced this issue May 29, 2017
* stable: (26 commits)
  (maint) COMMITTERS - EOL change to CRLF
  (doc) update CONTRIBUTING/COMMITTERS
  (GH-1241) XmlService - trace logging information
  (maint) formatting
  (GH-1241) FileSystem - move file creates directory
  (GH-1241) Explicitly handle closing
  (maint) formatting
  (GH-1292) Ensure manifest extracted on unpackself
  (log) replace_file use new lines for log
  (maint) update CHANGELOG / nuspec
  (maint) nuspec - add info links to description
  (maint) clean up lib nuspec
  (GH-1311) Install-ChocolateyPowershellCommand File/FileFullPath
aliases
  (GH-1284) ensure file/filefullpath on functions
  (maint) ReadMe updates
  (GH-1241) Use process id with update file
  (maint) restrict list start/end messages to log file
  (GH-1309) Restrict trace output
  (GH-1241) More Robust File.Replace
  (GH-1241) Use write_file for stream writing
  ...

# Conflicts:
#	CONTRIBUTING.md
#	README.md
@ferventcoder
Copy link
Member Author

Okay, now this is ready for 0.10.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants