From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 9DBF51F4B4 for ; Sat, 16 Jan 2021 16:41:21 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 97F0E1209A2; Sun, 17 Jan 2021 01:40:26 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 719C81209A0 for ; Sun, 17 Jan 2021 01:40:23 +0900 (JST) Received: by filterdrecv-p3iad2-5599b4d97-qzm6r with SMTP id filterdrecv-p3iad2-5599b4d97-qzm6r-19-60031726-40 2021-01-16 16:41:10.960315223 +0000 UTC m=+155851.189761957 Received: from herokuapp.com (unknown) by geopod-ismtpd-2-4 (SG) with ESMTP id 79zmbkPhQSq0X7onxCzzPQ for ; Sat, 16 Jan 2021 16:41:10.929 +0000 (UTC) Date: Sat, 16 Jan 2021 16:41:10 +0000 (UTC) From: marcandre-ruby-core@marc-andre.ca Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 78017 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17543 X-Redmine-Issue-Author: marcandre X-Redmine-Issue-Assignee: ko1 X-Redmine-Sender: marcandre X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?6=2FIMxCQLDposcQf5wmbDAtfaKduBAO0bKyhL3BGZtMQ5q7K2TvpbN6A7JIyt9E?= =?us-ascii?Q?aOgg+AUI9we8odPSwND5NGW3M8rsXPOFgHghP7e?= =?us-ascii?Q?rdvqIAttGxMoNMP4dlHJd=2FrFTXbb=2FRVHPFC8o=2FA?= =?us-ascii?Q?Zd1a5Qpe9KYu2gMe1Ij+fyOd8GYq37=2FZIUz9O75?= =?us-ascii?Q?Tw2zC0Hn6tw4sZD=2F2plKyroUv8=2F76WmfFJj3aEH?= =?us-ascii?Q?wgKVpeUEqM+KZBloKNQurZ1r65YHTuIvWKxyio?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 102114 Subject: [ruby-core:102114] [Ruby master Bug#17543] Ractor isolation broken by `self` in shareable proc X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17543 has been updated by marcandre (Marc-Andre Lafortune). Eregon (Benoit Daloze) wrote in #note-3: > Not preserving `self` would be confusing, e.g., when calling other methods like: > ```ruby > class Foo > def bar > ... > end > > def foo > test { bar } > end > end > ``` In my proposal, calling that block would result in a `NoMethodError` on `<#DetachedSelf>`. Is it that confusing? The detached self could be created dynamically with the location of the call to `make_shareable` to help debugging. > I think the only case where it's acceptable to change the self of a proc implicitly I am not sure why you state "implicitly". It is explicit that `make_shareable` changes the block in more ways than just freezing, in particular by detaching the local variables and binding. > is `Ractor.new {}` because that already cannot capture any local variables: > ``` > ruby -e 'o=Object.new; Ractor.new { o }' > :267:in `new': can not isolate a Proc because it accesses outer variables (o). (ArgumentError) > ``` > > Capturing variables or self is very similar IMHO, so I think they should be treated the same. > ``` > ruby -e 'o=Object.new; Ractor.make_shareable(-> { o }); p o.frozen?' > :816:in `make_shareable': can not make shareable Proc because it can refer unshareable object # from variable `o' (Ractor::IsolationError) > ``` Note that they are *not* treated the same way. `Ractor.new{}` forbids any reference to outside variables, while `make_shareable(proc)` allows it as long as the values are themselves shareable. ``` o=Object.new.freeze Ractor.new { o } # => ArgumentError Ractor.make_shareable(-> { o }) # => no error ``` > The most obvious fix is therefore to raise the same error if `self` is not shareable. I agree that is the most obvious, but that makes `Ractor.make_shareable(-> { puts "hello" })` fail, which is far from being useful. ---------------------------------------- Bug #17543: Ractor isolation broken by `self` in shareable proc https://bugs.ruby-lang.org/issues/17543#change-89970 * Author: marcandre (Marc-Andre Lafortune) * Status: Open * Priority: Normal * Assignee: ko1 (Koichi Sasada) * ruby -v: 3.0.0p0 * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN ---------------------------------------- Discussing with @MaxLap we realized that the `self` in a shareable proc is not properly isolated: ``` class Foo attr_accessor :x def pr Ractor.make_shareable(Proc.new { self }) end end f = Foo.new f.x = [1, 2, 3] Ractor.new(f.pr) { |pr| pr.call.x << :oops } p f.x # => [1, 2, 3, :oops] ``` If the `self` refers to a shareable object then it's fine, but for non-shareable objects it has to be reset to `nil` or to a global shareable object that would have an instructive `inspect`. ```ruby Ractor::DETACHED_SELF = Object.new def << Ractor::DETACHED_SELF def inspect '<#detached self>' end alias to_s inspect end Ractor::DETACHED_SELF.freeze ``` -- https://bugs.ruby-lang.org/