[ruby-core:121468] [Ruby Feature#17414] Ractor should allow access to shareable attributes for Modules/Classes
From:
"jhawthorn (John Hawthorn) via ruby-core" <ruby-core@...>
Date:
2025-03-28 22:34:00 UTC
List:
ruby-core #121468
Issue #17414 has been updated by jhawthorn (John Hawthorn).
Status changed from Assigned to Closed
I think we can close this as accessing the class instance variables has been allowed for a while (Ruby 3.1?)
``` ruby
module Config
class << self
attr_accessor :conf
end
self.conf = 42
end
Ractor.new { Config.conf = 66 }.take # => can not set instance variables of classes/modules by non-main Ractors (Ractor::IsolationError)
Ractor.new { puts Config.conf }.take # => 42
```
----------------------------------------
Feature #17414: Ractor should allow access to shareable attributes for Modules/Classes
https://bugs.ruby-lang.org/issues/17414#change-112475
* Author: marcandre (Marc-Andre Lafortune)
* Status: Closed
* Assignee: ko1 (Koichi Sasada)
----------------------------------------
Current situation is *very* limiting.
Use-case: global config.
Example: [yaml has a global config](https://github.com/ruby/psych/blob/master/lib/psych.rb#L637-L640) and it's not clear to me how to make that Ractor-aware (nicely).
It is possible to have the same effect but in ugly ways:
```ruby
# Using instance variables of Module not allowed:
module Config
class << self
attr_accessor :conf
end
self.conf = 42
end
Ractor.new { Config.conf = 66 }.take # => can not access instance variables from non-main Ractors
Ractor.new { puts Config.conf }.take # => can not access instance variables from non-main Ractors
# Same functionality using constants allowed:
module Config
class << self
def conf
CONF
end
def conf=(new_conf)
remove_const(:CONF)
const_set(:CONF, new_conf)
end
end
CONF = 42
end
Ractor.new { Config.conf = 66 }.take # => ok
Ractor.new { puts Config.conf }.take # => 66
# Same functionality using methods allowed:
module Config
class << self
def conf
42
end
def conf=(new_conf)
singleton_class.undef_method(:conf)
define_singleton_method(:conf, &Ractor.make_shareable(Proc.new { new_conf }))
end
end
end
Ractor.new { Config.conf = 66 }.take # => ok
Ractor.new { puts Config.conf }.take # => 66
```
The priority would be to allow reading these instance variables if they are shareable. Ideally writing would also be allowed, but limiting that to main ractor is less probablematic than with reading.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/