From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-2.5 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.1 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id F27101F453 for ; Tue, 30 Oct 2018 15:09:04 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 375C1120E12; Wed, 31 Oct 2018 00:09:02 +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 5C6AD120DFC for ; Wed, 31 Oct 2018 00:08:59 +0900 (JST) Received: by filter0090p3iad2.sendgrid.net with SMTP id filter0090p3iad2-9011-5BD87407-19 2018-10-30 15:08:55.467285431 +0000 UTC m=+21628.923118402 Received: from herokuapp.com (ec2-54-221-178-180.compute-1.amazonaws.com [54.221.178.180]) by ismtpd0001p1iad2.sendgrid.net (SG) with ESMTP id dW9qnySOTViXn4ru-NaRxg Tue, 30 Oct 2018 15:08:55.531 +0000 (UTC) Date: Tue, 30 Oct 2018 15:08:55 +0000 (UTC) From: lars@greiz-reinsdorf.de To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 65028 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15262 X-Redmine-Issue-Author: larskanis X-Redmine-Sender: larskanis 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS5jobV+Dlf5ATOJKKX0P9bXicQ8PGuPUX57ab uNC+ARC0fbfY0P1lDN3otzeckhDzloTHxK4aQdklLDYLc+zL1kthB2IDHhsA5/5EGGSHlfYO65VGZ0 MIlyqnIJxmlm6cuNb1XT2JhxvVcVbo6fPpY5GPU1W94Kh0vKabsxR9ea+g== X-ML-Name: ruby-core X-Mail-Count: 89643 Subject: [ruby-core:89643] [Ruby trunk Bug#15262] WeakRef::RefError for object that is still in use 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 #15262 has been updated by larskanis (Lars Kanis). @nobu I updated the bug report, so that it should be more understandable now, what's going wrong. ---------------------------------------- Bug #15262: WeakRef::RefError for object that is still in use https://bugs.ruby-lang.org/issues/15262#change-74683 * Author: larskanis (Lars Kanis) * Status: Feedback * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.6.0dev (2018-10-27 trunk 65390) [x86_64-linux] * Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN ---------------------------------------- Given the following program: ```ruby require "weakref" Thread.abort_on_exception = true class Adder def self.start_adder(obj, oid) obj.add end def initialize @qu = Queue.new count = 10 count.times do Thread.new(WeakRef.new(self), object_id, &self.class.method(:start_adder)) end count.times do @qu.pop end end def add @qu.push true end end def test_adder 10.times.map do Thread.new do Adder.new end end.each(&:join) end 1000.times do test_adder end ``` ## Expected behaviour: The program should simply execute without error. This is the case on JRuby but not on MRI. ## Actual behavior: The program stops with a probability of approximately 50% with the following error: ``` $ ruby -W2 adder-test.rb # terminated with exception (report_on_exception is true): Traceback (most recent call last): adder-test2.rb:7:in `start_adder': Invalid Reference - probably recycled (WeakRef::RefError) ``` Although `start_adder` works with a `WeakRef`, the `Adder` object should still be GC marked and therefore kept usable per `WeakRef` until `Adder.new` exited. This is because `Adder.new` waits for completion of all Threads to have called `start_adder`. If `Adder.start_adder` is changed to catch the `WeakRef` error like so: ```ruby def self.start_adder(obj, oid) obj.add rescue WeakRef::RefError end ``` ... then the ruby VM detects a deadlock, which shows that `@qu.pop` is still executed within `initialize`. Therefore `WeakRef#add` should not raise a `WeakRef::RefError` to that point in time, but allow access to the object: ``` Traceback (most recent call last): 5: from adder-test2.rb:35:in `
' 4: from adder-test2.rb:35:in `times' 3: from adder-test2.rb:36:in `block in
' 2: from adder-test2.rb:32:in `test_adder' 1: from adder-test2.rb:32:in `each' adder-test2.rb:32:in `join': No live threads left. Deadlock? (fatal) 2 threads, 2 sleeps current:0x000056520aa3cee0 main thread:0x000056520a5f2ff0 * # rb_thread_t:0x000056520a5f2ff0 native:0x00007f7ccf535740 int:0 adder-test2.rb:32:in `join' adder-test2.rb:32:in `each' adder-test2.rb:32:in `test_adder' adder-test2.rb:36:in `block in
' adder-test2.rb:35:in `times' adder-test2.rb:35:in `
' * # rb_thread_t:0x000056520a609fc0 native:0x00007f7ca54d4700 int:0 depended by: tb_thread_id:0x000056520a5f2ff0 adder-test2.rb:18:in `pop' adder-test2.rb:18:in `block in initialize' adder-test2.rb:17:in `times' adder-test2.rb:17:in `initialize' adder-test2.rb:30:in `new' adder-test2.rb:30:in `block (2 levels) in test_adder' ``` I verified this on ruby-trunk, but get the same behavior on all older MRI versions. ---Files-------------------------------- adder-test2.rb (476 Bytes) -- https://bugs.ruby-lang.org/