From 744c879998ee86ab590db29ca49b421b5295b930 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 6 Apr 2013 14:12:33 -0700 Subject: [PATCH] Don't repair forwarded port to used forwarded port --- CHANGELOG.md | 2 ++ .../handle_forwarded_port_collisions.rb | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f708de91..41edf8079 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ BUG FIXES: - The `:mac` option for host-only networks is respected. [GH-1536] - Don't preserve modified time when untarring boxes. [GH-1539] + - Forwarded port auto-correct will not auto-correct to a port + that is also in use. ## 1.1.6 (April 3, 2013) diff --git a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb index b33d2e4eb..656f985e1 100644 --- a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb +++ b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb @@ -78,18 +78,30 @@ module Vagrant @logger.info("Attempting to repair FP collision: #{host_port}") + repaired_port = nil + while !usable_ports.empty? + # Attempt to repair the forwarded port + repaired_port = usable_ports.to_a.sort[0] + usable_ports.delete(repaired_port) + + # If the port is in use, then we can't use this either... + if extra_in_use.include?(repaired_port) || is_port_open?("127.0.0.1", repaired_port) + @logger.info("Reparied port also in use: #{repaired_port}. Trying another...") + next + end + + # We have a port so break out + break + end + # If we have no usable ports then we can't repair - if usable_ports.empty? + if !repaired_port && usable_ports.empty? raise Errors::ForwardPortAutolistEmpty, :vm_name => env[:machine].name, :guest_port => guest_port.to_s, :host_port => host_port.to_s end - # Attempt to repair the forwarded port - repaired_port = usable_ports.to_a.sort[0] - usable_ports.delete(repaired_port) - # Modify the args in place options[:host] = repaired_port