Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ko1
Copy link
Contributor

@ko1 ko1 commented Jul 17, 2025

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.

    a = 42
    b = []
    Ractor.shareable_proc{ p a }.call # ok
    Ractor.shareable_proc{ p b }
    #=> can not make shareable Proc because it can refer unshareable object [] from variable 'b' (Ractor::IsolationError)

Ractor.sharaeble_lambda is also introduced.

Ractor.make_sharable(a_proc) is no longer supported.

[Feature #21039]

@ko1 ko1 force-pushed the ractor_shareable_proc branch 4 times, most recently from f05ec1f to 50b94b7 Compare July 17, 2025 09:50
@ko1 ko1 force-pushed the ractor_shareable_proc branch from 50b94b7 to 7a2f07f Compare August 27, 2025 18:03
Copy link

launchable-app bot commented Aug 27, 2025

Tests Failed

✖️no tests failed ✔️62175 tests passed(1 flake)

@ko1 ko1 force-pushed the ractor_shareable_proc branch 2 times, most recently from 234be4a to 0e8659b Compare August 28, 2025 06:27
@ko1 ko1 force-pushed the ractor_shareable_proc branch 2 times, most recently from 407e392 to 535130d Compare September 18, 2025 17:51
ko1 added 2 commits September 24, 2025 01:04
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.
@ko1 ko1 force-pushed the ractor_shareable_proc branch from 535130d to 8c3a48d Compare September 23, 2025 16:04
@ko1 ko1 merged commit 8fad3e8 into ruby:master Sep 23, 2025
94 of 98 checks passed
}

# Ractor.make_shareable(a_proc) makes a proc shareable.
# Ractor.make_shareable(a_proc) is not supported now.
Copy link
Member

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)

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants