Project

General

Profile

Actions

Bug #19376

closed

Ability to read ivars of non-frozen classes/modules across ractors is not thread-safe

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

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:112038]

Description

This segfaults:

class C
  class << self
    attr_accessor :val
  end
end

GO = false
rs = 100.times.map do
  Ractor.new do
    Thread.pass until GO
    1000.times do
      val = C.val
      p val
    end
  end
end
ts = 100.times.map do |i|
  Thread.new do
    Thread.pass until GO
    1000.times do
      C.val = Object.new.freeze
    end
  end
end

GO = true
ts.each(&:join)
rs.each(&:take)

It looks like a silly example, but it can crash even if the main thread doesn't spawn any more threads. Getting/setting ivars on objects in parallel without VM lock isn't safe, so for non-frozen shareable objects (basically classes and modules), it's unsafe to access their instance variables even if the values are shareable.

Actions

Also available in: Atom PDF

Like0
Like0Like0