1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/lib/active_support/fork_tracker.rb
Daniel Ritz f3260272f2 fix(fork-tracker): Only patch fork if it's supported
Some tests are currently wrapped with
  `if Process.respond_to?(:fork)`
but they're still executed on JRuby. The problem is that ForkTracker
unconditionally patches `#fork` so `Process` does indeed
`respond_to? :fork`.

Fix it by installing the patch conditionally with the same check.
2019-10-06 23:41:37 +02:00

58 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module ActiveSupport
module ForkTracker # :nodoc:
module CoreExt
def fork(*)
if block_given?
super do
ForkTracker.check!
yield
end
else
unless pid = super
ForkTracker.check!
end
pid
end
end
end
module CoreExtPrivate
include CoreExt
private :fork
end
@pid = Process.pid
@callbacks = []
class << self
def check!
if @pid != Process.pid
@callbacks.each(&:call)
@pid = Process.pid
end
end
def hook!
if Process.respond_to?(:fork)
::Object.prepend(CoreExtPrivate)
::Kernel.prepend(CoreExtPrivate)
::Kernel.singleton_class.prepend(CoreExt)
::Process.singleton_class.prepend(CoreExt)
end
end
def after_fork(&block)
@callbacks << block
block
end
def unregister(callback)
@callbacks.delete(callback)
end
end
end
end
ActiveSupport::ForkTracker.hook!