-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Ractor shareable proc #13926
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
Ractor shareable proc #13926
Conversation
f05ec1f
to
50b94b7
Compare
50b94b7
to
7a2f07f
Compare
❌ Tests Failed✖️no tests failed ✔️62175 tests passed(1 flake) |
234be4a
to
0e8659b
Compare
407e392
to
535130d
Compare
call-seq: Ractor.sharable_proc(self: nil){} -> sharable proc It returns shareable Proc object. The Proc object is shareable and the self in a block will be replaced with the value passed via `self:` keyword. In a shareable Proc, the outer variables should * (1) refer shareable objects * (2) be not be overwritten ```ruby a = 42 Ractor.shareable_proc{ p a } #=> OK b = 43 Ractor.shareable_proc{ p b; b = 44 } #=> Ractor::IsolationError because 'b' is reassigned in the block. c = 44 Ractor.shareable_proc{ p c } #=> Ractor::IsolationError because 'c' will be reassigned outside of the block. c = 45 d = 45 d = 46 if cond Ractor.shareable_proc{ p d } #=> Ractor::IsolationError because 'd' was reassigned outside of the block. ``` The last `d`'s case can be relaxed in a future version. The above check will be done in a static analysis at compile time, so the reflection feature such as `Binding#local_varaible_set` can not be detected. ```ruby e = 42 shpr = Ractor.shareable_proc{ p e } #=> OK binding.local_variable_set(:e, 43) shpr.call #=> 42 (returns captured timing value) ``` Ractor.sharaeble_lambda is also introduced. [Feature #21550] [Feature #21557]
to catch up new sharable proc semantics.
535130d
to
8c3a48d
Compare
} | ||
|
||
# Ractor.make_shareable(a_proc) makes a proc shareable. | ||
# Ractor.make_shareable(a_proc) is not supported now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this only error because self is not shareable:
$ ruby -ve 'pr = Proc.new{}; Ractor.make_shareable(pr)'
ruby 3.5.0dev (2025-09-23T19:05:52Z master 0fe1099d9a) +PRISM [x86_64-linux]
-e:1:in 'Ractor.make_shareable': Proc's self is not shareable: #<Proc:0x00007f9341328148 -e:1> (Ractor::IsolationError)
from -e:1:in '<main>'
It seems to work the same as shareable_proc
if self
is shareable (good for compatibility):
$ ruby -e 'nil.instance_exec { pr = Proc.new{}; p Ractor.make_shareable(pr) }'
#<Proc:0x00007fd5eb078050 -e:1>
(so this comment should probably be updated)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, my comment was wrong. make_shareable(proc)
is not prohibited now.
Ractor.shareable_proc
call-seq:
Ractor.sharable_proc(self: nil){} -> sharable proc
It returns shareable Proc object. The Proc object is
shareable and the self in a block will be replaced with
the value passed via
self:
keyword.In a shareable Proc, the outer variables are read-only
and the outer variables should point only shareable objects.
Ractor.sharaeble_lambda is also introduced.
Ractor.make_sharable(a_proc) is no longer supported.
[Feature #21039]