Project

General

Profile

Actions

Feature #18137

open

A new method to check Proc is isolated or not

Added by tagomoris (Satoshi TAGOMORI) over 1 year ago. Updated about 1 year ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:105079]

Description

I want to check a Proc is isolated or not, like Proc#isolated?.

Proc objects are passed to libraries very often. For example, Rack web application is a callable (respond_to(:call)) object, and it may be a Proc.
When the library will call that Proc object in a Ractor, the passed Proc should be isolated by Ractor.make_shareable(). Otherwise, it causes RuntimeError.

So I want to check the Proc object is isolated or not earlier. It should be very helpful for library users because of the early and clear error messages.

Updated by ko1 (Koichi Sasada) about 1 year ago

Ractor.shareable?(proc_object) is not enough?

Updated by ko1 (Koichi Sasada) about 1 year ago

I'm afraid that we can define "Isolated Proc".

As I know there are several versions.

  1. self
    a: self is shareable
    b: self is not shareable

  2. reference to outer scope variables
    x: no references
    y: has references, but read-only and values are fixed (if outer-scope variables are modified by other code, but keep to refer original values).
    z: has references, but the variable storage is independent from outer-scope.

example of y:

i = 1
pr = proc{
  p i #=> always 1
}
i = 2
pr.call #=> 1

example of z:

i = 1
pr = proc{
  p i += 1
}
i += 10
pr.call #=> 2

From Ractor perspective, x and y are acceptable. for a and b,
Ractor doesn't care because the self will be replaced with Ractor.new{} and define_method. Proc#call is not allowed (!! Now it is allowed. BUG!! https://bugs.ruby-lang.org/issues/18243).

Actions

Also available in: Atom PDF

Like0
Like0Like0