Skip to content

Commit

Permalink
add testing logics for Process.fork to avoid freezing on QEMU
Browse files Browse the repository at this point in the history
  • Loading branch information
midnight-wonderer committed Feb 14, 2025
1 parent cae219a commit 3c2ce3e
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/bootsnap/cli/worker_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@ 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

0 comments on commit 3c2ce3e

Please sign in to comment.