123456789_123456789_123456789_123456789_123456789_

Module: ActiveSupport::Ractors

Do not use. This module is for internal use only.

Overview

Shims for Ractor shareability methods so framework code can call them unconditionally regardless of the Ruby version.

Class Attribute Summary

Class Method Summary

Autoload - Extended

Class Attribute Details

.main? (readonly)

See additional method definition at line 81.

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 121

def main?
  Ractor.main?
end

.unshareable_proc_action (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 13

attr_accessor :unshareable_proc_action

Class Method Details

.make_shareable(obj, copy: false)

Makes obj Ractor-shareable by delegating to Ractor.make_shareable.

The copy: option is forwarded unchanged. On Ruby versions without Ractor.make_shareable, this shim returns obj unchanged.

See additional method definition at line 89.

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 129

def make_shareable(...)
  Ractor.make_shareable(...)
end

.on_main(obj = nil, &block)

See additional method definition at line 71.

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 125

def on_main(obj = nil, &block)
  if Ractor.main?
    obj.instance_eval(&block)
  else
    require "ractor/dispatch"
    shareable_block = Ractor.shareable_proc(&block)
    Ractor::Dispatch.main.run { obj.instance_eval(&shareable_block) }
  end
end

.shareable?(obj) ⇒ Boolean

Returns whether obj is Ractor-shareable by delegating to Ractor.shareable?.

On Ruby versions without Ractor.shareable?, this shim returns obj unchanged.

See additional method definition at line 98.

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 133

def shareable?(obj)
  Ractor.shareable?(obj)
end

.shareable_lambda(self: nil, &block)

Returns a Ractor-shareable lambda by delegating to Ractor.shareable_lambda.

The optional self: value is forwarded as the lambda's receiver. On Ruby versions without Ractor.shareable_lambda, this shim returns the block unchanged.

See additional method definition at line 117.

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 141

def shareable_lambda(...)
  Ractor.shareable_lambda(...)
end

.shareable_proc(self: nil, &block)

Returns a Ractor-shareable proc by delegating to Ractor.shareable_proc.

The optional self: value is forwarded as the proc's receiver. On Ruby versions without Ractor.shareable_proc, this shim returns the block unchanged.

See additional method definition at line 107.

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 137

def shareable_proc(...)
  Ractor.shareable_proc(...)
end

.try_make_shareable(obj)

Attempt to make an object shareable. If successful, a shareable object is returned. If a Ractor::IsolationError is raised, the outcome will depend on how the user's application configuration:

:raise - The error is raised :warn - A deprecation warning is triggered and the original unshareable object is returned.

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 49

def try_make_shareable(obj)
  return obj unless unshareable_proc_action

  make_shareable(obj)
rescue Ractor::IsolationError
  case unshareable_proc_action
  when :raise
    raise
  when :warn
    ActiveSupport.deprecator.warn(<<~MSG)
      Rails attempted to make an object from your application Ractor shareable but a Ractor
      Isolation error was raised. The object being returned is not Ractor safe and a runtime
      error may occur anytime during the request lifecycle.

      #{obj.inspect}
    MSG

    obj
  end
end

.try_shareable_proc(proc = nil, &block)

Attempt to make a proc shareable. If successful, a shareable proc is returned. If a Ractor::IsolationError is raised, the outcome will depend on how the user's application configuration:

:raise - The error is raised :warn - A deprecation warning is triggered and the original unshareable proc is returned.

[ GitHub ]

  
# File 'activesupport/lib/active_support/ractors.rb', line 21

def try_shareable_proc(proc = nil, &block)
  proc ||= block
  return proc unless unshareable_proc_action

  shareable_proc(&proc)
rescue Ractor::IsolationError
  case unshareable_proc_action
  when :raise
    raise
  when :warn
    ActiveSupport.deprecator.warn(<<~MSG)
      Rails attempted to make a Proc from your application Ractor shareable but a Ractor
      Isolation error was raised. The proc being returned is not Ractor safe and a runtime
      error may occur anytime during the request lifecycle.

      #{proc.inspect}
    MSG

    proc
  end
end