Module: SemanticLogger::RactorCopy

Defined in:
lib/semantic_logger/ractor_copy.rb

Overview

Internal, experimental: what Ruby can copy from one Ractor to another, and how to replace what it cannot. Ruby 4.0 and later only.

Sending a value to another Ractor copies it, unless it is shareable. Ruby cannot copy some objects, such as a Proc, a Thread, a Method, or an Enumerator, or anything that holds one.

Class Method Summary collapse

Class Method Details

.copyable(value, depth = 0) ⇒ Object

Returns the value when it can be copied to another Ractor. Otherwise a Hash or an Array with each element that cannot be copied replaced, and any other value replaced by its #inspect. A Hash or an Array is always rebuilt, so that each value in it is tried once, not with every Hash or Array that holds it.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/semantic_logger/ractor_copy.rb', line 32

def self.copyable(value, depth = 0)
  case value
  when Hash
    return Utils.safe_inspect(value) if depth >= Utils::MAX_JSON_SAFE_DEPTH

    value.to_h { |key, val| [copyable(key, depth + 1), copyable(val, depth + 1)] }
  when Array
    return Utils.safe_inspect(value) if depth >= Utils::MAX_JSON_SAFE_DEPTH

    value.map { |element| copyable(element, depth + 1) }
  else
    plain?(value) || copyable?(value) ? value : Utils.safe_inspect(value)
  end
end

.copyable?(value) ⇒ Boolean

Whether the value can be copied to another Ractor: sends it through a port of this Ractor.

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
# File 'lib/semantic_logger/ractor_copy.rb', line 56

def self.copyable?(value)
  port = ::Ractor::Port.new
  port << value
  port.receive
  true
rescue StandardError
  false
ensure
  port&.close
end

.copyable_exception(exception, depth = 0) ⇒ Object

Returns the exception when it can be copied to another Ractor. Otherwise a copy of the same class with the message it was raised with, its backtrace, its cause, which is copied the same way, and its instance variables, made copyable as by .copyable. Or a RuntimeError with the class name in its message, for an exception whose #message needs what could not be copied.



51
52
53
# File 'lib/semantic_logger/ractor_copy.rb', line 51

def self.copyable_exception(exception, depth = 0)
  copyable?(exception) ? exception : plain_exception(exception, depth)
end

.pack(log) ⇒ Object

Returns [Array] the log entry, and the backtraces of its exceptions, to send to another Ractor.

Ruby 4.0 leaves an exception copied to another Ractor without its backtrace (Ruby bug #21818), so .unpack sets it again.



12
13
14
15
16
# File 'lib/semantic_logger/ractor_copy.rb', line 12

def self.pack(log)
  backtraces = []
  log.each_exception { |exception, i| backtraces[i] = exception.backtrace }
  [log, backtraces]
end

.unpack(log, backtraces) ⇒ Object

Returns [Log] the log entry received from another Ractor (see .pack), with the backtraces of its exceptions.



20
21
22
23
24
25
26
# File 'lib/semantic_logger/ractor_copy.rb', line 20

def self.unpack(log, backtraces)
  log.each_exception do |exception, i|
    backtrace = backtraces[i]
    exception.set_backtrace(backtrace) if backtrace && exception.backtrace != backtrace
  end
  log
end