core: delete environment lock files

This commit is contained in:
Mitchell Hashimoto 2014-04-24 23:13:08 -07:00
parent 84f889e801
commit 0ed3c5174b
1 changed files with 44 additions and 18 deletions

View File

@ -352,6 +352,8 @@ module Vagrant
# @param [String] name Name of the lock, since multiple locks can
# be held at one time.
def lock(name="global", **opts)
f = nil
# If we don't have a block, then locking is useless, so ignore it
return if !block_given?
@ -362,7 +364,15 @@ module Vagrant
lock_path = data_dir.join("lock.#{name}.lock")
@logger.debug("Attempting to acquire process-lock: #{name}")
File.open(lock_path, "w+") do |f|
if name != "dotlock"
lock("dotlock", no_clean: true) do
f = File.open(lock_path, "w+")
end
else
f = File.open(lock_path, "w+")
end
# The file locking fails only if it returns "false." If it
# succeeds it returns a 0, so we must explicitly check for
# the proper error case.
@ -374,17 +384,33 @@ module Vagrant
@logger.info("Acquired process lock: #{name}")
result = nil
begin
# Mark that we have a lock
@locks[name] = true
return yield
result = yield
ensure
# We need to make sure that no matter what this is always
# reset to false so we don't think we have a lock when we
# actually don't.
@locks.delete(name)
end
# Clean up the lock file, this requires another lock
if !opts[:no_clean]
lock("dotlock", no_clean: true) do
f.close
File.delete(lock_path)
end
end
# Return the result
return result
ensure
begin
f.close if f
rescue IOError
end
end