Skip to content

Commit

Permalink
Merge pull request #10891 from chrisroberts/f-trigger-multithread
Browse files Browse the repository at this point in the history
Update trigger abort behavior when running parallel actions
  • Loading branch information
chrisroberts committed Jun 5, 2019
2 parents 04083e7 + 88c6756 commit 2507b68
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/vagrant/batch_action.rb
Expand Up @@ -71,6 +71,10 @@ def run
thread = Thread.new do
Thread.current[:error] = nil

# Note that this thread is being used for running
# a batch action
Thread.current[:batch_parallel_action] = par

# Record our pid when we started in order to figure out if
# we've forked...
start_pid = Process.pid
Expand Down Expand Up @@ -160,6 +164,16 @@ def run
if !errors.empty?
raise Errors::BatchMultiError, message: errors.join("\n\n")
end

# Check if any threads set an exit code and exit if found. If
# multiple threads have exit code values set, the first encountered
# will be the value used.
threads.each do |thread|
if thread[:exit_code]
@logger.debug("Found exit code set within batch action thread. Exiting")
Process.exit!(thread[:exit_code])
end
end
end
end
end
13 changes: 11 additions & 2 deletions lib/vagrant/plugin/v2/trigger.rb
Expand Up @@ -300,8 +300,17 @@ def run_remote(config, on_error, exit_codes)
#
# @param [Integer] code Code to exit Vagrant on
def trigger_abort(exit_code)
@ui.warn(I18n.t("vagrant.trigger.abort"))
Process.exit!(exit_code)
if Thread.current[:batch_parallel_action]
@ui.warn(I18n.t("vagrant.trigger.abort_threaded"))
@logger.debug("Trigger abort within parallel batch action. " \
"Setting exit code and terminating.")
Thread.current[:exit_code] = exit_code
Thread.current.terminate
else
@ui.warn(I18n.t("vagrant.trigger.abort"))
@logger.debug("Trigger abort within non-parallel action, exiting directly")
Process.exit!(exit_code)
end
end

# Calls the given ruby block for execution
Expand Down
3 changes: 3 additions & 0 deletions templates/locales/en.yml
Expand Up @@ -297,6 +297,9 @@ en:
Trigger configured to continue on error...
abort: |-
Vagrant has been configured to abort. Terminating now...
abort_threaded: |-
Vagrant has been configured to abort. Vagrant will terminate
after remaining running actions have completed...
start: |-
Running %{type} triggers %{stage} %{action} ...
fire_with_name: |-
Expand Down
19 changes: 19 additions & 0 deletions test/unit/vagrant/batch_action_test.rb
Expand Up @@ -63,5 +63,24 @@ def new_machine(options)
subject.action(machine, "up")
subject.run
end

context "with provider supporting parallel actions" do
let(:provider_options) { {parallel: true} }

it "should flag threads as being parallel actions" do
parallel = nil
subject.custom(machine) { |m| parallel = Thread.current[:batch_parallel_action] }
subject.custom(machine) { |*_| }
subject.run
expect(parallel).to eq(true)
end

it "should exit the process if exit_code has been set" do
subject.custom(machine) { |m| Thread.current[:exit_code] = 1}
subject.custom(machine) { |*_| }
expect(Process).to receive(:exit!).with(1)
subject.run
end
end
end
end
31 changes: 31 additions & 0 deletions test/unit/vagrant/plugin/v2/trigger_test.rb
Expand Up @@ -423,6 +423,37 @@
expect(Process).to receive(:exit!).with(3)
subject.send(:trigger_abort, 3)
end

context "when running in parallel" do
let(:thread) {
@t ||= Thread.new do
Thread.current[:batch_parallel_action] = true
Thread.stop
subject.send(:trigger_abort, exit_code)
end
}
let(:exit_code) { 22 }

before do
expect(Process).not_to receive(:exit!)
sleep(0.1) until thread.stop?
end

after { @t = nil }

it "should terminate the thread" do
expect(thread).to receive(:terminate).and_call_original
thread.wakeup
thread.join(1) while thread.alive?
end

it "should set the exit code into the thread data" do
expect(thread).to receive(:terminate).and_call_original
thread.wakeup
thread.join(1) while thread.alive?
expect(thread[:exit_code]).to eq(exit_code)
end
end
end

context "#ruby" do
Expand Down

0 comments on commit 2507b68

Please sign in to comment.