diff options
author | The Bundler Bot <bot@bundler.io> | 2016-12-27 15:32:31 +0000 |
---|---|---|
committer | The Bundler Bot <bot@bundler.io> | 2016-12-27 15:32:31 +0000 |
commit | 28b2d86db1963cb821b443e32c19104d048a901b (patch) | |
tree | 61c716f9ce41f74718538377ea4fb646707ebfa2 | |
parent | 159c7dc8566f9bdd54f6d877658ad29397e2ec67 (diff) | |
parent | 9f16e683d56dc12d50bad2b1811b87678ca5cf36 (diff) | |
download | bundler-28b2d86db1963cb821b443e32c19104d048a901b.tar.gz |
Auto merge of #5278 - bundler:seg-worker-thread-creation-failure, r=indirect
[Worker] Fail gracefully when creating threads fails
-rw-r--r-- | lib/bundler/errors.rb | 1 | ||||
-rw-r--r-- | lib/bundler/worker.rb | 32 | ||||
-rw-r--r-- | spec/bundler/worker_spec.rb | 22 |
3 files changed, 50 insertions, 5 deletions
diff --git a/lib/bundler/errors.rb b/lib/bundler/errors.rb index be72a9ee47..ecd9260ea0 100644 --- a/lib/bundler/errors.rb +++ b/lib/bundler/errors.rb @@ -53,6 +53,7 @@ module Bundler class GemfileLockNotFound < BundlerError; status_code(22); end class PluginError < BundlerError; status_code(29); end class SudoNotPermittedError < BundlerError; status_code(30); end + class ThreadCreationError < BundlerError; status_code(33); end class GemfileEvalError < GemfileError; end class MarshalError < StandardError; end diff --git a/lib/bundler/worker.rb b/lib/bundler/worker.rb index 3ef5addcee..62d93e285e 100644 --- a/lib/bundler/worker.rb +++ b/lib/bundler/worker.rb @@ -25,11 +25,8 @@ module Bundler @request_queue = Queue.new @response_queue = Queue.new @func = func - @threads = Array.new(size) do |i| - Thread.start { process_queue(i) }.tap do |thread| - thread.name = "#{name} Worker ##{i}" if thread.respond_to?(:name=) - end - end + @size = size + @threads = nil trap("INT") { abort_threads } end @@ -37,6 +34,7 @@ module Bundler # # @param obj [String] mostly it is name of spec that should be downloaded def enq(obj) + create_threads unless @threads @request_queue.enq obj end @@ -70,13 +68,37 @@ module Bundler # Stop the worker threads by sending a poison object down the request queue # so as worker threads after retrieving it, shut themselves down def stop_threads + return unless @threads @threads.each { @request_queue.enq POISON } @threads.each(&:join) + @threads = nil end def abort_threads + return unless @threads @threads.each(&:exit) exit 1 end + + def create_threads + creation_errors = [] + + @threads = Array.new(@size) do |i| + begin + Thread.start { process_queue(i) }.tap do |thread| + thread.name = "#{name} Worker ##{i}" if thread.respond_to?(:name=) + end + rescue ThreadError => e + creation_errors << e + nil + end + end.compact + + return if creation_errors.empty? + + message = "Failed to create threads for the #{name} worker: #{creation_errors.map(&:to_s).uniq.join(", ")}" + raise ThreadCreationError, message if @threads.empty? + Bundler.ui.info message + end end end diff --git a/spec/bundler/worker_spec.rb b/spec/bundler/worker_spec.rb new file mode 100644 index 0000000000..786778b08f --- /dev/null +++ b/spec/bundler/worker_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true +require "spec_helper" +require "bundler/worker" + +describe Bundler::Worker do + let(:size) { 5 } + let(:name) { "Spec Worker" } + let(:function) { proc {|object, worker_number| [object, worker_number] } } + subject { described_class.new(size, name, function) } + + after { subject.stop } + + describe "#initialize" do + context "when Thread.start raises ThreadError" do + it "raises when no threads can be created" do + allow(Thread).to receive(:start).and_raise(ThreadError, "error creating thread") + + expect { subject.enq "a" }.to raise_error(Bundler::ThreadCreationError, "Failed to create threads for the Spec Worker worker: error creating thread") + end + end + end +end |