From 9af48e576487c8b7dd00d6f8c4769cfaf7dcda33 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Fri, 26 Jun 2020 13:25:59 -0400 Subject: [PATCH] Raise an error on nil exit status This commit changes the SSH communicator to raise an error if Vagrant doesn't receive an exit status from an SSH command, for example if the command is terminated by the OOM-killer. --- lib/vagrant/errors.rb | 4 ++++ plugins/communicators/ssh/communicator.rb | 5 +++++ templates/locales/en.yml | 5 +++++ test/unit/plugins/communicators/ssh/communicator_test.rb | 9 +++++++++ 4 files changed, 23 insertions(+) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 05e821bba..cdb365bf0 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -796,6 +796,10 @@ module Vagrant error_key(:ssh_key_type_not_supported) end + class SSHNoExitStatus < VagrantError + error_key(:ssh_no_exit_status) + end + class SSHNoRoute < VagrantError error_key(:ssh_no_route) end diff --git a/plugins/communicators/ssh/communicator.rb b/plugins/communicators/ssh/communicator.rb index da43cb23e..80a2c108c 100644 --- a/plugins/communicators/ssh/communicator.rb +++ b/plugins/communicators/ssh/communicator.rb @@ -729,6 +729,11 @@ module VagrantPlugins yield :stdout, data if block_given? end + if !exit_status + @logger.debug("Exit status: #{exit_status.inspect}") + raise Vagrant::Errors::SSHNoExitStatus + end + # Return the final exit status return exit_status end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 985006e0d..a961388a2 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1435,6 +1435,11 @@ en: usually indicates that SSH within the guest machine was unable to properly start up. Please boot the VM in GUI mode to check whether it is booting properly. + ssh_no_exit_status: |- + The SSH command completed, but Vagrant did not receive an exit status. + This indicates that the command was terminated unexpectedly. Please + check that the VM has sufficient memory to run the command and that no + processes were killed by the guest operating system. ssh_no_route: |- While attempting to connect with SSH, a "no route to host" (EHOSTUNREACH) error was received. Please verify your network settings are correct diff --git a/test/unit/plugins/communicators/ssh/communicator_test.rb b/test/unit/plugins/communicators/ssh/communicator_test.rb index d6e9d2b85..f25d93163 100644 --- a/test/unit/plugins/communicators/ssh/communicator_test.rb +++ b/test/unit/plugins/communicators/ssh/communicator_test.rb @@ -317,6 +317,15 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do end end + context "with no exit code received" do + let(:exit_data) { double("exit_data", read_long: nil) } + + it "raises error when exit code is nil" do + expect(command_channel).to receive(:send_data).with(/make\n/) + expect{ communicator.execute("make") }.to raise_error(Vagrant::Errors::SSHNoExitStatus) + end + end + context "with garbage content prepended to command output" do let(:command_stdout_data) do "Line of garbage\nMore garbage\n#{command_garbage_marker}bin\ntmp\n"