-
Notifications
You must be signed in to change notification settings - Fork 15
Allow use of DelegateClass in ractors #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
f2b2c0d to
81ea986
Compare
Tempfile uses DelegateClass and Tempfile should be able to be used by different ractors.
81ea986 to
cad1942
Compare
|
I would like to get this in before ruby 4 release please 🙏 |
|
This causes ruby 4.0 CI to crash.
I claim this should be reverted, if no other rationale. (#52) |
|
@luke-gru Can you describe what is the use-case of this? I'm wondering this is for your project of something. |
|
The use case is to be able to call I will look into the assertion failure in CI. |
|
Conclusion: I think it is hard to introduce it now because of essential problem. small repro code: class C
define_method :gets, &(Ractor.shareable_proc do
File.open("/tmp/foo").gets
# $_ is assigned
end)
end
C.new.gets
GC.verify_internal_consistencyThe problem is svar (storage of Some possibilities: |
|
Thank you for looking into this. That makes sense, I didn't realize this |
|
For diff --git a/lib/delegate.rb b/lib/delegate.rb
index 838de675f1..79b039af48 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -344,11 +344,13 @@ def __setobj__(obj)
end
end
-def Delegator.delegating_block(mid) # :nodoc:
- lambda do |*args, &block|
+def Delegator.delegating_code(mid)
+ <<~RUBY
+ def #{mid}(*args, &block)
target = self.__getobj__
- target.__send__(mid, *args, &block)
- end.ruby2_keywords
+ target.__send__(:'#{mid}', *args, &block)
+ end
+ RUBY
end
#
@@ -412,11 +414,11 @@ def __setobj__(obj) # :nodoc:
@delegate_dc_obj = obj
end
protected_instance_methods.each do |method|
- define_method(method, Delegator.delegating_block(method))
+ eval Delegator.delegating_code(method)
protected method
end
public_instance_methods.each do |method|
- define_method(method, Delegator.delegating_block(method))
+ eval Delegator.delegating_code(method)
end
end
klass.define_singleton_method :public_instance_methods do |all=true| |
|
I have made another PR for review after this one gets reverted with the suggestions from ko1. |
This is basically what TruffleRuby already does, the svars are never shared across Threads and not even across Fibers, they are always Fiber-local conceptually. And it can still be optimized for the common case of only a single Fiber accessing the svar during a method call. |
Tempfile uses DelegateClass and Tempfile should be able to be used by different ractors.