Skip to content

Commit

Permalink
avoid nil errors by initializing all workers during isolation runs
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 24, 2019
1 parent 88c75b7 commit 27fd098
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
3 changes: 0 additions & 3 deletions Gemfile.lock
Expand Up @@ -65,6 +65,3 @@ DEPENDENCIES
rspec-rerun
ruby-progressbar
sqlite3

BUNDLED WITH
1.16.1
23 changes: 11 additions & 12 deletions lib/parallel.rb
Expand Up @@ -283,6 +283,7 @@ def worker_number
Thread.current[:parallel_worker_number]
end

# TODO: this does not work when doing threads in forks, so should remove and yield the number instead if needed
def worker_number=(worker_num)
Thread.current[:parallel_worker_number] = worker_num
end
Expand Down Expand Up @@ -359,18 +360,15 @@ def work_in_threads(job_factory, options, &block)
end

def work_in_processes(job_factory, options, &blk)
workers = if options[:isolation]
[] # we create workers per job and not beforehand
else
create_workers(job_factory, options, &blk)
end
workers = create_workers(job_factory, options, &blk)
results = []
results_mutex = Mutex.new # arrays are not thread-safe
exception = nil

UserInterruptHandler.kill_on_ctrl_c(workers.map(&:pid), options) do
in_threads(options) do |i|
worker = workers[i]
worker.thread = Thread.current

begin
loop do
Expand All @@ -379,28 +377,29 @@ def work_in_processes(job_factory, options, &blk)
break unless index

if options[:isolation]
worker = replace_worker(job_factory, workers, i, options, blk)
lap ||= 0
lap += 1
replace_worker(job_factory, workers, i, options, blk) if lap > 1
worker.thread = Thread.current
end

worker.thread = Thread.current

begin
result = with_instrumentation item, index, options do
worker.work(job_factory.pack(item, index))
end
results_mutex.synchronize { results[index] = result } # arrays are not threads safe on jRuby
rescue
exception = $!
rescue StandardError => e
exception = e
if Parallel::Kill === exception
(workers - [worker]).each do |w|
w.thread.kill unless w.thread.nil?
w.thread.kill if w.thread
UserInterruptHandler.kill(w.pid)
end
end
end
end
ensure
worker.stop if worker
worker.stop
end
end
end
Expand Down

0 comments on commit 27fd098

Please sign in to comment.