Project

General

Profile

Actions

Feature #22444

closed

Ractor.new should use Ractor.shareable_proc semantics for the block

Feature #22444: Ractor.new should use Ractor.shareable_proc semantics for the block
1

Added by jhawthorn (John Hawthorn) about 2 months ago. Updated 21 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:127115]

Description

#21557 introduced very well considered rules for Ractor.shareable_proc, where locals can be referenced so long as they are shareable and never reassigned. However Ractor.new { ... } continues to use a more restrictive rule that no local variable access is allowed at all.

I'd like to propose that Ractor.new follow the same rules as Ractor.shareable_proc. This simplifies implementation, and should be simpler for users both because of what it allows and because of consistency.

outer_var = :ok
Ractor.new { puts outer_var }.join # raise on 4.0, I propose we allow this
Ractor.new(outer_var) { |inner_var| puts inner_var }.join # old way to do this, still works

# This doesn't add any new abilities, as one could already emulate this with the following on 4.0
pr = Ractor.shareable_proc { puts outer_var } # allowed, because no reassignment to outer_var
Ractor.new(pr){ |pr| pr.call }.join # allowed, because pr is shareable

unshareable = Object.new
reassigned = :bad
Ractor.new { puts unshareable }.join # still raises because unshareable
Ractor.new { puts reassigned }.join # still raises due to reassignment
reassigned = :still_bad

self continues to be the new Ractor object.

One additional change here is that previously the proc passed to Ractor.new(&some_proc) would actually be mutated to be shareable. I don't think this was intentional and we should remove this behaviour.


Related issues 2 (1 open — 1 closed)

Related to Ruby - Feature #21557: Ractor.shareable_proc to make sharable Proc objects, safely and flexiblyClosedko1 (Koichi Sasada)Actions
Related to Ruby - Feature #22523: define_method should make its body shareable when possible, using Ractor.shareable_proc semanticsOpenractorActions

Updated by jhawthorn (John Hawthorn) about 2 months ago Actions #1

  • Description updated (diff)

Updated by jhawthorn (John Hawthorn) 26 days ago Actions #2

  • Status changed from Open to Closed

Applied in changeset git|1f3e810694485f284871aee695a1a9d15467c983.


Ractor.new: use Ractor.shareable_proc semantics for the block [Feature #22444]

Previously Ractor.new isolated the given block Proc in place
(rb_proc_isolate_bang) when the ractor's thread was set up. This
mutated the caller's Proc, and rejected every outer variable access.

Now Ractor.new duplicates the block and makes the copy shareable exactly
as Ractor.shareable_proc does, before any ractor resources are
allocated. The block may read outer variables that are never
reassigned and refer to shareable objects; writes, reassigned
variables, unshareable values and yield still raise
Ractor::IsolationError. The original Proc is left unchanged (not frozen,
not shareable), and instance variables on it no longer prevent use, as
the copy does not carry them.

Procs without an iseq (e.g. method procs) keep using rb_proc_isolate.
Tests and docs are updated accordingly.

Updated by Eregon (Benoit Daloze) 25 days ago Actions #3

  • Related to Feature #21557: Ractor.shareable_proc to make sharable Proc objects, safely and flexibly added

Updated by Eregon (Benoit Daloze) 25 days ago Actions #4 [ruby-core:127344]

Nice and agreed, this seems far more convenient and already envisioned in #21557.

Maybe we should do the same for define_method, but just fallback to current behavior if not possible to make the block shareable (clearly not for this ticket though).

Updated by rkh (Konstantin Haase) 25 days ago Actions #5 [ruby-core:127346]

Eregon (Benoit Daloze) wrote in #note-4:

Nice and agreed, this seems far more convenient and already envisioned in #21557.

Maybe we should do the same for define_method, but just fallback to current behavior if not possible to make the block shareable (clearly not for this ticket though).

I think this would be great, especially for making it easier for existing libraries to become Ractor compatible.

Updated by Eregon (Benoit Daloze) 21 days ago Actions #6

  • Related to Feature #22523: define_method should make its body shareable when possible, using Ractor.shareable_proc semantics added
Actions

Also available in: PDF Atom