From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-1.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [94.130.110.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 3CEEA1F5CB for ; Thu, 3 Oct 2024 06:43:25 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; unprotected) header.d=ml.ruby-lang.org header.i=@ml.ruby-lang.org header.a=rsa-sha256 header.s=mail header.b=LrJDy5Sw; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=TvmOIJt1; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1727937802; bh=QC0ajEoj3ZjHNXqH9TkEO0gO/GHDaqrPcarAxw+bsPg=; h=Date:References:To:Reply-To:Subject:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From:Cc:From; b=LrJDy5Swaj8ld/kgHcBnsO34tI+XWsU02BLDr1q6zTjarn3/kcCCSYI7VqAu6OxWf tVzUKX8rV8aqbIg8YB7e7/fFTaaZJqhpMH8P3tBj1KLO6PgbP+p+f3C5stajQrCcQb tSwsBg/MyTTBshUVsFQPwxx1HMjw7LjDq9jpz/Dg= Received: from nue.mailmanlists.eu (localhost [IPv6:::1]) by nue.mailmanlists.eu (Postfix) with ESMTP id E6A4B4406A for ; Thu, 3 Oct 2024 06:43:22 +0000 (UTC) Authentication-Results: nue.mailmanlists.eu; dkim=pass (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=TvmOIJt1; dkim-atps=neutral Received: from s.wfbtzhsv.outbound-mail.sendgrid.net (s.wfbtzhsv.outbound-mail.sendgrid.net [159.183.224.104]) by nue.mailmanlists.eu (Postfix) with ESMTPS id CCC3543F4E for ; Thu, 3 Oct 2024 06:43:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ruby-lang.org; h=from:references:subject:mime-version:content-type: content-transfer-encoding:list-id:to:cc:content-type:from:subject:to; s=s1; bh=JGqx6Sh+FJZ4WCMjGsiBp/WigZvEw3FlhqoXXkY/m7U=; b=TvmOIJt1jUSa93uz1BsB3XNmAoyhf8sk3+sGzhQalfMMhviFgd/vDNPSio+ix/EkbVf+ NDPhMH6qyLpPbN97+mXIdlVteWSf/i5oo/p/sji2yt+dB5fq0MgIQhdxsfXkeGEv3ZQpel JWOvrp56KlXMR4jqg9gXWNcTxS+7sekOq2ph1Vx+w6oS+ymPq4TExD0eEx90/z4TF8WBf3 NG6OiupxA4lOSCd0awvWnJfkl6kqtR4nMGQVAoADVtrdSFMlNbk7HaflZYsr6/BJivqHZZ QcWbuXKe0kM6CojucZq8RzhtTq9dGT/Rhu4b17dGOZ6dvUIJogmeTcpFDjeXVl6w== Received: by recvd-d66cff667-cnt4j with SMTP id recvd-d66cff667-cnt4j-1-66FE3CFE-9 2024-10-03 06:43:10.328490166 +0000 UTC m=+1773926.667461486 Received: from herokuapp.com (unknown) by geopod-ismtpd-5 (SG) with ESMTP id Z6rYhfauQJ-JKDnMUce8cQ for ; Thu, 03 Oct 2024 06:43:10.218 +0000 (UTC) Date: Thu, 03 Oct 2024 06:43:10 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 18127 X-Redmine-Issue-Author: rm155 X-Redmine-Issue-Priority: Normal X-Redmine-Sender: hsbt 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-Redmine-MailingListIntegration-Message-Ids: 96056 X-SG-EID: =?us-ascii?Q?u001=2ESNtsfy=2FRYrUmxOeGSN+Ah1new64UppKFg8YOAgra6KK6sIhqEP6LswYh4?= =?us-ascii?Q?tCf2AhAD20FCmXanLCSpdJ9Heq83FKB1eeA=2FIRz?= =?us-ascii?Q?=2FUFVHcMb3GN7+EqIdoK6yyPLKIMsCx6c5wCytZc?= =?us-ascii?Q?6X6VNMrz0L+E1j8CeqPEUEtBRIr8IRobwYggYpH?= =?us-ascii?Q?7VM35STBAzq=2FxblLCKBFFGZbRpj5Nvbdl61IEwg?= =?us-ascii?Q?prEo16JUQcEh98uRDS9Mh++hCIeSwwuhAXUoVDu?= =?us-ascii?Q?RdztsVouy4D69UuXhMZrh6Q2Gw=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: BN3GDG6ZK6VYNGJY5U5VKLWT25UC54I6 X-Message-ID-Hash: BN3GDG6ZK6VYNGJY5U5VKLWT25UC54I6 X-MailFrom: bounces+313651-b711-ruby-core=ml.ruby-lang.org@em5188.ruby-lang.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.9 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:119420] [Ruby master Feature#18127] Ractor-local version of Singleton List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "hsbt (Hiroshi SHIBATA) via ruby-core" Cc: "hsbt (Hiroshi SHIBATA)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #18127 has been updated by hsbt (Hiroshi SHIBATA). Status changed from Open to Closed I have been merged https://github.com/ruby/singleton/pull/4 ---------------------------------------- Feature #18127: Ractor-local version of Singleton https://bugs.ruby-lang.org/issues/18127#change-110034 * Author: rm155 (Rohit Menon) * Status: Closed ---------------------------------------- **Background** When the Singleton module (from the Singleton library) is included in a class, that class will have only one instance. Since the instance can only be in one Ractor at once, Singleton is not Ractor-compatible. For example, the following code would fail upon trying to access Example.instance in the Ractor: ``` ruby class Example def initialize @value = 1 end end Example.include Singleton Ractor.new do Example.instance end.take #=> can not access instance variables of classes/modules from non-main Ractors (Ractor::IsolationError) ``` In some cases, this may be the desired behavior, as it may be important that the class truly have only one instance. However, in many other cases, it would be more convenient for the class to have one instance per Ractor. **Proposal** The proposal is to create a RactorLocalSingleton module that can be included instead of Singleton to make the instance Ractor-local. Here is how RactorLocalSingleton might be used in the situation above: ``` ruby class Example def initialize @value = 1 end end Example.include RactorLocalSingleton Ractor.new do Example.instance end.take ``` **Discussion** The advantage of creating RactorLocalSingleton is that classes could have Singleton-like behavior while being usable in Ractors. Since some libraries, such as Prime, currently rely on the Singleton module, this would enable those libraries to have more flexibility with Ractors. The disadvantage of creating this module is that it supports the continued use of the Singleton design pattern, which is sometimes considered harmful. An alternative to RactorLocalSingleton might be to simply use Thread-local variables as Singleton instances. Here is how Thread-local variables might be used in the given situation: ``` ruby class Example def initialize @value = 1 end end Ractor.new do Thread.current[:Example] = Example.new Thread.current[:Example] end.take ``` **Summary** Classes that include Singleton are currently incompatible with Ractors. By instead including a new module RactorLocalSingleton, classes can have Singleton-like properties while being used in Ractors. However, this may perpetuate the use of the Singleton design pattern, and using Thread-local variables may be a preferable solution. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/