summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Giddins <segiddins@segiddins.me>2016-12-24 19:20:24 +0100
committerSamuel Giddins <segiddins@segiddins.me>2016-12-25 17:46:46 -0600
commit9f16e683d56dc12d50bad2b1811b87678ca5cf36 (patch)
tree41d04d581e57b98c151d4b220e243c56a70cc224
parent857168ed707027c8c4eb913c25425dad8a4b0e11 (diff)
downloadbundler-seg-worker-thread-creation-failure.tar.gz
[Worker] Fail gracefully when creating threads failsseg-worker-thread-creation-failure
-rw-r--r--lib/bundler/errors.rb1
-rw-r--r--lib/bundler/worker.rb32
-rw-r--r--spec/bundler/worker_spec.rb22
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