Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/bootsnap/cli/worker_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,41 @@ class CLI
class WorkerPool
class << self
def create(size:, jobs:)
if size > 0 && Process.respond_to?(:fork)
if size > 0 && _fork_works?
new(size: size, jobs: jobs)
else
Inline.new(jobs: jobs)
end
end

private

def _fork_works?
return false unless ::Process.respond_to?(:fork)

# test forks
# this will hang on certain QEMU environments
pids = 2.times.map do
::Process.fork do
exit!(true)
end
end
# Wait for all forked processes, 1 second at most
returned = []
11.times do |count|
sleep 0.1 unless count.zero?
pids.each do |pid|
next if returned.include?(pid)

pid, _status = ::Process.wait2(pid, ::Process::WNOHANG)
returned << pid if pid
end
# No issues
return true unless returned.size < pids.size
end
# Detected a freeze
false
end
end

class Inline
Expand Down
Loading