Project

General

Profile

Actions

Feature #21942

closed

Allow reading class variables from non-main Ractors

Feature #21942: Allow reading class variables from non-main Ractors
1

Added by tenderlovemaking (Aaron Patterson) 28 days ago. Updated 14 days ago.

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

Description

It's very common in Rails applications to use class variables, and today class variables aren't even allowed to be read inside a Ractor:

class Foo
  # This is NOT allowed to be read in non-main Ractors
  @@bar = 123
  def self.bar; @@bar; end

  # This is allowed to be read in non-main Ractors
  @baz = 123
  def self.baz; @baz; end
end

# This is OK
Ractor.new {
  p Foo.baz
}.value

# Exception here
Ractor.new {
  p Foo.bar
}.value

Output is like this:

test.rb:4:in 'Foo.bar': can not access class variables from non-main Ractors (@@bar from Foo) (Ractor::IsolationError)
	from test.rb:10:in 'block in <main>'

Can we allow shareable values to cross the Ractor boundary, similar to class instance variables?

Just for reference, the Rails code in question is here.

Updated by tenderlovemaking (Aaron Patterson) 28 days ago Actions #2

  • Assignee set to ractor

Updated by tenderlovemaking (Aaron Patterson) 14 days ago Actions #3

  • Status changed from Open to Closed

Applied in changeset git|ab32c0e690b805cdaaf264ad4c3421696c588204.


Allow reading cvars from non-main Ractors (#16308)

Today you can read instance variables from non-main Ractors, but many
Rails applications use cvars, and we cannot read them.

For example:

class Foo
  # This is NOT allowed to be read in non-main Ractors
  @@bar = 123
  def self.bar; @@bar; end

  # This is allowed to be read in non-main Ractors
  @baz = 123
  def self.baz; @baz; end
end

  # This is OK
Ractor.new {
  p Foo.baz
}.value

  # Exception here
Ractor.new {
  p Foo.bar
}.value

This commit changes the semantics of cvars to be the same as instance
variables:

  • It's ok to read Ractor shareable objects from the non-main Ractor
  • It's NOT ok to write from the non-main Ractor

[Feature #21942]

Actions

Also available in: PDF Atom