1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2025, by Shopify Inc.
# Copyright, 2025, by Samuel Williams.
module Async
# Private module that hooks into Process._fork to handle fork events.
#
# If `Scheduler#process_fork` hook is adopted in Ruby 4, this code can be removed after Ruby < 4 is no longer supported.
module ForkHandler
def _fork(&block)
result = super
if result.zero?
# Child process:
if Fiber.scheduler.respond_to?(:process_fork)
Fiber.scheduler.process_fork
end
end
return result
end
end
private_constant :ForkHandler
# Hook into Process._fork to handle fork events automatically:
unless RUBY_VERSION > "4"
::Process.singleton_class.prepend(ForkHandler)
end
end
|