From 64828f1ed3a4d33b3e85e1c925a5c3bc5acb1bca Mon Sep 17 00:00:00 2001 From: Matt Wrock Date: Sat, 13 Aug 2016 07:49:08 -0700 Subject: [PATCH] fix #7489 preventing winrm connection leakage --- plugins/communicators/winrm/shell.rb | 12 ++++++------ test/unit/plugins/communicators/winrm/shell_test.rb | 4 +++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/plugins/communicators/winrm/shell.rb b/plugins/communicators/winrm/shell.rb index f55c8fb75..0ea9e3cbb 100644 --- a/plugins/communicators/winrm/shell.rb +++ b/plugins/communicators/winrm/shell.rb @@ -56,11 +56,15 @@ module VagrantPlugins def powershell(command, &block) # Ensure an exit code command += "\r\nif ($?) { exit 0 } else { if($LASTEXITCODE) { exit $LASTEXITCODE } else { exit 1 } }" - execute_with_rescue(executor.method("run_powershell_script"), command, &block) + session.create_executor do |executor| + execute_with_rescue(executor.method("run_powershell_script"), command, &block) + end end def cmd(command, &block) - execute_with_rescue(executor.method("run_cmd"), command, &block) + session.create_executor do |executor| + execute_with_rescue(executor.method("run_cmd"), command, &block) + end end def wql(query, &block) @@ -172,10 +176,6 @@ module VagrantPlugins @session ||= new_session end - def executor - @executor ||= session.create_executor - end - def endpoint case @config.transport.to_sym when :ssl diff --git a/test/unit/plugins/communicators/winrm/shell_test.rb b/test/unit/plugins/communicators/winrm/shell_test.rb index 5cf1f6810..0c77290c8 100644 --- a/test/unit/plugins/communicators/winrm/shell_test.rb +++ b/test/unit/plugins/communicators/winrm/shell_test.rb @@ -6,7 +6,7 @@ require Vagrant.source_root.join("plugins/communicators/winrm/config") describe VagrantPlugins::CommunicatorWinRM::WinRMShell do include_context "unit" - let(:session) { double("winrm_session", create_executor: executor) } + let(:session) { double("winrm_session") } let(:executor) { double("command_executor") } let(:port) { config.transport == :ssl ? 5986 : 5985 } let(:config) { @@ -22,6 +22,8 @@ describe VagrantPlugins::CommunicatorWinRM::WinRMShell do end } + before { allow(session).to receive(:create_executor).and_yield(executor) } + subject do described_class.new('localhost', port, config).tap do |comm| allow(comm).to receive(:new_session).and_return(session)