Project

General

Profile

Actions

Bug #19369

open

Small corner-case issue that breaks Ractor isolation: change cloned object from another thread

Added by luke-gru (Luke Gruber) almost 2 years ago. Updated almost 2 years ago.

Status:
Assigned
Target version:
-
[ruby-core:111979]

Description

I was looking into how objects are traversed for deep cloning and I came up with a way to break it. I don't think it'll ever happen in real life so it's not really an issue, just
an interesting case. Run with warnings disabled.

obj = Object.new
p "unshareable obj:", obj
UNSHAREABLE = obj
GO = false
SET = false
class Object
  attr_accessor :unshareable
  def initialize_clone(orig)
    puts "Clone called for #{orig.inspect}, self = #{self.inspect}"
    _self = self
    if orig == UNSHAREABLE
      t = Thread.new do
        puts "In thread"
        Thread.pass until GO
        puts "Setting unshareable!"
        # this must be done in separate thread to bypass object traversal deep-cloning
        _self.unshareable = UNSHAREABLE
        Object.const_set(:SET, true)
      end
    end
    super(orig)
  end
end

r = Ractor.new(obj) do |o|
  puts "from r#{Ractor.current.object_id} obj #{o.inspect}"
  GO = true
  loop until SET
  p "from ractor, got unshareable:", o.unshareable
end

r.take
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0