diff --git a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb index ea462be2f..5425a4221 100644 --- a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb +++ b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb @@ -32,6 +32,7 @@ module Vagrant end def call(env) + @leased = [] @machine = env[:machine] # Acquire a process-level lock so that we don't choose a port @@ -46,6 +47,13 @@ module Vagrant end @app.call(env) + + # Always run the recover method so that we release leases + recover(env) + end + + def recover(env) + lease_release end protected @@ -176,10 +184,22 @@ module Vagrant f.write(Time.now.to_i.to_s + "\n") end + # Add to the leased array so we unlease it right away + @leased << port.to_s + # Things look good to us! false end + def lease_release + leasedir = @machine.env.data_dir.join("fp-leases") + + @leased.each do |port| + path = leasedir.join(port) + path.delete if path.file? + end + end + def port_check(port) is_port_open?("127.0.0.1", port) end