{"record":{"id":"0f93cb644aeb9667","repo":"hashicorp/vagrant","slug":"vagrant-attempted-to-acquire-a-lock-named-name","errorCode":null,"errorMessage":"Vagrant attempted to acquire a lock named '%{name}', but this\nlock is being held by another instance of Vagrant already. Please\nwait and try again.","messagePattern":"Vagrant attempted to acquire a lock named '%(.+?)', but this\nlock is being held by another instance of Vagrant already\\. Please\nwait and try again\\.","errorType":"exception","errorClass":"Vagrant::Errors::EnvironmentLockedError","httpStatus":null,"severity":"error","filePath":"lib/vagrant/environment.rb","lineNumber":633,"sourceCode":"      # This allows multiple locks in the same process to be nested\n      return yield if @locks[name] || opts[:noop]\n\n      # The path to this lock\n      lock_path = data_dir.join(\"lock.#{name}.lock\")\n\n      @logger.debug(\"Attempting to acquire process-lock: #{name}\")\n      lock(\"dotlock\", noop: name == \"dotlock\", retry: true) do\n        f = File.open(lock_path, \"w+\")\n      end\n\n      # The file locking fails only if it returns \"false.\" If it\n      # succeeds it returns a 0, so we must explicitly check for\n      # the proper error case.\n      while f.flock(File::LOCK_EX | File::LOCK_NB) === false\n        @logger.warn(\"Process-lock in use: #{name}\")\n\n        if !opts[:retry]\n          raise Errors::EnvironmentLockedError,\n            name: name\n        end\n\n        sleep 0.2\n      end\n\n      @logger.info(\"Acquired process lock: #{name}\")\n\n      result = nil\n      begin\n        # Mark that we have a lock\n        @locks[name] = true\n\n        result = yield\n      ensure\n        # We need to make sure that no matter what this is always\n        # reset to false so we don't think we have a lock when we\n        # actually don't.","sourceCodeStart":615,"sourceCodeEnd":651,"githubUrl":"https://github.com/hashicorp/vagrant/blob/35f3160f4ad6edc3a9f3aa9570adfc1a4d73aaa5/lib/vagrant/environment.rb#L615-L651","documentation":"Raised as Vagrant::Errors::EnvironmentLockedError from Vagrant::Environment#lock (lib/vagrant/environment.rb:633) when flock(File::LOCK_EX | File::LOCK_NB) on data_dir/lock.<name>.lock keeps returning false (another Vagrant instance holds it) and opts[:retry] is false. With retry: true the method sleeps 0.2s and loops instead; the lock is per local-data-directory, so only Vagrants sharing the same .vagrant dir contend.","triggerScenarios":"Two vagrant processes touching the same environment at once — e.g. `vagrant provision` while `vagrant up` runs — on a code path that calls env.lock(name) without retry: true; custom tooling invoking Environment#lock with retry: false; parallel CI jobs sharing one checkout and its .vagrant directory.","commonSituations":"Overlapping cron and interactive runs; a hung vagrant Ruby process still holding the flock; CI pipelines that start multiple vagrant stages concurrently on the same workspace.","solutions":["Find the holder with `ps aux | grep vagrant` and wait for it to finish or stop it — flock is released automatically on process exit, so there are no stale locks to delete","Retry the command once the other invocation completes","Serialize access in CI (single job lane, or wrap commands in `flock /path/.vagrant/lock.global.lock vagrant ...`)","If you call env.lock yourself, pass retry: true to block until the lock frees instead of failing fast"],"exampleFix":"# before\nenv.lock(\"global\", retry: false) { env.cli(%w[up]) }\n\n# after\nenv.lock(\"global\", retry: true) { env.cli(%w[up]) }","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"def with_vagrant_lock(env, name, tries: 5, delay: 0.5)\n  begin\n    env.lock(name, retry: false) { yield }\n  rescue Vagrant::Errors::EnvironmentLockedError\n    tries -= 1\n    raise if tries.zero?\n    sleep delay\n    delay *= 2\n    retry\n  end\nend","preventionTips":["Never run two vagrant commands concurrently against one project/.vagrant dir","Serialize CI stages that touch the same environment (job lanes or flock wrappers)","Remember locks self-release on process exit — never delete lock.*.lock files to 'fix' contention"],"tags":["vagrant","locking","concurrency","flock","environment"],"backgroundTag":"lock-contention","analyzedSha":"35f3160f4ad6edc3a9f3aa9570adfc1a4d73aaa5","analyzedAt":"2026-08-21T13:34:32.514Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}