From e98db8dc867dda6eb84f86093257c8fd7fa90ccf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 19 Dec 2010 10:27:07 -0800 Subject: [PATCH] Determine SSH on main thread for up? to fix issues with multi-thread access on JRuby --- lib/vagrant/ssh.rb | 12 +++++++++--- test/vagrant/ssh_test.rb | 11 ++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/vagrant/ssh.rb b/lib/vagrant/ssh.rb index 38f1424de..472dc5a8e 100644 --- a/lib/vagrant/ssh.rb +++ b/lib/vagrant/ssh.rb @@ -61,12 +61,12 @@ module Vagrant # Merge in any additional options opts = opts.dup opts[:forward_agent] = true if env.config.ssh.forward_agent + opts[:port] ||= port retryable(:tries => 5, :on => Errno::ECONNREFUSED) do Net::SSH.start(env.config.ssh.host, env.config.ssh.username, - opts.merge( :port => port, - :keys => [env.config.ssh.private_key_path], + opts.merge( :keys => [env.config.ssh.private_key_path], :user_known_hosts_file => [], :paranoid => false, :config => false)) do |ssh| @@ -93,8 +93,14 @@ module Vagrant # # @return [Boolean] def up? + # We have to determine the port outside of the block since it uses + # API calls which can only be used from the main thread in JRuby on + # Windows + ssh_port = port + Timeout.timeout(env.config.ssh.timeout) do - execute(:timeout => env.config.ssh.timeout) { |ssh| } + execute(:timeout => env.config.ssh.timeout, + :port => ssh_port) { |ssh| } end true diff --git a/test/vagrant/ssh_test.rb b/test/vagrant/ssh_test.rb index 93542c5f9..f63c38788 100644 --- a/test/vagrant/ssh_test.rb +++ b/test/vagrant/ssh_test.rb @@ -221,7 +221,11 @@ class SshTest < Test::Unit::TestCase end should "specifity the timeout as an option to execute" do - @ssh.expects(:execute).with(:timeout => @env.config.ssh.timeout).yields(true) + @ssh.expects(:execute).yields(true).with() do |opts| + assert_equal @env.config.ssh.timeout, opts[:timeout] + true + end + assert @ssh.up? end @@ -229,6 +233,11 @@ class SshTest < Test::Unit::TestCase @ssh.expects(:execute).raises(Net::SSH::AuthenticationFailed) assert_raises(Vagrant::Errors::SSHAuthenticationFailed) { @ssh.up? } end + + should "only get the port once (in the main thread)" do + @ssh.expects(:port).once.returns(2222) + @ssh.up? + end end context "getting the ssh port" do